Uncover IOS App Extension development--today

Source: Internet
Author: User
Tags notification center

Transferred from: http://www.cocoachina.com/ios/20160619/16760.html

This article authorizes reprint, Cyandev (Pinterest)

Starting with IOS 8, Apple has introduced a new App Extension that covers everything from today's panels, keyboards, content interceptors, sharing actions, and more. But the official development guide for APP Extension is few and far between, and there are a lot of holes to get started. So I am going to write a series of articles to help you get started with the App Extension development, but also less detours.

What is App Extension?

As the name implies, it is an extension, very similar to the plug-in mechanism of some large-scale software (OK, now may be an application can have). App Extension is actually not your application plug-in, but the system plug-in, its lifecycle is managed by the system, so if you want to do something bad still does not work ... But the app Extension distribution of the carrier is the application, that is, if you just want to make a today's panel plug-in, you also need to have a main program, your main program can do nothing, you can also provide some basic settings and data.

What is the relationship between APP Extension and the main program?

It can be said that there is no relationship, basically two separate programs, your main program can not access the APP Extension code, and can not access its storage space, this is completely two processes, two programs. At this point you may ask, I wipe, can not interact with what egg?? Don't worry, I'll talk about how to do some interaction later.

What can App Extension do? What can I do?

Basically everything is able to do, someone is not in today's panel to the Chrome dinosaur egg stuffed in it? Also take the input method when the browser for the split-screen multi-task ... Only you can't imagine, no you can't do ... Eh, wait, some of them can't be done. For example, there are limitations, and app Extension is far less usable than regular apps, so if you really want to play games, think about whether you can solve the problem of resource occupancy. And also can't access uiapplication, because its container application is the system, you take the system of uiapplication want to do ... (Of course, you can use recursion to find Uiresponder method to get uiapplication, but I did not try) again, you can not perform long operation, your App Extension may be killed by the system at any time, who knows?

There are many more unavailable APIs to see in this Apple Official document: Understand how an App Extension Works

Start creating an App Extension

First look at what we're going to do, it's a simple Todo application, the main program looks like this:

Today panel plugin long This kind:

The interface is very simple ~

The main program implementation is actually very simple, that is, the use of Table View and data persistence, here is not the emphasis. But notice that we're going to set aside an interface for today's panel, assuming here we're going to show the top 4 to-do items in today's panel, we have to separate the 4 items into one where both the main program and the extension can be accessed. I'll say how to do it later.

Tips:

Apple's HIG makes it clear that you should not use a scrollable Scroll View in today's panel, but rather to fully expand, so for multiple pieces of data, we can either page or just show the first few items.

Below, we'll create a Today Extension for the project:

The next step, enter a subproject name, click "Finish" to complete the Today Extension Add.

The initial directory structure for this subproject is as follows:

P.S. The entitiements file was created later and will not be in the first

Then we drag the approximate interface out of the Storyboard, if the canvas size is not appropriate can be adjusted here, but that is to adjust the preview effect, the actual size can not be modified in the IB.

So how do we change the size of the view in today's panel? The answer is to modify the View Controller's Preferredcontentsize property, regardless of width, adjust the height to the appropriate size, because the width is always the same as the screen width.

In this example, I use the number of 44 * items-one as the view height, because the height of a standard Table view Cell is 44, and then the height of the dividing line of the last entry is reduced to be our ideal height.

The main program shares data with APP Extension

We created a to-do item in the main program, how can we get the App Extension? Since both the code and data are not interoperable, we can take it for granted that the APP Group is the solution. First create an APP Group in the main program:

Then add the app Group to the app Extension.

This way, we can use Nsuserdefaults to communicate data through the APP Group.

Remember when I told you to put the first four of all the data in the panel today? Let's implement this feature:

This method is called when the data of the main application changes to update the snapshot data.

Let's take a look at Today's Extension How to do this, first looking at these two methods:

The first method is that the system tells Extension need to update, when you update the completion of the block callback to tell the system what you have done, and usually we tell the system we update the data can be (to block ncupdateresultnewdata Enumeration as an argument).

The second method is to return an inner complement size, which, if not implemented, will have some indentation on the left side of the view by default. Of course, Apple still wants you not to modify the default inner fill ~

Then we implement the reading of the data:

P.S. The third line is wrong.

It's also easy to take the first 4 snapshots from the APP Group configuration and update the Table View. This method can be called either in Viewdidload or Widgetperformupdatewithcompletionhandler:.

Here we look at the effect, select the Today Extension Scheme Click the Debug button, pop up the following dialog box:

Select our main program and click "Run".

APP Extension to tune the main program and perform actions

When our Todo List is empty, we want to display a button in today's panel, and click on it to quickly go to the Create Todo interface, like this:

Because we can not access the main program code, so there is only one way to choose, that is, URL Scheme.

First, we register a URL Scheme for the main program:

Then respond to the button click:

Because the App Extension can't access uiapplication, it can't use its openURL:, but we can use Extensioncontext to open the URL, the usage and the effect is the same.

Back to the main program, we handle the opening of the URL:

Here I use the Notification way to inform the designated View Controller to perform the corresponding action, of course, you can also use your own favorite way, here the most complex is the processing of routes, there are many ways to achieve, I do not delve into.

Look at the effect below (sorry, the picture is not good, do not move please open in a new window):

Well, to this we basically get through the main program and APP Extension communication between, is it also very simple?

Finally, a little reminder

Because the interface of the Notification Center is a large chunk of uivisualeffectview, and the specific parameters have been adjusted, so the background color of the plug-in is best to keep transparent, the main text color is preferably white, the color of the secondary text is best lighttextcolor, so that can adapt to the glass under the Vibrancy effect.

Today panel of each plug-in height calculation and UITableView adaptive height of the calculation method, if you do not set preferredcontentsize, or set it to Cgsizezero, it means that you want to use adaptive height, then the system will be based on your design Auto Layout to determine the appropriate height. If you want to do this, refer directly to UITableViewCell's adaptive height after IOS 8.

Uncover IOS App Extension Development--today 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.