iOS Development App Extension Overall Summary

Source: Internet
Author: User

First, the introduction of APP extension

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 . app Extension is the extension that this article describes. Extension is not a standalone app, it has a separate bundle,extension bundle suffix that is included in the app bundle. Appex. The life cycle is different from the normal app, which will be described in detail later. Extension cannot exist alone, there must be a containing app that contains it. Extension (App Extension) is a very important new feature introduced in IOS 8

Extension activation mode: Different extension activation methods are different, some extension need usersmanual activation and some can be activated in any application, for example: The widget in today needs to be activated and closed in today, Custom keyboard needs to be set in settings, and photo editing needs to be activated or closed in Photo Manager when working with photos Storage Provider can appear when a file is selected, share and action can be activated in any application, but only if the developer needs to set activation Rules to determine if extension needs to appear properly.

We usually see the widget, and QQ share and so on, are app Extension, are some examples:

 

several key words extension PointThe areas that support extension in the system are called Extension point (Extension points), and the categories of extension are differentiated by this, with the common today, Share, Action, Photo Editing, Storage on iOS Provider, Custom keyboard several, in which today's extension is called the widget. Each extension point is not used in the same way as it does for dry work, so there is no universal extension. containing app containing appWe can understand it as a container app, like the share extension, the container app. Although Apple has opened the extension, but in iOS extension does not exist alone, in order to submit to AppStore, extension must be included in an app to submit, and the app's implementation part can not be empty, This app, which contains extension, is called the containing app. Extension will be installed with the containing app and uninstalled as the containing app is uninstalled. Host AppWe can think of it as the host app, the app that can be extension is called the host app, for example: The Safari app inside the website to share, Safari is the host app; The widget's host app is today.

ii. Types of 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:

You can also refer to the official documentation directly

The IOS 8 system has 6 system areas that support extensions , namely Today, Share, Action, Photo Editing, Storage Provider, Custom keyboard. System regions that support extensions are also known as extension points.

Today Widget

Today extensions can quickly get updates or perform a quick task in the recent view of notification hubs. For event scores, stocks, weather, express, and so on, you can create a today extension implementation in the Today view of the notification hub. Today's extensions are also known as widgets.

Today extension

Share Extension

Share extensions, publish a shared site, or share content with other apps, before IOS 8, users can choose from a limited number of sharing options, such as Facebook,twitter. In IOS 8, developers can create custom sharing options.

Share extension

Action Extension

Action extensions, which operate in the context of another application or view content, are the strongest in all supported extension points. It enables the conversion of content in another app context. Apple demonstrated a Bing translation action extension at the WWDC conference, which translates the text selected in Safari into different languages.

Action extension note?? : Host App(Photos, Safari, mail, voice, etc.) the first line of the Share menu is: Share Extension;The second line is: Action Extension;

Photo Editing

Image editing extensions, editing photos or videos in the Photos app, before IOS 8, if you want to add a special filter to your photos, you need to go into a third-party app, which is quite tedious. In IOS 8, you can use third-party apps directly in Photos, such as Instagram, VSCO Cam, and Aviary for photo Editing extensions to complete your edits without leaving your current app.

Photo editing extension

Document Provider

Document Provider makes managing across multiple file storage services easier. Storage providers like Dropbox and Google drive can use these extensions to retrieve and store files without having to create unnecessary copies by providing a document Provider extension in IOS 8.

Document provider Extension

Custom Keyboard

Keyboard expansion, such as the third-party keyboard, Sogou input method, Baidu input method and so on. Apple pioneered the touch-screen keyboard in 2007, but it hasn't improved much. In this regard, Android will open the keyboard permissions to third-party developers, so there are many like Swype, SwiftKey and other excellent keyboard input method. In IOS 8, Apple has finally developed keyboard access to third-party developers, and the custom keyboard input method allows users to use it throughout the system.

Custom Keyboard Extension

Following are the new extensions in iOS 9 and later

1.Audio Unit Extension: Audio Unit expansion

2.Broadcast UI Extension: Broadcast UI Extension

3.Broadcast Upload Extension: Broadcast upload extension

4.Call directory Extension: Call directory Extension

