IOS12 Sirikit new features Shortcuts (i) (Objective-c version)

Source: Internet
Author: User

IOS12 Sirikit added a new feature shortcuts, the equivalent of playing a computer when the user set shortcut keys, such as I set CTRL+F10 to open Notepad, set the success, I can quickly open the Notepad every time I press CTRL+F10 directly.

Shortcuts this time there are two uses, the first is to set the shortcut (shortcut, the same below), through Siri directly back to the app, when you go back to the app, you can get the data to set the shortcut, depending on the data or not according to the data automatically next step.

The second usage is to set up an extended page without entering the app to complete the simple operation of the app, such as sending messages/orders, etc.

Let's start with a brief introduction to the second usage today, first look at the effect, such as:

New single View APP, named: Gotosiri, Language: objective-c

Again in File->new->target->intents Extension, remember to check the include UI Extension to show the extended page later. Name: Ticket, you need to click the Activate two times after creation.

The following two folders appear in the Project catalog:

Next you need to create a intents.intentdefinition file, look at the name like a custom intent, in fact very chicken.

New File->sirikit Intent Definition File

Add a new intent named Test, after the program is created and run once, then click Test, in the callout area you will see the intent file name testintent that you just set, Clicking on the right arrow will go into the TestIntent.h header file, and later use this custom testintent intent when you need to introduce the header file.

In the viewcontroller.m file, "TestIntent.h" and <Intents/Intents.h> two header files are introduced.

#import "ViewController.h"#import "TestIntent.h"#import<Intents/Intents.h>@interfaceViewcontroller () <testintenthandling, inuiaddvoiceshortcutviewcontrollerdelegate>@property (nonatomic,strong) Inuiaddvoiceshortcutviewcontroller*Customshortcutviewcontroller, @property (nonatomic,strong) testintent*testintent, @property (nonatomic,strong) testintentresponse*Testintentresponse, @property (nonatomic,strong) ininteraction*interaction, @property (Nonatomic,strong) inshortcut*shortcut;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.Self.title=@"siritest"; if(@available (IOS10.0, *) {[Inpreferences requestsiriauthorization:^(insiriauthorizationstatus status) {Switch(status) { CaseInsiriauthorizationstatusnotdetermined:nslog (@"the user has not made a selection for the application. ");  Break;  CaseInsiriauthorizationstatusrestricted:nslog (@"This application does not have permission to use the Siri service");  Break;  CaseInsiriauthorizationstatusdenied:nslog (@"user has explicitly denied authorization for this application");  Break;  CaseInsiriauthorizationstatusauthorized:nslog (@"the user can use the authorization of this application");  Break; default:                     Break;    }                    }]; } UIButton*_ADDSIRIBTN = [[UIButton alloc] Initwithframe:cgrectmake ( -,151, $, -)]; [_addsiribtn settitle:@"Add to Siri (Intent)"Forstate:uicontrolstatenormal];    [_addsiribtn SetTitleColor:UIColor.blueColor Forstate:uicontrolstatenormal]; [_addsiribtn addtarget:self Action: @selector (Buildshortcutincurrentviewcontroller) forControlEvents:        UIControlEventTouchUpInside]; [Self.view addsubview:_addsiribtn];}-(void) buildshortcutincurrentviewcontroller{self.testintent=[[Testintent alloc] init]; Self.testIntent.ticket=@"Shenzhen Bay stadium Lee Hom concert"; Self.testIntent.number= [NSNumber Numberwithinteger:2]; Self.testIntent.suggestedInvocationPhrase=@"Buy Tickets"; Self.interaction=[[Ininteraction alloc] initWithIntent:self.testIntent Response:nil]; [Self.interaction donateinteractionwithcompletion:^ (Nserror *_nullable Error) {        if(Error) {NSLog (@"%@", error); }        Else{NSLog (@"Donate Success");        }    }]; Inshortcut*shortcut =[[Inshortcut alloc] initWithIntent:self.testIntent]; Self.customshortcutviewcontroller=[[Inuiaddvoiceshortcutviewcontroller alloc] initwithshortcut:shortcut]; Self.customshortcutviewcontroller.Delegate=Self ; [Self PresentViewController:self.customShortCutViewController animated:yes completion:nil];}-(void) Addvoiceshortcutviewcontrollerdidcancel: (Inuiaddvoiceshortcutviewcontroller *) controller{[controller Dismissviewcontrolleranimated:yes Completion:nil];}-(void) Addvoiceshortcutviewcontroller: (Inuiaddvoiceshortcutviewcontroller *) controller didfinishwithvoiceshortcut: ( Invoiceshortcut *) Voiceshortcut Error: (Nserror *) error{[controller Dismissviewcontrolleranimated:yes Completion:nil];}- (void) Handletest: (nonnull testintent *) Intent Completion: (nonnullvoid(^) (Testintentresponse *_nonnull)) Completion {NSString*ticket =Intent.ticket; Self.testintentresponse=[Testintentresponse Successintentresponsewithticket:ticket]; Completion (self.testintentresponse);}-(void) Confirmtest: (nonnull testintent *) Intent Completion: (nonnullvoid(^) (Testintentresponse *_nonnull)) completion{NSString*ticket =Intent.ticket; Self.testintentresponse=[Testintentresponse Successintentresponsewithticket:ticket]; Completion (self.testintentresponse);}@end

