Transfer from Pinterest: http://www.jianshu.com/p/8cf08db29356
IOS 10 has introduced a number of new features, including several high-profile changes: The notification bar is more practical, the phone is anti-harassment, the imessage becomes more interesting and powerful, and a new round of Siri flirting. These important features let us look forward to iOS10 officially on-line! As a developer, we also need to constantly recharge their batteries, want to seize the opportunity? Let's first look at their basics-App Extension
Introduced
App Extensions (app Extension) are officially signed in to the iOS platform from iOS 8, and developers can provide system-specific extensions to users through app extensions. Developers use extension points (Extension point) to specify specific system functions such as today's extension, notification bar extension, messages extension, phone book extension, and so on.
Create an app extension
App extensions must be attached to a host app(iOS10 updates, see below), in existing projects, select File->new->target->application extension to create an app extension
Create Extension.png
Developers can create different application extensions based on their business needs, or they can create multiple app extensions in one app.
IOS10 start, the messages extension can be created independently of the host app:
Create Messages Application.png
Messages application will automatically create a sticker pack and Messages Extension.
Refer to IMessage Apps and stickers, Part 1 WWDC 2016 for the 7 minute introduction.
Xcode automatically generates some files for us, in two categories:
- With user interface.
Example: Today Extension
Today Extension.png
Contains a viewcontroller, a UI, and a plist.
The same type of message Extension (requires Xcode8), Share Extension, Action Extension, and so on.
2. Does not include user interaction interface
such as: Share Link Extension
Share Link Extension.png
Contains a handler and a plist.
The same type of call Directory Extension (requires Xcode8) and so on.
These two types of plist are roughly the same, except for the Nsextension property.
Application extensions with user interfaces are defined NSExtensionMainStoryboard , typically maininterface, and application extensions that do not include the user interface are defined NSExtensionPrincipalClass , typically xxxhandler (in the Share Link The extension example is RequestHandler).
Life cycle
Understanding the lifecycle of application extensions begins with two concepts:
1. Container app (container app)
2. Hosting app (host app)
The container app is the app that contains the app extension, the app that the app extension relies on;
The host app is an app that provides an app with an extended interface display or functionality.
Secondly:
Although the Container app contains app extensions, it has a completely different lifecycle from app extensions, and app extensions have different processes from the container app.
Apply Extended life cycle
1. The user selects the app extension in the Host app (view its UI or click to start its function). The following will explain
2. The system launches the application extension. During this time, the host app invokes the relevant method to launch the app extension, and the app extension can be extensioncontext to the interactive information.
3. Apply extensions to execute business logic. Some app extensions don't need to interact with the host app at this stage, such as share Link Extension, and some app extensions need to keep in touch with the host app to listen for user actions or accept commands, such as imessage Extension.
4. The application extension execution is complete and the system terminates it.
There is even no need to interact with the container app during the lifetime of the app extension. In fact, the container app does not need to be in a running state when the app extension runs (unless it is actively invoked).
Apply extended Response Host app request
We mentioned two types of application extensions above, and different types of application extensions are different in how they respond to requests.
App extensions with user interface are launched when displayed in the host app, and the UI is loaded. We can get the information that the loadView self.extensionContext host app has passed to the app extension (nsextensioncontext type) in the Viewcontroller method.
Application extensions without user interaction interface NSExtensionPrincipalClass directly accept requests from the host app in handler (specified handler), calling methods:
- (void)beginRequestWithExtensionContext:(NSExtensionContext *)context;
When the app extends the response host app and requests the business logic, you need to proactively inform the host app. NSExtensionContextThese two methods are called by:
- (void)completeRequestReturningItems:(nullable NSArray *)items completionHandler:(void(^ __nullable)(BOOL expired))completionHandler;
- (void)cancelRequestWithError:(NSError *)error;
After the application extension executes completeRequestReturningItems: , the system can terminate it at any time, if it is an application extension with user interaction interface, after executing this method, Viewcontroller will immediately dismiss (the application extension with user interface is usually presentViewController displayed by the way).
IOS App Extension Getting Started