Share Extension and appextensionshare in App Extensions

Source: Internet
Author: User
Tags introductions notification center

Share Extension and appextensionshare in App Extensions

Reprinted please indicate the source: http://www.cnblogs.com/zhanggui/p/7119572.html

1. Preface

Here are some introductions to App Extension and details about Share Extension. Other Extension introductions will be added later.

2. Start

This section mainly introduces App Extension and Share Extension. Continue to the following:

2.1: Introduction to App Extension

The official saying is: App Extension allows you to expand the custom functions and content of your APP so that users can use it when interacting with other apps or systems. The translation is not necessarily accurate. In this case, it may be easy to understand: the widgets we usually see and the QQ share are all App Extension. Some examples are as follows:

In fact, it is the widgets we often see, but the widgets are only Today Extension, in addition to Today Extension, there are many more.

An extended system region is called an extension point ). Each extension point has its own unique usage and API. You can choose different extensions based on your needs. The official API puts forward the term "Host app". We can understand it as a Host App, that is, an App that provides the display or function of the application extension interface. There is also a container app, which can be understood as a container App, just like share extension, which is the container app.

Unlike apps, Extensions cannot be added to the AppStore separately. Although you must use an app to include and distribute your extension, extension is also a separate binary file, independent of the container app for transfer and distribution.

You can use File ---> New ---> Target to create an Extension. Like other targets, it is combined with your app project to form a product. An app can have one or more extensions. The best way to create an Extension is to create an Extension by selecting the Extension type provided by Xcode, which includes the necessary APIs and method implementation.

If you want users to use your extensions, You need to publish your ining app to the AppStore. When the user installs your containing app, the extensions will be installed. Different extended startup methods are different, such as Today Extension. You need widgets to display them to your notification center. Do not use extensions indiscriminately. The best user experience of extensions has always been to make user operations more streamlined and faster, and focus on a single task.

 

2.1.1: Extension Type

We can see the Extension of different platforms in Xcode File ---> New ---> Target, including iOS, watchOS, tvOS, and macOS. Here we mainly introduce iOS, including the following Extensions:

1. Action Extension: Action Extension. You can operate or view the content in the context of another application.

2. Audio Unit Extension: Audio Unit Extension

3. Broadcast UI Extension: Broadcast UI Extension

4. Broadcast Upload Extension: Broadcast Upload Extension

5. Call Directory Extension: Call Directory Extension

6. Content Blocker Extension: Content interceptor Extension

7. Custom Keyboard Extension: Keyboard extensions, such as third-party keyboards, sogou input methods, and Baidu input methods.

8. iMessage Extension: Message Extension

9. Intents Extension: Intents Extension

10. Intents UI Extension: Intents UI Extension

11. Notification Content Extension: Notification Content Extension

12. Notification Service Extension: Notification Service Extension

13. Photo Editing Extension: image Editing Extension. edit a Photo or video in the Photo app.

14. Share Extension: Share an Extension, publish a shared website or Share content with other applications.

15. Shared Links Extension: Shared Link Extension

16. Spotlight Index Extension: Spotlight Index Extension

17. Sticker Pack Extension: Sticker package Extension

18. Today Extension: The Today Extension can be used to quickly obtain updates or execute a quick task in the recent view of the notification center.

And so on. You can also see more extension here.

2.1.2: lifecycle of App Extensions

First, you may have seen this image many times. Congratulations, you have seen it again this time because it is an official image provided by Apple.

 

 

1. Select the App extension to use

2. the system starts App Extension.

3. Run the App Extension code

4. After running, the system kill the App Extension.

This is the lifecycle of App Extension. For example:

Share Extension: select an image from the image library, click Share, and select Share Extension (step 1). The system starts your Share Extension (step 2 ). Then share the selected image to the specified program (for example, send it to a friend) (Step 3 ). The sharing page is closed, and the system kill Share Extension.

2.1.3: Communication Method of App Extension

The main communication between App Extension and its host app (such as Share Extension and) is the request from the host app and the response of extension. You should also be familiar with (the app extension communicates directly with the host app ):

 

 

 

This shows the relationship between the running App Extension, host app, and containing app. We can see that there is no direct communication between the Containing App and app Extension. Sometimes, the Containing app may not run, but the App Extension will run directly. There is no communication between the Containing app and the Host app.

In a typical request/response, the system opens the extension (share extension) representing the host app (Image Library), and transfers the data (images and selected friends) provided by the host app) it is delivered to the extension context, and then the extension display interface provides some functional tasks (such as sharing with friends ).

Another way is that app extension can communicate with its containing app directly:

