Today extension and add Group

Source: Internet
Author: User
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

Related Article

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.