Don't forget to change three info.plist files

Gotosiri in the Info.plist,

Ticket in the Info.plist,

Ticketui in the Info.plist,

In the intenthandler.m file:

#import "IntentHandler.h"#import "TestIntent.h"@interfaceIntenthandler () <TestIntentHandling>@end@implementationIntenthandler- (ID) Handlerforintent: (Inintent *) Intent {//This is the default implementation. If you want different objects to handle different intents,//You can override this and return the handler-want for that particular intent.    returnSelf ;}- (void) Handletest: (testintent *) Intent Completion: (void(^) (Testintentresponse *_nonnull)) Completion {if(Intent.number = =0) {completion ([testintentresponse failureIntentResponseWithTicket:intent.ticket]); } completion ([Testintentresponse successIntentResponseWithTicket:intent.ticket]);}-(void) Confirmtest: (testintent *) Intent Completion: (void(^) (Testintentresponse *_nonnull)) completion{completion ([[[Testintentresponse alloc] Initwithcode:testintentresponsecodeready userActivity:nil]);}@end

Draw a simple interface in Ticketui's Maininterface.storyboard

In INTENTVIEWCONTROLLER.M:

#import "IntentViewController.h"#import<Intents/Intents.h>#import "TestIntent.h"//as an example, this extension ' s Info.plist have been configured to handle interactions for insendmessageintent.//You'll want to the replace this or add other intents as appropriate.//The intents whose interactions you wish to handle must is declared in the extension ' s info.plist.//You can test the example integration by saying things to Siri like://"Send a message using <myApp>"@interfaceIntentviewcontroller () @property (weak, nonatomic) Iboutlet UILabel*TestLabel;@end@implementationIntentviewcontroller- (void) viewdidload {[Super viewdidload]; //Do any additional setup after loading the view.}#pragmaMark-inuihostedviewcontrolling//Prepare your view controller for the interaction to handle.- (void) Configureviewforparameters: (Nsset <inparameter *> *) Parameters Ofinteraction: (ininteraction *) interaction Interactivebehavior: (Inuiinteractivebehavior) Interactivebehavior context: (inuihostedviewcontext) context Completion: (void(^) (BOOL success, Nsset <inparameter *> *configuredparameters, cgsize desiredSize)) Completion {//Do configuration here, including preparing views and calculating a desired size for presentation.testintent*intent = (Testintent *) interaction.intent; Self.testlabel.text= [NSString stringWithFormat:@"buy%@ Zhang%@ ticket", Intent.number,intent.ticket]; if(Completion) {completion (YES, parameters, [self desiredSize]); }        if(Interaction.intenthandlingstatus = =inintenthandlingstatussuccess) {Self.testlabel.text=@"# # #123 # #"; }}-(cgsize) desiredSize {cgsize customsize=[self extensioncontext].hostedviewmaximumallowedsize; Customsize.height=200.0; returncustomsize;}@end

Run the program, you can see the effect, this is the original, forwarding please indicate the source.

IOS12 Sirikit new features Shortcuts (i) (Objective-c version)

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.