APP Today Extension Development considerations

Source: Internet
Author: User
Tags hosting

From iOS 8, there's app Extension. The variety of extension has also been expanded to 19 kinds, the application is also very extensive, deserves the key attention.

Extension can almost be seen as an embedded standalone app with independent Bundleid, certificates, profile profiles, process space, sandbox, and more. It just needs to be packaged in the app, similar to the parasite in the host app, bundled and installed. However, once the application is installed, the extension can be called independently by the system, executing the code logic within the extension.

This article only records the issues that are noteworthy in the process of developing today extension. In fact, most of them are universal.

1. Creating extension and certificate management

In the created project, create a new File->new->target->today Extension, and after you determine the name, you can see the new Extension in the targets list of the project file.

As you can see in the General column, extension has independent identity content, and bundle identifier generally has the bundle identifier prefix of the host application. If you use Xcode8 's automatic management certificate and login to the developer account, you can see the automatically created AppID in the developer Center.

Then create provisioning files for the AppID.

2.maininterface.storyboard and the main class

The newly created extension and the app's main target are isolated from each other in separate file directories. The main interface, Info.plist, and localization files are all managed separately, so you can create and modify Cfbundledisplayname fields in the infoplist.strings localization file, name the extensions separately, and if you don't modify them, The display name of the host app will be used by default.

Extensions and hosting apps are isolated, cannot use each other's classes, frameworks, resources, and if you want to use the host's class library or resource files, the most straightforward way is to copy to the current target or add a frame that needs link. However, view extension's build Settings,prefix header to fill in the Prefix header of the host app and use the macro definition in its reference header file.

The extension uses Maininterface.storyboard by default, binds a class Todayviewcontroller, and has created a Hello world view that we can use and modify directly. But if you don't want to use storyboard or need to modify the startup class, You can remove the Nsextensionmaininterface entry for the Nsextension dictionary in the Info.plist file, increase the Nsextensionprincipalclass entry, and value the class name for the specified startup class, for example Todayviewcontrol Ler

3. Life cycle

The extended view controller, like the host view controller, has a common life cycle approach. For example, when the application extension is displayed for the first time in the "Today" list of a drop-down, the Viewdidload method is executed, followed by another method, disappears and reappears, and the disappear and appear series methods are called, and the Dealloc method is called when it is about to be destroyed.

Size of 4.Today extension

After the iOS10, the size of today extension was re-defined. The width is fixed (for example, 359 on IPhone6), so it cannot be changed, but in terms of height, two modes are available:

Ncwidgetdisplaymodecompact: fixed height, 110

Ncwidgetdisplaymodeexpanded: Can vary the height, the interval is 110~616

The pattern can be modified using the following code:

 if ([[uidevice currentdevice] . systemversion floatvalue >= 10) {        

    Self. Extensioncontext. Widgetlargestavailabledisplaymode = ncwidgetdisplaymodeexpanded;

}

we can be in of the Ncwidgetproviding protocol in the proxy method below, take the maximum range of size:

-(void) Widgetactivedisplaymodedidchange: (ncwidgetdisplaymode) Activedisplaymode withmaximumsize :(cgsize) maxSize ns_available_ios(_0);

Use the following method to determine the margin:

-(uiedgeinsets) widgetmargininsetsforproposedmargininsets: (uiedgeinsets) defaultmargininsets Ns_deprecated_ios(8_0, _0, "This method is not being called on widgets linked against IOS Versi ONS 10.0 and later. " );

If you need to modify the background height of the widget display, you can modify the properties: Self.preferredcontentsize  

5. Data sharing

Since the extension is isolated from the hosting app, data sharing requires the use of app groups.

In the app main target's capabilities bar, find the app groups item, turn on the function, and click the "+" symbol to add a shared data container name, such as Group.xxx. You will then find a entitlements type file in both the primary target and the extended target directory, recording an app groups item.

This shared container is the space to store the data that is shared between the extension and the host app.

In order to compile properly, you also need to go to the developer Center, edit the main app and the extended AppID, turn on the Support app groups feature, similar to turn on push function.

After the configuration is complete, it is used. Regardless of which data is stored or manipulated using Userdefaults, Archive, CoreData, FMDB, and Leveldb, you simply point the path to the shared container path.

For example, use the Userdefaults method:

Nsuserdefaults *defaults = [[Nsuserdefaults alloc] Initwithsuitename:@ "group.xxx" ];    [Defaults setobject: @" xxxxx " forkey:@"phonenum"];

Just opens the specified Userdefaults object, using an instance method of Initwithsuitename.

The other way.

6. Class libraries and resource sharing

As mentioned earlier, the extension needs to use the class library and resources of the host target, which can be copied directly to the extension target directory, but this will increase the application volume.

Another way is to create a framework dynamic link library. New File->new->target->framework, get a new Target, similar to the framework of the system, we can freely add the class file encapsulation function, only expose the public header file for invocation. In the headers item in the build phases bar, drag the project group's header file to the public group.

The framework also has bundle IDs, but does not require attention and does not require a certificate to be configured.

It is important to note that the newly created dynamic link library typically automatically creates a header file with the same name, directly referencing the custom class header file that needs to be exposed. However, if you delete the header file, you may have the following warning:

Warning:no umbrella header found for target ' orderfoodtodaykit ', module map is not being generated

Another area that requires special attention, because the dynamic link libraries we create need to be available to the extension, and the extensions do not support part of the API, so the framework needs to remove some of the APIs that are not supported by the extension. In the deployment info of the General section of the framework, tick the Allow app extension API only. In this case, if the API is used in the framework that is not supported by the extension, the compilation will error.

7.Today Extension Host App

If you need to tune up the hosting 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 the development process, the need to pay attention to the matters of record.

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

APP Today Extension Development considerations

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.