IOS10 Notice and notice expand extension use Detailed (with demo)

Source: Internet
Author: User

Introduction to 1.1-IOS10 Expansion

1.2-IOS10 Notification use

1.3-IOS10 Notice Expand extension Use

1.4-Effect Demo

    • If you are interested in developing dark Horse learning iOS development: Dark Horse Programmer

    • Source code: Code Download

Introduction to 1.1-IOS10 Expansion
    • One of the biggest highlights of the IOS10 system is the increased functionality of the system application.Extension

      • Extension function can be understood as a custom system interface
    • In this section we will use the custom system notification interface to learn Extension

      • Other functions of the We can not explain one by one Extension , I hope that we can understand the basis, to achieve extrapolate

1.2-IOS10 Notification use
    • After iOS10, the Notification Content iOS system launched a new framework to support the expansion of the custom notification interface.<UserNotifications>

      • Notification of the use of ideas and steps unchanged, but the API has changed, and all the system will be prompted, we only need to modify according to the system prompt can
    • 1. Request authorization and Add classification

#import "AppDelegate.h" //IOS10 notify the new framework#import <UserNotifications/UserNotifications.h> //IOS10 Custom Notification interface#import <UserNotificationsUI/UserNotificationsUI.h>  @interface appdelegate () <unusernotificationcenterdelegate>@end @implementation appdelegate - (BOOL) Application: (uiapplication*) Application Didfinishlaunchingwithoptions: (nsdictionary*) Launchoptions {//Override point for customization after application launch.    //Apply for authorization    //1. Creating notification hubsUnusernotificationcenter *center = [Unusernotificationcenter currentnotificationcenter];//Set the agent for the notification hub (the receive time of the listen notification after iOS10 and the response of the interactive button is done by proxy)Center. Delegate= Self;//2. Notification Hubs Settings Classification[Center Setnotificationcategories:[nsset setwithobjects:[ SelfCreatecatrgory],Nil]];//3. Requesting Authorization    /**unauthorizationoption Unauthorizationoptionbadge = (1 << 0), red circle Unauthorizationoptionsound = (1      << 1), Sound Unauthorizationoptionalert = (1 << 2), Content Unauthorizationoptioncarplay = (1 << 3), onboard notifications */[Center requestauthorizationwithoptions:unauthorizationoptionalert| unauthorizationoptionbadge| Unauthorizationoptionsound completionhandler:^ (BOOLGranted,Nserror* _nullable error) {if(Granted = =YES) {NSLog(@"Authorized Success"); }    }];return YES;}//Receive notifications when the app is in the foreground- (void) Usernotificationcenter: (unusernotificationcenter *) Center willpresentnotification: (unnotification *) notification Withcompletionhandler: (void(^) (unnotificationpresentationoptions)) completionhandler{//Pop up a webpage    UIWebView*webview = [[UIWebViewAlloc] Initwithframe:cgrectmake (0,0, -, -)]; WebView. Center= Self. Window. Center; [WebView loadrequest:[nsurlrequestrequestwithurl:[Nsurlurlwithstring:@"Http://www.itheima.com"]]]; [ Self. WindowAddsubview:webview];//pop-up animationsWebView. Alpha=0; [UIViewAnimatewithduration:1animations:^{WebView. Alpha=1; }];}#pragma mark-Create notification classifications (interactive buttons)-(Unnotificationcategory *) createcatrgory{//Text interaction (iOS10 support for text interaction with notifications)    /**options unnotificationactionoptionauthenticationrequired for text unnotificationactionoptionforeground foreground mode, in Into the app unnotificationactionoptiondestructive destroy mode, do not enter the app * /Untextinputnotificationaction *textinputaction = [Untextinputnotificationaction actionwithidentifier:@"Textinputaction"title:@"Please enter information"Options:unnotificationactionoptionauthenticationrequired textinputbuttontitle:@"Input"textinputplaceholder:@"How many more things to say ..."];//Open App buttonUnnotificationaction *action1 = [Unnotificationaction actionwithidentifier:@"ForeGround"title:@"Open"Options:unnotificationactionoptionforeground];//Do not open the app buttonUnnotificationaction *action2 = [Unnotificationaction actionwithidentifier:@"BackGround"title:@"Off"Options:unnotificationactionoptiondestructive];//Create categories    /** Identifier: identifier of the taxonomy, notification can add different types of category interactive Buttons actions: Interactive button intentidentifiers: Categorical internal identifiers There's nothing to do with the general emptiness. Options: Notifications Parameter unnotificationcategoryoptioncustomdismissaction: Custom interactive button Unnotificationcategoryoptionallowincarplay: On-board interactive */Unnotificationcategory *category = [Unnotificationcategory categorywithidentifier:@"category"Actions:@[textinputaction,action1,action2] intentidentifiers:@[] Options: Unnotificationcategoryoptioncustomdismissaction];returnCategory;}//Button click event- (void) Usernotificationcenter: (unusernotificationcenter *) Center Didreceivenotificationresponse: ( Unnotificationresponse *) Response Withcompletionhandler: (void(^) ()) completionhandler{//According to Identifer to determine the button type, if it is textinput to get the input text    if([Response. Actionidentifierisequaltostring:@"Textinputaction"]) {//Get text responseUntextinputnotificationresponse *textresponse = (Untextinputnotificationresponse *) response;NSLog(@"Input:%@", Textresponse. Usertext); }//Processing other time    NSLog(@"%@", response. Actionidentifier);} - (void) Applicationwillresignactive: (uiapplication*) Application {//Sent when the application is on-move from active to inactive state. This can occur for certain types of temporary interruptions (such as a incoming phone call or SMS message) or when the US Er quits the application and it begins the transition to the background state.    //Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.}- (void) Applicationdidenterbackground: (uiapplication*) Application {//Use this method to release shared resources, save user data, invalidate timers, and store enough application state Information to the restore your application to the it is terminated later.    //If Your application supports background execution, this method is called instead of Applicationwillterminate:when The user quits.}- (void) Applicationwillenterforeground: (uiapplication*) Application {//called as part of the transition from the background to the active state; Here you can undo many of the changes mad E on entering the background.}- (void) Applicationdidbecomeactive: (uiapplication*) Application {//Restart Any tasks this were paused (or not yet started) while the application is inactive. If the application is previously in the background, optionally refresh the user interface.}- (void) Applicationwillterminate: (uiapplication*) Application {//Called when the application are about to terminate. Save data if appropriate. See also Applicationdidenterbackground:.}@end
    • 2. Send notification (with category interactive button)
