in the latest WWDC14 above, Apple has released iOS8 new features, and the most exciting feature of the programmer Extension , or call it Widgets .
Here's a taste test.
First, Extension Introduction
first, Apple supports only the following types of Extension Point , and does not support third-party app settings Extension Point .
Extension Point |
Example extension that helps users: |
Today (IOS and OS X) |
Get a quick update or perform a quick task in the Today view of Notification Center (a Today extension is called A widget) |
Share (IOS and OS X) |
Post to a sharing website or share content with others |
Action (IOS and OS X) |
Manipulate or view content within the context of the another app |
Photo Editing (IOS) |
Edit a photo or video within the Photos app |
Finder (OS X) |
Use of a remote file storage service in OS X |
Storage Provider (IOS) |
Choose a document from among the set of documents the current IOS app can access |
Custom keyboard (IOS) |
Replace the IOS system keyboard with a custom keyboard for use on all apps |
today, we focus only on Today of the Extension . Notification Center was launched at the time of iOS5 , at the time of launch, the developer is wondering whether it can be customized, can add some application related information, three years later ,iOS8 's launch finally completed this mission.
Extension and the App not the same, he is equivalent to a light weight App . In each program's installation package you can bring multiple Extension, each Extension is a target .
Extension The life cycle is relatively short, basically as follows:
for a Noficationcenter Extension , when the user pulls down Noficationcenter When the user shuts down and starts running Noficationcenter will end, so you must ensure that every Extension must be lightweight and fast. So after you have completed your update, the system will use the previous exit to do the display, the logic and the App is the same.
and Extension and the App can only be communicated through the OpenURL and the Shared Resources the way to communicate because often in the run Extension the time App may not be running, so you can only interact with files from a shared resource pool, such as:
second simple. DEMO
Now let's write a simple Extension:
The first step is to create a new Target , and then select Extension , and then select Today:
we can see that basically a Extension is a Viewcontroller , so Viewcontroller in the Viewwillappear The callback is also in effect here and can be used as a Viewcontroller to handle .
Let's run a bit and Notificationcenter Add our Extension you can see that the system has created Hello World the content of:
If you need to customize your Viewcontroller the height of the word, you can use AutoLayout or call Viewcontroller of the preferredcontentsize to set the size you need.
Step two, get the content
is just such a Extension and can't do anything, so we need some data to show, and the content of the data can be Extension access to the network itself, or through App here, let's talk about it . App get what to do.
first of all, to communicate the App and the Extension must be in the same App Group inside, in Xcode the project configuration inside the Capabilities inside found App Group This entry, open, and login through the Development Account to generate a App Group , and will App and the Extension all join the same App Group .
then, create a local plain text that needs to be played on the Extension And then add the following code at the start of the Groupidentifier is created App Group identifier.
- (BOOL) Application: (uiapplication*) Application Didfinishlaunchingwithoptions: (nsdictionary*) Launchoptions {//Override point for customization after application launch. Nsurl* FileURL = [[NsfilemanagerDefaultmanager] containerurlforsecurityapplicationgroupidentifier:@"Group.notificationcenter.extension.com"] ;Nsurl* sourceURL = [NsurlFileurlwithpath: [[NSBundleMainbundle] pathforresource:@"Helloextension"oftype:@"TXT"] ] ;Nsurl* TargetUrl = [Nsurlurlwithstring:[[FileURL absolutestring] Stringbyappendingstring: @"Helloextension.txt"] ] ; [[NsfilemanagerDefaultmanager] Moveitematurl:sourceurl Tourl:targeturl Error:Nil] ;return YES;}
and then in Extension Add the following code:
-(void ) viewdidload {[super viewdidload]; //do any additional setup after loading the view from its nib. nsurl * FileURL = [[nsfilemanager Defaultmanager] Containerurlforsecurityapplicationgroupidentifier:@ " Group.notificationcenter.extension.com "]; nsurl * targeturl = [nsurl urlwithstring:[nsstring stringwithformat:@, [FileURL Absolutestring]]; nsstring * nsstring = [nsstring Stringwithcontentsofurl : TargetUrl encoding:nsutf8stringencoding error:nil ]; _label.text = nsstring; [_label SizeToFit];}
The following results can be achieved by running:
iii. Summary
Of course, this DEMO Simply describes how to complete a Extension, in the actual process need to face how to reuse Framework, how to handle multiple processes simultaneously read and write the same file, and some security authentication issues, here is just a simple test.
Resources
1.APP Extension Programming Guide