5.Content blocker Extension: content blocker extension

6.iMessage Extension: Extension of messages

7.Intents extension:intents Extension

8.Intents UI extension:intents UI Extension

9.Notification content Extension: Notification contents extension

10.Notification Service Extension: Notification Service Extension

11.Shared links Extension: Sharing link Extensions

12.Spotlight index extension:spotlight indexing extension

13.Sticker Pack Extension: Sticker Pack Extension

Third, the life cycle of App extensions

Here is an official picture from Apple:

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.

Four, the App extension communication way

App extension main communication is with his host app (for example, share extension and)

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.

The app extension and containing apps can read and write a storage area called shared resources, which is implemented through the app groups.

It is important to note that in iOS 8.0, only today extension supports direct calls to Nsextensioncontext 's openurl:completionhandler: Open the URL link; share/action extension to implement URL Scheme, you can only create a Sink uiwebvew loadrequest The URL to achieve the salvation (so-called "Sink "It means to be invisible, such as Frame=cgrectzero." Now this method is also not possible (see Apple is not recommended to open the main app) but if you are not afraid of Apple audit does not pass or the next rack can also be opened in the following ways:

//UIWebView *webview = [[UIWebView alloc]init];//Webview.hidden = YES;//nsurlrequest *request = [[Nsurlrequest alloc]initwithurl:[nsurl urlwithstring:@] Weixin://"]];//[WebView loadrequest:request];//[Self.view Addsubview:webview];//This method has not been opened.Uiresponder* Responder =Self ;  while((responder = [responder Nextresponder])! =Nil) {        if([Responder Respondstoselector: @selector (openURL:)] = =YES) {[Responder Performselector: @selector (openURL:) Withobject:[nsurl urlwithstring:[nsstring stringwithformat:@"weixin://"]]]; }    }

StackOverflow has answers about Share Extension to open containing app

V, App GroupsImplementing data sharing This is a new and open feature of IOS8, which is already available on OS X. It is primarily used by apps under the same group to share the same read-write space for data sharing. Extension and containing app read and write a piece of data is a very reasonable demand, such as the system of stock market applications, widgets and apps need to show a few of the company's stock data, which can be achieved through the app groups. function OpenFor easy follow-up, make sure your developer account is logged on to Xcode. (or create a good app group ID in the developer account first) first, open in the appAPP groups is located at:
    1. Targets-->appextensiondemo-->capabilities-->app Groups
Find later, the app groups in the upper right corner of the switch to open, and then choose to add groups, such as my is group.wangzz, of course, this is to test random name, the regular point naming rules should be: Group.com.company.app. After the add succeeds as shown: second, in the extension also to openI created the widget,target name called Todayextension, and the corresponding app groups is located at:
    1. Targets-->todayextension-->capabilities-->app Groups
In the same way as the app, it is important to ensure that the app groups name is the same as the app, which is Group.wangzz.

Third, extension and containing app data sharing

