iOS8 new feature widget development-B

Source: Internet
Author: User

Os8 has been released for some time, and with iOS8 there are a lot of new features, and the iOS system will become more and more open, which is a good thing. One of the new features is the addition of a personalized widget to the drop-down notification bar, where developers can define the widget's style content themselves. Of course, this feature has long been implemented on Android, ^_^ ..., such as:

First, a few concepts.

1, App Extension:extension is a new open iOS8 to several fixed system area extension mechanism, extension is not a standalone app, it has a separate bundle included in the app bundle, The extension bundle suffix name is. Appex. Of course it has a lot of types, such as the official documentation on the day, today we mainly talk about the present, also known as the widget.

2, containing app: Contains extension app is containning app, because extension can't exist alone, so must be attached to a normal app, so as to submit the store.

3, host app: The app that can be extension is called host app, such as Widget's host app is today.

Let's look at how the three of them work, such as:

Next we look at how to complete a simple demo, so you know better, ^_^

First we create a new project, this step is omitted, next we add a target, select today Extension, such as:

And then create it, such as:

The storyboard created by default is Hello World, so let's run the project first to see the effect:

is not very simple, ^_^ ...

Then let's look at the containing app and extension how to share data, such as my containing app data, I want to show in extension. Before that, let's start with the app groups, which is primarily used by apps under the same group to share the same read-write space for data sharing. We look at the project how to open the app groups, first select targets, and then select containing App,->capabilities, open the app groups switch, such as:

Click the plus button below to create a new group, for example: Group.com.company.app, such as:

Once created, such as:

Similarly, choose targets for extension, also create an app groups, choose the same name as the containing app, containing app groups and extension app Groups have the same name, such as:

Ok... Once the app groups is created, we're ready to share the data.

The first method is through Nsuserdefaults, the data code is as follows:

[OBJC]View PlainCopy 
    1. Nsuserdefaults *shared = [[Nsuserdefaults alloc] initwithsuitename:groupid];
    2. [Shared setobject:[nsnumber numberwithint:10] forkey:@ "number"];
    3. [Shared synchronize];


Take the following data:

[OBJC]View PlainCopy 
    1. Nsuserdefaults *shared = [[Nsuserdefaults alloc] initwithsuitename:groupid];
    2. NSString *string=[nsstring stringWithFormat:@ "%@", [Gkfx Objectforkey:@ "number"]];
    3. _numberlabel. text=string;

We saved a data in the containing app, and then we took a data in the extension, and I saved a number 10 in the demo, and then we looked at the effect:


Another way to do this is through Nsfilemanager, where the data code is as follows:

[OBJC]View PlainCopy 
  1. Nserror *err = nil;
  2. Nsurl *containerurl = [[Nsfilemanager Defaultmanager] containerurlforsecurityapplicationgroupidentifier  : GroupID];
  3. Containerurl = [Containerurl urlbyappendingpathcomponent:@ "Library/caches/test"];
  4. nsstring *value =@ "123";
  5. BOOL result = [value writetourl:containerurl atomically:YES encoding:nsutf8StringEncoding  error:&err];

Take the data code as follows:

[OBJC]View PlainCopy 
    1. nserror *err =  nil;  
    2.    nsurl *containerurl = [[nsfilemanager  defaultmanager]  CONTAINERURLFORSECURITYAPPLICATIONGROUPIDENTIFIER:GROUPID];  
    3.    containerurl = [containerurl  Urlbyappendingpathcomponent:@ "Library/caches/test"];  
    4.    nsstring *value = [nsstring  Stringwithcontentsofurl:containerurl encoding:nsutf 8stringencoding ERROR:&ERR];  

For more complex projects to add extension, you may need to share the code, you may need to write some common data into their own framework, and then containing the app and extension to save one, in order to achieve the common code, as to the specific implementation, You can try ^_^ yourself.

We'll look at the content in the extension to go back to the containing app, in fact, through the OpenURL, the first need to create a URL type in the containing app, such as:

In extension, write the following code:

[OBJC]View PlainCopy 
    1. [self. Extensioncontext openurl:[nsurl urlwithstring:@ "testwight://"] completionhandler:^ ( BOOL Success) {  
    2. NSLog (@ "Open URL result:%d", success);
    3. }];

It is important to note that the location of the view in extension is not the leftmost start, but the default from the icon after the start, so if you need to modify the code in the following:

[OBJC]View PlainCopy 
  1. The general default view is from the right side of the icon ... If you want to change, you have to implement this method.
  2. -(Uiedgeinsets) Widgetmargininsetsforproposedmargininsets: (uiedgeinsets) Defaultmargininsets {
  3. //uiedgeinsets newmargininsets = Uiedgeinsetsmake (Defaultmargininsets.top, defaultmargininsets.left-16,  Defaultmargininsets.bottom, Defaultmargininsets.right);
  4. //return newmargininsets;
  5. //return Uiedgeinsetszero;//completely on the left ....
  6. return Uiedgeinsetsmake (0. 0, 16. 0, 0, 0);
  7. }

There is sometimes a control in the view is not visible, so you need to add this sentence:

[OBJC]View PlainCopy 
    1. Self. preferredcontentsize = Cgsizemake (self. View. Bounds.size. Width, 100);

Need to manually add, in the drop-down notice in the edit,

iOS8 new feature widget development-B

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.