APP Extensions Share Extension

Source: Internet
Author: User
Tags notification center

Reprint Please specify source: http://www.cnblogs.com/zhanggui/p/7119572.html

1. Preface

Here is mainly to the app Extension some introduction and detailed to introduce you share Extension, later will add other Extension introduction.

2. Start

The app extension and share extension are mainly introduced. Please keep looking down:

introduction of 2.1:app extension

The official argument is that app extension allows you to expand your app's custom features and content so that users can use it when interacting with other apps or systems. Translation is not necessarily accurate, so it may be good to understand: we usually see the widget, and QQ share and so on, are app Extension, are some examples:

In fact, we often see the widget, but the widget is today Extension, in addition to today Extension, there are many.

A system area that supports extensions is called a extension point (extension). Extensions for each extension point have their own unique usage methods and APIs. You can choose different extensions according to your needs. The official API contains a noun called: the host app, which we can interpret as hosting apps that provide apps that extend the display or functionality of the app. There is also a container app that we can understand as a container app, like the share extension, the container app is.

Extensions are different from apps, and extensions can't be listed separately AppStore. 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 delivery and distribution.

You can create extension with the file--->new--->target, which, like any other target, combines with your app project as a product. An app can have an extension, or it can have multiple extensions. The best way to create an extension is to choose what you need by using the extension kind provided by Xcode, which contains the necessary APIs and method implementations.

If you want to allow users to use your extension, then you need to publish your containing app to AppStore, and when the user installs your containing app, the extension installs. Different extensions start differently, such as today Extension, which you need widgets to show to your notification center. Extensions are also not to be used, and the best user experience for scaling is always to make user operations leaner, faster, and focused on a single task.

Types of 2.1.1:extension

We can see the extension of different platforms in Xcode's file--->new--->target, including iOS, WatchOS, TvOS, MacOS, and more. The main introduction of iOS, mainly includes the following several extensions:

1.Action Extension: Action extension, manipulating or viewing content in the context of another application

2.Audio Unit Extension: Audio Unit expansion

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 blocker extension

7.Custom Keyboard Extension: Keyboard extension, such as the third-party keyboard, Sogou input method, Baidu input method and so on.

8.iMessage Extension: Extension of messages

9.Intents extension:intents Extension

10.Intents UI extension:intents UI Extension

11.Notification content Extension: Notification contents extension

12.Notification Service Extension: Notification Service Extension

13.Photo Editing Extension: Image editing extensions, editing photos or videos in the Photos app

14.Share Extension: Share extensions, publish a shared site, or share content with other apps.

15.Shared links Extension: Sharing link Extensions

16.Spotlight index extension:spotlight indexing extension

17.Sticker Pack Extension: Sticker Pack Extension

The 18.Today extension:today extension lets you quickly get updates or perform a quick task in the recent view of notification hubs.

Wait a minute. You can also see more extension directly here.

The life cycle of 2.1.2:app extensions

First, it is estimated that you have seen this picture many times, congratulations you see again this time, because this is Apple's official picture.

1. User selects the app to use extension

2. System Launch App Extension

3.APP Extension Code Run

4. After running the system kill the app Extension

This is the life cycle of app extension, for example:

A share Extension, in the gallery you select a picture, and then click Share, select your share Extension (first step), the system will start your share Extension (second step). Then you share the selected picture to the specified program (for example, send to a friend) (step three). Next the Sharing page closes, the system kill share Extension.

2.1.3:app extension means of communication

App extension main communication is with his host app (for example, share extension and), requests from the host app and extension response. You should also be familiar with (app extensions directly communicate with the host app):

This shows the relationship between the running app Extension, the host app, and the containing app. As you can see: The containing app and app extension don't communicate directly. Even sometimes the containing app does not run, and app extension runs directly. There is no communication between the containing app and the host app.

In a typical request/response, the system opens the extension (Shared share extension) that represents the host app (Photo gallery), and the data provided by the host app (images and selected friends) Transfer to the extension context and then extension the display interface to provide some functional tasks (such as sharing to friends).

Another is that app extension can communicate directly with his containing app:

For example, the Today Widget can tell the system to open his containing app, just call Nsextensioncontext's Openurl:completionhandler: method.

2.1.4: Things you can't do in app extension

An app extension cannot have the following conditions:

