Development and use of widgets for IOS development (next)

Source: Internet
Author: User

in the development and use of the iOS Development widget (above), the additions to the widget extension and the contents of the Layout section are clearly explained, and the key part: operations on data sharing.
After Apple publishes iOS8.0, there is a new extension option for the app, called app groups, which is found below the Capabilities tab after the host target is selected:
So what's the main use of this option?
iOS system, each development should be clear, its internal procedures are to follow the sandbox mechanism, the app and the app, is not data sharing, a can not access B data, also B can not access a data. Don't ask why, iOS is inherently self-willed.
To allow widgets to be shared with the data in the host program, the APP groups this feature.
Select the target of the host program, open this option, then add the key you need to go in, as shown in the following figure, this is the two groups I added:
These two sets of keys can be used as a primary and critical part of the extension that you share with the host program's data.
then in the selection of the extension of the target, the same operation, choose to open the app groups, just different is that the key here does not need you to add, but to tick.
after these things, the basic data sharing step is more than half, down is the specific data sharing section of the code part of the work.
With app groups, you can use Nsuserdefault and host programs for small data sharing, but it's also possible to share CoreData, SQLite, or other big data.

When using Nsuserdefault for data sharing, if you just store the data regularly, read the data such as:

[[nsuserdefaultsstandarduserdefaults] setobject:@ "" forkey:@ "xxx"];

[[nsuserdefaultsstandarduserdefaults] valueforkey:@ "XXX"];

is not able to read the data in the widget properly.


So, when initializing the Nsuserdefaults, do this:

Nsuserdefaults *userdefault=[[nsuserdefaultsalloc] initwithsuitename :k_app_group_key];

[Userdefault setValue:d ic forkey:@ ""];

[Userdefault synchronize];

This initializes, stores the data, and then initializes and reads the data in the same way in the extended viewdidload in order to interoperate with the normal root host program's data. Group_key is the name of the key that was filled in when the app group was added.


Well, see here, it should be possible to complete a data sharing by nsuserdefaults to extend the operation of the program, you can try.


Development and use of widgets for IOS development (next)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.