App groups provides us with areas where apps in the same group can read and write, and data sharing can be done in the following ways: 3.1 sharing data through Nsuserdefaults Save DataSave data to Nsuserdefaults in the following ways:
-(void) savetextbynsuserdefaults {     *shared = [[Nsuserdefaults alloc] Initwithsuitename:@ "  group.wangzz"];     [Shared Setobject:_textfield.text Forkey: @" Wangzz " ];     

Note that: 1. The group ID must be specified when the data is saved; 2. Also note that the data that nsuserdefaults can handle can only be plist objects, see Property List Programming Guide for details. 3. To prevent data synchronization problems, do not forget to call [shared Synchronize]; Read DataThe corresponding read data mode:
-(NSString *) readdatafromnsuserdefaults {     *shared = [[Nsuserdefaults alloc] Initwithsuitename:@ "  group.wangzz"];       *value = [Shared Valueforkey:@ "wangzz"];  return

3.2 sharing data through NsfilemanagerNsfilemanager provides a containerurlforsecurityapplicationgroupidentifier method in iOS7 that can be used to implement the app group sharing data. Save Data

-(BOOL) Savetextbynsfilemanager {nserror*err =Nil; Nsurl*containerurl = [[Nsfilemanager Defaultmanager] Containerurlforsecurityapplicationgroupidentifier:@"Group.wangzz"]; Containerurl= [Containerurl urlbyappendingpathcomponent:@"Library/caches/good"]; NSString*value =_textfield.text; BOOL result= [value Writetourl:containerurl atomically:yes encoding:nsutf8stringencoding error:&err]; if(!result) {NSLog (@"%@", err); } Else{NSLog (@"save value:%@ success.", value); }     returnresult;} 

Read Data
-(NSString *) readtextbynsfilemanager {     *err = nil;      *containerurl = [[Nsfilemanager Defaultmanager] Containerurlforsecurityapplicationgroupidentifier:@ "  Group.wangzz"];      = [Containerurl urlbyappendingpathcomponent:@ "library/caches/good"];       *value = [NSString stringwithcontentsofurl:containerurl encoding:nsutf8stringencoding error:&err];   return

Here I try to save and read the string data, but read and write SQLite I believe it is no problem. Data SynchronizationTwo applications that read the same data together can cause data synchronization problems. In WWDC2014 's video, it is recommended to use nsfilecoordination to achieve read and write synchronization of normal files, and the database can use Coredata,sqlite to support synchronization. iv. extension and containing app code sharingSimilar to data sharing, the extension and containing apps naturally have some code that can be shared by the business logic, which can be achieved through the framework that was just opened in IOS8. Apple is described in the app Extension Programming Guide as follows: In IOS 8.0 and later, you can use a embedded framework to share code between Y Our extension and its containing app. For example, if you develop image-processing code this you want both your Photo Editing extension and its containing app T o Share, you can put the code to a framework and embed it in both targets. Code sharing is being implemented in target for the framework to be embedded in the extension and containing apps, respectively. (meaning: You need to copy the framework separately into the main bundle of the extension and containing apps.)  In other words, the code is not shared, and the code to use the containing app is only in copy. As a reference for your own consideration: refer to extension and containing app data sharing, I wonder can I just save one copy of the framework in the app groups area? 4.1 Copy Framework to app GroupsPut the framework into the app groups area when the app is first launched:
-(BOOL) copyframeworkfrommainbundletoappgroup {Nsfilemanager*manager =[Nsfilemanager Defaultmanager]; Nserror*err =Nil; Nsurl*containerurl = [[Nsfilemanager Defaultmanager] Containerurlforsecurityapplicationgroupidentifier:@"Group.wangzz"]; NSString*sorpath = [NSString stringWithFormat:@"%@/dylib.framework", [[NSBundle Mainbundle] bundlepath]; NSString*despath = [NSString stringWithFormat:@"%@/library/caches/dylib.framework", Containerurl.path]; BOOL Removeresult= [Manager Removeitematpath:despath error:&err]; if(!Removeresult) {NSLog (@"%@", err); } Else{NSLog (@"remove success."); } BOOL Copyresult= [[Nsfilemanager Defaultmanager] Copyitematpath:sorpath topath:despath error:&err]; if(!Copyresult) {NSLog (@"%@", err); } Else{NSLog (@"copy success."); }     returnCopyresult;} 
4.2 Using the framework:
-(BOOL) loadframeworkinappgroup {nserror*err =Nil; Nsurl*containerurl = [[Nsfilemanager Defaultmanager] Containerurlforsecurityapplicationgroupidentifier:@"Group.wangzz"]; NSString*despath = [NSString stringWithFormat:@"%@/library/caches/dylib.framework", Containerurl.path]; NSBundle*bundle =[NSBundle Bundlewithpath:despath]; BOOL result= [Bundle loadandreturnerror:&err]; if(Result) {Class root= Nsclassfromstring (@" Person"); if(Root) { person*person =[[Root alloc] init]; if(person) {[Person run]; }         }     } Else{NSLog (@"%@", err); } returnresult;} 
Tested to be able to load successfully. It should be noted that this is only used to successfully load the framework, but there are still a lot of problems, such as if the user before launching the app to use extension, then the framework has not copy the past, how to deal with In addition, the mechanism of iOS or whether Apple's audit allows such use etc. Use it as a document before you know it. vi. 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

iOS Development App Extension Overall Summary

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.