1. Access the Sharedapplication object. So you can't use any of that object's defenses

2. Use any API labeled Ns_extension_unavailable macros, or similar macros, or not available within the framework of the API, such as the HealthKit framework cannot be used for apps extensions

3.iOS device access to the camera or microphone (IMessage app can access these resources, as long as the configuration using the Info.plist inside the description can be)

4. Run a long background task (varies by platform)

5. Using airdrop to receive data

Simple use of 2.2:share extension

Here we take share extension as an example to introduce.

2.2.1: Choose the right extension point to start developing

When you create an app extension, you can create the extension you need directly using the template that comes with Xcode. Click File--->new--->target:

Choose the extension that fits your needs, and when you're done, your project catalog will have one more folder:

You can find one more. Swift,. Storyboard, and a info.plist. Next you'll find a extension in scheme, and a bundle with a. Appex suffix

Here's what to note:

An app extension must contain arm64 (iOS) or x86_64 (OS X) in the architectures build settings, or it will be rejected when the containing app is on the shelves. The default standard architecture for Xcode contains the architecture of 64-bit.

inch  - New  -bit Architecture or it'll be rejected by the App Store.
2.2.2: Take a look at the default app extension template

From the project catalog 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 what's inside Info.plist:

Take a look at the Info.plist of Project engineering:

A comparison can be made between the two to see:

1, Cfbundlepackagetype not the same, the project is appl, and extension is xpc!

2, the more obvious is the app extension inside more a nsextension dictionary.

In Info.plist, the file must contain a dictionary of keys and values specified by the Nsextension key and extension point. The extensionpointidentifier here is com.apple.share-services, because I created the share Extension.

Note here that if your app extension info.plist contains Uibackgroundmodes key, you won't be able to pass the AppStore audit.

2.2.3: Debug App Extension

Debugging the app extension is simple, all you have to do is choose the scheme extension, then click Run and a popup will pop up to let you choose the Host app, and then you can run the debug after selecting the Host app. For example, if you debug share Extension, you can select a photo, then let the photo be the host app, then open the photo after running, select Share to see your app extension, then debug breakpoint processing and so on.

2.3:share Extension Demo

Let's take a look at what I've done. Share Demo effects:

Then view the shared image in the containing app: the third picture. First look at the share extension view controller created here by default:

classShareviewcontroller:slcomposeserviceviewcontroller {OverrideFunc Iscontentvalid ()Bool {//Do validation of contenttext and/or Nsextensioncontext attachments here        return true    }    Overridefunc didselectpost () {//This was called after the user selects Post.            Do the upload of ContentText and/or nsextensioncontext attachments. //Inform The host that we ' re do, so it un-blocks it UI. Note:alternatively could call super ' s-didselectpost, which'll similarly complete the extension context.self.extensioncontext!. CompleteRequest (returningitems: [], Completionhandler:nil)}OverrideFunc configurationitems (), [any]! {        //to add configuration options via table cells in the bottom of the sheet, return an array of Slcomposesheetconfigurati Onitem here.        return []    }}

There are three main methods:

Iscontentvalid is used to determine whether the content is available, there are some checks, such as whether the content we share to meet the requirements to share, if return false, then the Post button will not be clicked. Because once returned false, the share content does not meet the requirements, it will not post.

Configuration is an array of configurations that can configure multiple lists, such as shared [send to friends, share to friends, favorites]:

Didselectpost is the event you click to post, such as the Click Collection, the API that can be called, and then the collection. The default annotations also illustrate the purpose of this method:

Called when the user selects post. is the upload of content or nsextensioncontext attachments. I use app group to interact with app extension and containing apps. Save the content to Userdefaults and then take out the images in the containing app and show them to the containing app.

Here I store the images in Userdefaults, and then get them in the containing app:

The name of the suite is the names of the app group. See shareextension in the GitHub source for details.  

Summary

The advent of app extension makes the app more convenient to use, such as the system's weather widget, and similar (QQ) sharing, can be implemented without opening the containing app and directly using share extension share.

In the following time, will be updated irregularly, to the reader to introduce other extension, if there is any doubt, feel free to message communication.

Resources

1. APP Extension Programming Guide

2. In-depth app Extensions for iOS8

3, Information property List Key Reference

4. APP Extension Programming Guide---Share

APP Extensions Share Extension

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.