iOS extension extension today

Source: Internet
Author: User

1. Introduction

The extension (Extension) is a new feature introduced in iOS 8. Extensions make it possible to interact with data between apps. Before the iOS 8 system, each app is physically independent of each other, and apps cannot exchange visits to each other's private data. After the extension is introduced, other apps can exchange data with the extension. 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. This article simply records today's use!

2. Creation of the Today extension

Method One: Create through path file-->new-->target!

Method Two: Directly click on the item-click +!

2. Today's introduction and method description

Today's extension, although attached to the app, but it is a separate individual, the use of files, resources, third parties, etc., need to be set separately!

After today's creation, it's actually a viewcontroller! The minimum height is 110pt. There are three-party protocol methods:

//new methods for iOS10//to set the method for whether the widget is expanded or collapsed, you can set the related Preferredcontentsizes property to modify the size- (void) Widgetactivedisplaymodedidchange: (Ncwidgetdisplaymode) Activedisplaymode withmaximumsize: (CGSize) maxSize{ if(Activedisplaymode = =ncwidgetdisplaymodecompact) {NSLog (@"default size:%@ in stacked state", Nsstringfromcgsize (maxSize)); } Else{NSLog (@"default size:%@ in expanded state", Nsstringfromcgsize (maxSize)); }    }//This method is a protocol method used to inform the widget controller whether it needs to be updated- (void) Widgetperformupdatewithcompletionhandler: (void(^) (Ncupdateresult)) Completionhandler {//Perform any setup necessary in order to update the view. //If An error was encountered, use ncupdateresultfailed//If There ' s no update required, use Ncupdateresultnodata//If There ' an update, use NcupdateresultnewdataCompletionhandler (ncupdateresultnewdata);//typedef ns_enum (Nsuinteger, ncupdateresult) {//Ncupdateresultnewdata,//Ncupdateresultnodata,//ncupdateresultfailed//} ns_enum_available_ios (8_0);}//iOS10 version will no longer be aroused//The method used to set the border spacing of the widget controller, and if deviations occur, you can adjust the return value of this method to operate-(Uiedgeinsets) Widgetmargininsetsforproposedmargininsets: (uiedgeinsets) defaultmargininsets NS_DEPRECATED_IOS (8_ 0, 10_0,"This method is not being called on widgets linked against IOS versions 10.0 and later.");

2.1 Two options are loaded via storyboard loading and encoding:

By modifying the plist file storyboard

Encoding load settings:

2.2 Today's expansion and folding:

The first step: Setting the Widgetlargestavailabledisplaymode property;

    Self.extensionContext.widgetLargestAvailableDisplayMode = ncwidgetdisplaymodeexpanded;

The second step: Implement the Protocol method;

//new methods for iOS10//to set the method for whether the widget is expanded or collapsed, you can set the related Preferredcontentsizes property to modify the size- (void) Widgetactivedisplaymodedidchange: (Ncwidgetdisplaymode) Activedisplaymode withmaximumsize: (CGSize) maxSize{ if(Activedisplaymode = =ncwidgetdisplaymodecompact) {NSLog (@"default size:%@ in stacked state", Nsstringfromcgsize (maxSize)); } Else{NSLog (@"default size:%@ in expanded state", Nsstringfromcgsize (maxSize)); }    }

  

3. Sharing data

Sharing data through extension points, real-computer testing requires registering an app Groups on the developer site. Simulator Pro test can be used free of charge (App groups beta group.com.xxx.TextToday), followed by bundle Identifier.

3.1 Developer website Register an app Groups

3.2 Certificates are deployed using automatic signing, setting the project and adding the appgroups option to the target in today

3.3 Data stored in the project, get data in today

[[NSUserDefaults alloc] initWithSuiteName:@"groupName"]to create NSUserDefaults an object in this format, groupName fill in the AppGroups name of the current settings tick.

-(void) Savadatawitharray: (Nsarray *) dataarray {    NSData  *data = [nskeyedarchiver Archiveddatawithrootobject:dataarray];     *defaults = [[Nsuserdefaults alloc] Initwithsuitename:@ "group.com.ForeverGuard.TextToday " ];    [Defaults setobject:data forkey: @" Todaymodel " ];    [Defaults synchronize];}

Get the data: Note that the data storage needs to conform to the Nscoding protocol, the type of storage, the custom transfer data store, and so on.

-(void) readdata {    *defaults = [[Nsuserdefaults alloc] Initwithsuitename:@ "  Group.com.ForeverGuard.TextToday"];     *dataarray = [Defaults objectforkey:@ "todaymodel"];     = [Nskeyedunarchiver Unarchiveobjectwithdata:dataarray];    }

By loading a tableview in the Today interface, the effect

Note the model and resources used by today and the project need to set the target option:

4. Click today to jump to the app

4.1 Setting the app URL schemes

4.2 In today's jump code:

-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{    [ TableView Deselectrowatindexpath:indexpath Animated:yes];    [Self.extensioncontext openurl:[nsurl urlwithstring:[nsstring stringWithFormat:@ "texttoday:// todaymodel=%@",@"123"]] completionhandler:nil];}

  

iOS extension extension today

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.