#pragma mark-Send local notification-(ibaction) Sendlocalnotification: (ID) Sender {//1. Creating notification hubsUnusernotificationcenter *center = [Unusernotificationcenter currentnotificationcenter];//2. Checking the current user authorization[Center getnotificationsettingswithcompletionhandler:^ (unnotificationsettings * _nonnull settings) {NSLog (@"Current authorization status:%zd", [Settings authorizationstatus]);//3. Creating a notificationUnmutablenotificationcontent *notification = [[Unmutablenotificationcontent alloc] init];//3.1 Notification TitleNotification.title = [NSString localizedusernotificationstringforkey:@"Wisdom Podcast"Arguments:nil];//3.2 Small titleNotification.subtitle = @"Hellow World";//3.3 Notification ContentNotification.body = @"Welcome to the Dark Horse programmer";//3.4 Notification SoundNotification.sound = [Unnotificationsound defaultsound];//3.5 Notification Small Circle numberNotification.badge = @2;//4. Creating a trigger (equivalent to the time of notification firing in iOS9)        /** notification trigger mainly has three kinds of untimeintervalnotificationtrigger specified time trigger uncalendarnotificationtrigger specified calendar Time trigger Unlocationnotificationtrigger specified zone trigger * /Untimeintervalnotificationtrigger * Timetrigger = [Untimeintervalnotificationtrigger triggerWithTimeInterval:5Repeats:no];//5. Classification of the specified notification (1) identifer represents the unique identifier when creating the classification (2) The code must be set before the notification request is created, otherwise invalidNotification.categoryidentifier = @"category";//Add attachments to notifications (picture music movies are all possible)NSString *path = [[NSBundle Mainbundle] pathforresource:@"logo"oftype:@"PNG"]; Unnotificationattachment *attachment = [Unnotificationattachment attachmentwithidentifier:@"Image"Url:[nsurl Fileurlwithpath:path] Options:nil Error:nil]; Notification.attachments = @[attachment];//7. Creating a notification request        /** Identifier: Notification request identifier for deleting or finding notifications content: Notification contents trigger: Notification Trigger * /Unnotificationrequest *request = [Unnotificationrequest requestwithidentifier:@"Localnotification"Content:notification Trigger:timetrigger];//8. Notification hubs sending notification requests[Center Addnotificationrequest:request withcompletionhandler:^ (nserror * _nullable error) {if(Error = = nil) {NSLog (@"Notification sent successfully"); }Else{NSLog (@"%@", error);    }        }]; }];}
    • 3. Removal of notifications
#pragma mark-Remove all notifications- (ibaction) Removeallnotification: (ID) Sender {//1. Creating notification hubsUnusernotificationcenter *center = [Unusernotificationcenter currentnotificationcenter];//2. Delete a notification that has been pushed[Center removealldeliverednotifications];//3. Deleting an unsolicited notification request[Center removeallpendingnotificationrequests];}#pragma mark-Remove the specified notification- (ibaction) Removesinglenotification: (ID) Sender {//1. Creating notification hubsUnusernotificationcenter *center = [Unusernotificationcenter currentnotificationcenter];//2. Delete sent notifications for the specified identifer[Center removedeliverednotificationswithidentifiers:@[@"Localnotification"]];//3. Delete a comrade request that specifies identifer not sent[Center removedeliverednotificationswithidentifiers:@[@"Localnotification"]];}
1.3-IOS10 Notice Expand extension Use
    • 1. Add notification Extension

    • 2. notification extension extension is actually equivalent to re-adding an application in the current application, and the project will add the corresponding code folder and target

    • 3. The default expansion controller has only one label, we can customize our controller interface here

    • 4. You can also load the data pushed in the notification

    • 5. Configuring the Plist file
      • By default the application will not load the extended interface, need to configure the plist file, close the system default notification interface

    • 6. Running
      • Run without the choice Extension of target, select the application's target directly

1.4-Effect Demo

IOS10 Notice and notice expand extension use Detailed (with demo)

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.