What are you doing with the WKInterfaceController method?

Asked 2 years ago, Updated 2 years ago, 101 views


Throw a request from the AppleWatch to the iPhone. [WKInterfaceController openParentApplication: userInfo reply:^(NSDictionary*replyInfo, NSError*error){
if(replyInfor){
...

It says
^(NSDictory*replyInfo, NSError* error)
What is doing?

Also, when receiving an AppleWatch request on the iPhone,
-(void) application:(UIApplication*) application handleWatchKitExtensionRequest:(NSDictionary*)userInfo reply:(void(^)(NSDictionary*)) reply{
   ...
I don't know what **(void(^)(NSDictionary*)) reply** is also doing.
"Maybe it's because I don't understand ""^"" in the first place..."

It doesn't matter if it's a helpful URL.Please tell me who it is.

ios objective-c

2022-09-30 19:28

1 Answers

The notation that begins with ^ is called a block, which is called a closure or unknown function in other languages, but roughly speaking, it is a language function that allows processing to be stored as an object in a variable or passed as an argument.

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Blocks/Articles/bxGettingStarted.html#//apple_ref/doc/uid/TP40007502-CH7-SW2

↑ In addition to the above official documentation, it is recommended to search for "Objective-C Block" and so on.

Now,

 reply:^ (NSDictory* replyInfo, NSError* error)

The part of the is named reply, passing as an argument a block that takes two arguments: NSDictionary and NSError.

In Cocoa, blocks are often used primarily as callbacks for asynchronous methods, which are also used in this method.

The block passed to this reply will be invoked by the system when the app is called.
This will inform you that the asynchronous method has been executed.

The following delivery methods will be invoked for the called application, so

-(void) application:(UIAapplication*) application
handleWatchKitExtensionRequest: (NSDictionary*) userInfo
                         reply:(void(^)(NSDictionary*replyInfo)) reply;

For example, you can inform the Watch side of completion (and pass the information at the same time) by filling the NSDictionary with the required information from the application side and calling the block:

-(void) application:(UIAapplication*) application
handleWatchKitExtensionRequest: (NSDictionary*) userInfo
                         reply:(void(^)(NSDictionary*replyInfo)) reply
    // Do something random...

    NSDictionary* replyInfo=@{@"aaa":@"bbb"};
    reply(replyInfo);// Invoke block as callback when required action is complete
}

The Watch will then call the block you passed as an argument, where you can see the completion, and if necessary, take the action you gave as a block in advance.

[WKInterfaceController openParentApplication:nil reply:^(NSDictionary*replyInfo, NSError* error){
    // This block will be called when the app calls are complete.
    NSLog (@"%@", replyInfo); // { "aaa": "bbb"}
}];


2022-09-30 19:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.