For example, the Today Widget can directly tell the system to open its Containing app. You only need to call the NSExtensionContext openURL: CompletionHandler: method.

2.1.4: what cannot be done in App Extension

An app extension cannot meet the following conditions:

1. Access the sharedApplication object. Therefore, this object cannot be used for defense.

2. Use any API that marks the NS_EXTENSION_UNAVAILABLE macro, similar macros, or APIs in the framework. For example, HealthKit framework cannot be used in app extensions.

3. iOS devices can access cameras or microphones (the iMessage app can access these resources, as long as you configure the usage description in Info. plist)

4. Run a long background task (depending on different platforms)

5. Use AirDrop to receive data

2.2: simple use of Share Extension

Here we will introduce Share Extension as an example.

2.2.1: select the correct Extension Point to start development

When creating app extension, you can directly use the template that comes with Xcode to create the Extension you need. Click File ---> New ---> Target:

Select the Extension that meets your needs from here. After you have created the Extension, your project directory will have one more Folder:

One more. swift,. storyboard, and Info. plist can be found. Next, you will also find an Extension in Scheme and add a Bundle with the suffix. appex.

Note:

An app extension must contain arm64 (ios) or x86_64 (OS X) in ubuntures build settings. Otherwise, the containing app will be rejected when it is installed. Xcode's default Standard architecture includes a 64-bit architecture.

An app extension target must include the arm64 (iOS) or x86_64 architecture (OS X) in its Architectures build settings or it will be rejected by the App Store. Xcode includes the appropriate 64-bit architecture with its “Standard architectures” setting when you create a new app extension target.If your containing app target links to an embedded framework, the app must also include 64-bit architecture or it will be rejected by the App Store.
2.2.2: Check the default App Extension template.

From the project directory above, each extension contains a plist file, A View Controller class, And a default user interface, which are defined by the extension point. Let's take a look at the items in Info. plist:

Let's take a look at Info. plist of the project:

The two can be compared and we can see that:

1. CFBundlePackageType is different. The project is APPL, And the Extension is XPC!

2. Obviously, the App Extension contains an additional NSExtension dictionary.

In Info. plist, the file must contain a dictionary of keys and values specified by the NSExtension key and extension point. Here, ExtensionPointIdentifier is com. apple. share-services, because I created Share Extension.

Note that if your app extension's Info. plist contains the UIBackgroundModes key, it will not pass the AppStore review.

 

2.2.3: Debug App Extension

Debugging App Extension is very simple. All you need to do is select (scheme) Extension, and then click Run. A pop-up box will pop up asking you to select the Host app. After selecting the Host app, you can Run debugging. For example, if you debug Share Extension, you can select a photo, set the photo as the Host app, and then open the photo after running it. If you select Share, you will see your app Extension, debug breakpoint processing.

2.3: Share Extension Demo

Let's take a look at my own Demo:
 

View the shared image in the containing app, such as the Third image. Let's take a look at the default Share Extension View Controller created here:

 

class ShareViewController: SLComposeServiceViewController {    override func isContentValid() -> Bool {        // Do validation of contentText and/or NSExtensionContext attachments here        return true    }    override func didSelectPost() {        // This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.            // Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context.        self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)    }    override func configurationItems() -> [Any]! {        // To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.        return []    }}

 

There are three main methods:

IsContentValid is used to determine whether the content is available. Here we can perform some verification, for example, whether the content we share meets the requirements for sharing. If false is returned, the Post button cannot be clicked. If false is returned, the shared content does not meet the requirements and cannot be Post.

Configuration is a configuration array that can be configured with multiple lists, such as [send to a friend, share to a friend, and add to favorites]:

 

DidSelectPost is an event that is processed after you click "send". For example, you can click "add to Favorites" to call the api and add it to favorites. The default comment also describes the role of this method:

It is called after you select post. Is the upload of content or NSExtensionContext attachments. Here I use the App Group method for app Extension and containing app interaction. Store the content in UserDefaults, and then take the image from the containing app and display it in the containing app.

Here, I store the image in UserDefaults and obtain it in the Containing app:

The name of suite is the name of app group. For details, see extension in Github source code.

Summary

The emergence of App Extension makes the App more convenient to use, such as the system weather widget and sharing similar to (QQ), so that you can directly use share extension sharing without opening the containing app.

In the future, the system will update the Extension from time to provide readers with other Extension information. If you have any questions, leave a message at any time.

References

1. App Extension Programming Guide

2. go deep into App Extensions for iOS8

3. Information property List Key Reference

4. App Extension Programming Guide --- Share

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.