Development and use of iOS development widgets (lower), ioswidget

Source: Internet
Author: User

Development and use of iOS development widgets (lower), ioswidget
In the development and use of iOS development widgets (I), I have clearly explained the addition of Widget extensions and the layout. The following describes the key parts: data sharing operations.
After Apple releases iOS8.0, it provides a new extension option for the App, called App groups. After selecting the host Target, this option is found under the capabilities tab:
What is the main purpose of this option?
In iOS systems, every development should be clear that its internal programs follow the sandbox mechanism. Data sharing is not allowed between apps, and A cannot access B's data, similarly, B cannot access data of. Don't ask why, iOS is born so capricious.
In order to allow widgets to share data in the Host Program, the App groups function came into being.
Select the Target of the Host Program, open this option, and add the key you need, as shown in the figure below. These are the two groups I added:
These two sets of keys can be used as the main and key part for sharing the data of the Host Program to the extension.
Then select the Target of the extension program. In the same operation, select Open App groups. The difference is that the key here does not need to be added, but is checked.
After these tasks are completed, the basic data sharing process has been completed for more than half, and the work of the Code part of the specific data sharing part has been completed.
Through App groups, you can use NSUserdefault to share small data with the Host Program. Of course, it may also be CoreData, sqlite or other big data sharing.

When NSUserDefault is used for data sharing, if you only store data normally, read the data as follows:

[[NSUserDefaultsstandardUserDefaults] setObject: @ "" forKey: @ "xxx"];

[[NSUserDefaultsstandardUserDefaults] valueForKey: @ "xxx"];

Data cannot be normally read in the widget.


Therefore, when initializing NSUserDefaults, you need to do this:

NSUserDefaults * userDefault = [[nsuserdefasalsalloc] initWithSuiteName: k_APP_GROUP_KEY];

[UserDefault setValue: dic forKey: @ ""];

[UserDefault synchronize];

In this way, after the data is stored, and then the extended ViewDidLoad initializes and reads the data in the same way, the data of the normal root Host Program can be interconnected. Group_Key is the name of the Key filled in when the App Group is added.


Now, you can complete an operation to share data with the extension using NSUserdefaults.


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.