1. Create a new target app and select today extension.
Http://blog.csdn.net/wjsxiaoweige/article/details/39524291Establish phone extension
We will find a new entitlement. Don't worry. I will tell you how it came out later.
2. It is very easy to build the sb of the today extension app. If your UI is very complicated, it doesn't matter.
3. Run the project and check the effect of the today extension app. Remember to select today to see the effect directly.
4 is it very simple, but the data displayed here can only be fixed. We need the actual data of our application, rather than meaningless data, so how can we connect it with our application, add group is required.
Add group is a secure data sharing method provided by Apple.
First, select the application we want to share and create the APP groups
Open the APP group button. in the middle, we need to correctly configure the appid to go online. So if you just want to learn it yourself, ignore the red exclamation point below.
5. Click + to create an Add a new container and fill in the name group. todaydataextensiondefaults OK. In the same way, create the same new container in the today extension app
After completing the above two steps, you will see the first step entitlements, which is the container for sharing data.
6. the following code will show you how to share the application data:
NSUserDefaults * shareDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.TodayDataExtensionDefaults"]; [shareDefaults setObject:@"this is the test1111" forKey:@"test"]; [shareDefaults synchronize];
Note that the alloc method is unavailable for the specified instance.
7. Add the following method to todayviewcontroller to listen to changes in usetdefault:
- (instancetype)initWithCoder:(NSCoder *)coder{ self = [super initWithCoder:coder]; if (self) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDefaultChaneg:) name:NSUserDefaultsDidChangeNotification object:nil]; } return self;}
8. Change the value of the today control in the notification method.
- (void)userDefaultChaneg:(id)sener{ NSUserDefaults * defaultes = [[NSUserDefaults alloc] initWithSuiteName:@"group.TodayDataExtensionDefaults"]; NSString * string = [defaultes valueForKey:@"test"]; NSLog(@"string = %@",string); self.myLable.text = string; }
For other knowledge, we can create a button to quickly open our application.
NSURL * url = [NSURL URLWithString:@"readbook://a=1"]; [self.extensionContext openURL:url completionHandler:^(BOOL success) { }];
Today extension and add Group