The widget implementation for iOS development
Objective
The advent of IOS extension makes it easier for users to view applications, such as users can view the application's brief information in today's widgets, and then click into the relevant application interface.
For the moment do not table the existing widget articles on the network, this article mainly explains my specific implementation of the widget steps, I hope to be able to help the need to realize the Widgets friends.
Figure 1 today's widget display----take Alipay as an example to illustrate
In turn, the article will proceed from the following several questions, detailed description:
1, how to add widgets for existing projects;
2, how to draw the UI;
3, how to adjust the app;
4, how to share data with host app.
Add Today Extension
Figure 2 Adding target for today
As shown, add today Extension for existing projects, name here not to repeat, we all understand.
Figure 3 Adding the engineering directory after today
This is the engineering directory after adding today extension.
Here, for the existing project to add today extension is finished, run the program can see a similar figure 1 simple effect, very simple AH.
Draw UI
Like children who publish articles online, I am also a code control (loaded b), accustomed to using pure code to draw today's UI.
Figure 4 Deleting the default created maininterface and modifying info.plist
Here, delete the Maininterface.storyboard created by default and modify the Info.plist file as shown in Figure 4. (Of course, the use of storyboard children's shoes can be skipped, direct operation storyboard can)
Figure 5 Setting the size of the widget display view
First, set the size of the widget display view. About the background color of the widget, as well as the specific display of the content you need to draw, here is not the table.
Figure 6 Setting the widget view distance from the left edge to 0
After running the program, you will find a problem: The drawn content is a certain distance from the left edge (about 30px). How to solve this problem, as shown in Figure 6, Todayviewcontroller compliance with the Ncwidgetproviding protocol gives the solution.
To tune up the app
Because the extension and containing apps are two completely separate processes, they can't communicate directly (they can't jump to a specific page like an application's internal click button). To enable the widget to tune up the app, it's OpenURL to start the containing app.
-(void) openurlcontainingapp{///via Extensioncontext with host app to tune up the app [Self.extensioncontext Openurl:[nsurl urlwithstring:@ "Appextension://xxx"] completionhandler:^ (BOOL success) {NSLog (@ "Open URL result:%d, success";}]; }
Data sharing
Figure 7 Adding the app Groups
By using the same group of app groups to read and write areas, the data sharing between extension and containing app can be achieved in two ways nsuserdefaults and Nsfilemanager.
1 Sharing data through Nsuserdefaults
Save Data
-(void) savedatabynsuserdefaults{nsuserdefaults *shared = [[Nsuserdefaults alloc] initwithsuitename:@ " Group.c om.xxx "]; [Shared setobject:@ "Asdfasdf" forkey:@ "widget"]; [Shared synchronize];
}
Read Data
-(NSString *) readdatafromnsuserdefaults{nsuserdefaults *shared = [[Nsuserdefaults alloc] Initwithsuitename: @ "Group.c om.xxx"]; NSString *value = [Shared valueforkey:@ "widget"]; return value;
}
2 Sharing data through Nsfilemanager
Save data-(BOOL) savedatabynsfilemanager{nserror *err = nil; Nsurl *containerurl = [[Nsfilemanager Defaultmanager] Containerurlforsecuri tyapplicationgroupidentifier:@ " Group.com.xxx "]; Containerurl = [Containerurl urlbyappendingpathcomponent:@ "Library/caches/widget"]; NSString *value = @ "asdfasdfasf"; BOOL result = [value Writetourl:containerurl atomically:yes encoding:nsutf8stringencoding error:&err];
if (!result) {NSLog (@ "%@", err);} else {NSLog (@ "Save value:%@ success.", value); Read Data-(NSString *) readdatabynsfilemanager{nserror *err = nil; Nsurl *containerurl = [[Nsfilemanager Defaultmanager] Containerurlforsecuri tyapplicationgroupidentifier:@ " Group.com.xxx "]; Containerurl = [Containerurl urlbyappendingpathcomponent:@ "Library/caches/widget"]; NSString *value = [NSString stringwithcontentsofurl:containerurl encoding:nsutf8stringencoding error:&err];
return value; }
Here, the widget for adding apps to today is complete, and you can browse the preset shortcut services from today.
Thank you for reading, I hope to help you, thank you for your support for this site!