Precautions for App Today Extension development, appextension

Source: Internet
Author: User

Precautions for App Today Extension development, appextension

App Extension has been available since iOS 8. Extension has been expanded to 19 categories and has been widely used.

Extension can almost be seen as an embedded independent App with independent BundleID, certificate, profile, process space, sandbox, and so on. It only needs to be packaged in the App, similar to the parasitic in the host App, bundled for installation. However, once the application is installed, the extension can be called by the system independently to execute the code logic in the extension.

This article only records the precautions during the previous development of Today Extension. In fact, most of them are generic.

 

1. Create Extension and Certificate Management

In a created Project, Create File> New> Target> Today Extension. After you confirm the name, you can see the New extension in the Targets List of the project File.

In the General column, we can see that Extension has independent Identity content, and Bundle Identifier generally has the Bundle Identifier prefix of the Host application. If you use the Xcode8 Automatic Management Certificate and log on to the developer account, you can view the automatically created AppID In the Developer Center.

Create Provisioning files for the AppID.

 

2. MainInterface. storyboard and main classes

The newly created Extension is isolated from the main Target of the App in different file directories. Main Interface, Info. plist and localization files are managed separately, so you can create and modify InfoPlist. the CFBundleDisplayName field in the strings localization file, which is the extension name. If it is not modified, the display name of the host App is used by default.

Extensions are isolated from the host App and cannot use their classes, frameworks, and resources. If you want to use the host class library or resource files, the most direct method is to copy the file to the current Target or add a Link framework. However, to view the Build Settings of Extension, you can enter the Prefix Header of the host App and use the macro definition in the referenced Header file.

By default, the extension uses MainInterface. storyboard, binds a class TodayViewController, and creates a Hello World view, which can be directly used and modified. If you do not want to use storyboard or need to modify the startup class, you can set Info. remove the NSExtensionMainInterface item of the NSExtension dictionary in the plist file and add the NSExtensionPrincipalClass item. value indicates the Class Name of the specified startup class, for example, TodayViewController.

 

3. Lifecycle

Like the host's View Controller, the extended View Controller has a common life cycle method. For example, if the current application extension is displayed in the drop-down "today" list for the first time, the viewDidLoad method is executed, and other methods are executed in sequence; the method disappears and appears again, the dealloc method is called when the disappear and appear methods are to be destroyed.

 

4. size of Today Extension

After ios10. The width is fixed (for example, it is 359 on iPhone 6), so it cannot be changed; but the height provides two modes:

NCWidgetDisplayModeCompact: fixed height, 110

NCWidgetDisplayModeExpanded: the changeable height. The interval is 110 ~ 616

Use the following code to modify the mode:

If ([[UIDevice currentDevice]. systemVersion floatValue]> = 10 ){

Self. extensionContext. widgetLargestAvailableDisplayMode = NCWidgetDisplayModeExpanded;

}

We can obtain the maximum size range from the following proxy methods of the NCWidgetProviding protocol:

-(Void) widgetActiveDisplayModeDidChange :( NCWidgetDisplayMode) activeDisplayMode withMaximumSize :( CGSize) maxSize NS_AVAILABLE_IOS (10_0 );

Use the following method to determine the margin:

-(UIEdgeInsets) Grouping :( UIEdgeInsets) defaultMarginInsets NS_DEPRECATED_IOS (8_0, 10_0, "This method will not be called on widgets linked against iOS versions 10.0 and later .");

 

To modify the background height of a Widget, you can modify the attribute self. preferredContentSize.

 

5. Data Sharing

Since the extension is isolated from the host App, App Groups is required for data sharing.

In the Capabilities column of the App master Target, find the App Groups item, enable the function, and click the "+" symbol to add a shared data container name, such as group. xxx. Then, an entitlements file is generated in the main Target and extended Target directories, recording an App Groups item.

The shared container stores the data space shared by the extension and the host App.

For normal compilation, you also need to go to the Developer Center, edit the main application and the extended AppID, enable the support for App Groups function, similar to enabling the push function.

After the configuration is complete, it is used. No matter which data storage or operation methods are used, such as UserDefaults, Archive, CoreData, FMDB, and LevelDB, you only need to direct the path to the shared container path.

For example, the UserDefaults method is used:

   NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.xxx"];    [defaults setObject:@"xxxxx" forKey:@"phoneNum"];

 

Only the specified userults ults object is opened, and the instance method used is initWithSuiteName.

The same applies to other methods.

 

6. Class Library and Resource Sharing

As mentioned above, the extension needs to use the class libraries and resources of the host Target, which can be directly copied to the extended Target directory. However, this will increase the application volume.

Another method is to create a framework dynamic link library. Create a File-> New-> Target-> Framework to get a New Target. Similar to the system Framework, we can freely Add the class File encapsulation function to expose only the public header File for calling. In the Headers item in the Build Phases column, drag the header file of the Project group to the Public group.

The framework also has a Bundle ID, but you do not need to pay attention to it and do not need to configure certificates.

Note that the newly created dynamic link library automatically creates header files with the same name. You can directly reference the custom Class header files to be exposed. However, if the header file is deleted, the following warning may occur:

Warning: no umbrella header found for target 'orderfoodtodaykit', module map will not be generated

 

In addition, we need to pay special attention to this because the dynamic link library we created needs to be provided to the extension, but the extension does not support some APIs, therefore, some APIs not supported by extensions must be removed from the framework. In Deployment Info of framework, select Allow app extension API only. In this case, if an Api not supported by the extension is used in the framework, an error will be reported during compilation.

 

7. The Today Extension calls the host App

If you need to call the host App from Today Extension, you can use the Url Scheme Method of the host App:

[Self. extensionContext openURL: [NSURL URLWithString: @ "xxxx: // xxxx"] completionHandler: nil];

 

The above is a record of precautions during the development process.

Complete development tutorial, recommended reference: http://www.cocoachina.com/ios/20141023/10027.html

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.