Today, I occasionally tinker with my cell phone and find these things. I just want to do them.

  • Add the Today Extension (What? You didn’t find it. Create Target, that’s it.)
  • Code written
// It is very clear and simple. // Call app [self.extensionContext openURL:[NSURL URLWithString:@ via host app"widgetsam://sam..."] completionHandler:^(BOOL success) {
        NSLog(@"open url result:%d 🐒 %d",success ,testNumber); }]; -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; / / add the folding effect self. ExtensionContext. WidgetLargestAvailableDisplayMode = NCWidgetDisplayModeExpanded; } -(void)widgetActiveDisplayModeDidChange:(NCWidgetDisplayMode)activeDisplayMode withMaximumSize:(CGSize)maxSize { /** After iOS10, the size of Today Extension was redefined. The width is fixed (359 on the iPhone6, for example), so it cannot be changed; But provides two modes: high NCWidgetDisplayModeCompact: fixed height, 110 NCWidgetDisplayModeExpanded: you can change the height, range of 110 ~ 616 * /if (activeDisplayMode == NCWidgetDisplayModeCompact) {
        self.preferredContentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 110);
    } else{ self.preferredContentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 250); }}Copy the code
  • The data sharing extension is isolated from the host App, so the data sharing needs to use App Groups.

In the App Target’s Capabilities bar, go to the App Groups TAB, enable the feature, and click the “+” symbol to add a shared data container name, such as group.xxx. We then see entitlements generated in both the main Target and extended Target directories, documenting an App Groups item. This shared container is the space that holds the data shared by the extension and the host App. To compile properly, you also need to go to the Developer Center, edit the main App and the extended AppID, and enable support for App Groups, similar to enabling push. After the configuration is complete, it is used. Regardless of which data store or operation you use, UserDefaults, Archive, CoreData, FMDB, LevelDB, etc., just point the path to the shared container path.

  • Full code Demo

The end of the