The profile
The purpose of this article is to document the implementation flow of iOS todayWidget.
implementation
Add TodayWidget
1. Add a target as shown below (the TodayWidget in the image is the component I have added)
2, select Today Extension
3. Click Next. The result is shown below. (It should only have ViewController files at the beginning, I added the other classes later.)
UI effect implementation
1. There’s nothing to go into here. It’s no different from iOS native development
self.extensionContext.widgetLargestAvailableDisplayMode = NCWidgetDisplayModeExpanded;
Copy the code
2, implement two agents, the comments are clear, there is nothing to say.
// If implemented, the system will call at opportune times for the widget to update its state, both when the Notification Center is visible as well as in the background. // An implementation is required to enable background updates. // It's expected that the widget will perform the work to update asynchronously and off the main thread as much as possible. // Widgets should call the argument block when the work is complete, passing the appropriate 'NCUpdateResult'. // Widgets should NOT block returning from 'viewWillAppear:' on the results of this operation. // Instead, widgets should load cached state in 'viewWillAppear:' in order to match the state of the view from the last 'viewWillDisappear:', then transition smoothly to the new data when it arrives. - (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult result))completionHandler; // If implemented, called when the active display mode changes. // The widget may wish to change its preferredContentSize to better accommodate the new display mode. - (void)widgetActiveDisplayModeDidChange:(NCWidgetDisplayMode)activeDisplayMode WithMaximumSize: CGSize maxSize __API_AVAILABLE (ios (10.0));Copy the code
Data interaction
-
The premise of data interaction is to set App Groups, and the specific steps are as follows
- Click the button in the red box below, select App Groups when it pops up, then click “+” under App Groups and enter the name. Set the two targtes that need to communicate separately. In this example, select “Premium” and “TodayWidget”. Remember to check the name you just set.
- Click the button in the red box below, select App Groups when it pops up, then click “+” under App Groups and enter the name. Set the two targtes that need to communicate separately. In this example, select “Premium” and “TodayWidget”. Remember to check the name you just set.
-
Data passing and event passing
- There are two communication modes between the two targets of App Groups, both of which have advantages and disadvantages
-
Notice the way CFNotificationCenterPostNotification,
/ / send notifications - (void) postNotificaiton {CFNotificationCenterRef notification = CFNotificationCenterGetDarwinNotifyCenter (); CFNotificationCenterPostNotification(notification, CFSTR("com.your.name.to_app"), NULL,NULL, YES); } // Register to receive notifications - (void)receiveNotification {CFNotificationCenterRef Notification = CFNotificationCenterGetDarwinNotifyCenter (); CFNotificationCenterAddObserver(notification, (__bridge const void *)(self), observerMethod,CFSTR("com.your.name.to_weiget"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately); } void observerMethod (CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) { NSNotification *notification = [[NSNotification alloc] initWithName:@"callBack" object:nil userInfo:nil]; [[NSNotificationCenter defaultCenter] postNotification:notification]; }Copy the code
-
Data is shared through NSUserDefaults, and access is similar. Be careful about how you instantiate NSUserDefaults and make sure suiteName is consistent
NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.ingeek.widget"]; NSArray *array = [userDefaults objectForKey:@"group.com.ingeek.your.suiteName"]; Copy the code
-
- Notifications can only send event responses, but cannot send or receive data. NSUserDefaults can only share data, so it is best to use both methods together.
- There are two communication modes between the two targets of App Groups, both of which have advantages and disadvantages
The end of the
Are called official Api, dry goods are not much. This article on one hand to record their own implementation process, and secondly to throw a brick to introduce jade, mainly data interaction part, there may be a better way, also hope to see the officer not hesitate to give advice.