问题
I am testing the execution of some code on the iPhone app. I follow the documentation that Apple suggests (without using background tasks, just a console log). However I do not get anything on the console (I'd like to see the string "howdy").
Is this because I am running the WatchKit Extension app on the simulator? Or is there something that I am missing?
Apple says:
If you are using openParentApplication:reply:, make sure you create a background task immediately upon entering application:handleWatchKitExtensionRequest:reply:. This will make sure that the iPhone app gets time in the background instead of being suspended again. Additionally, wrap the call to endBackgroundTask: in a dispatch_after of 2 seconds to ensure that the iPhone app has time to send the reply before being suspended again.
My implementation on the WatchKit extension (action method linked to a button):
- (IBAction)sendMessageToApp{
NSString *requestString = [NSString stringWithFormat:@"executeMethodA"]; // This string is arbitrary, just must match here and at the iPhone side of the implementation.
NSDictionary *applicationData = [[NSDictionary alloc] initWithObjects:@[requestString] forKeys:@[@"theRequestString"]];
[WKInterfaceController openParentApplication:applicationData reply:^(NSDictionary *replyInfo, NSError *error) {
NSLog(@"\nReply info: %@\nError: %@",replyInfo, error);
}];
NSLog(@"sending message..");
}
My implementation on the AppDelegate.m file:
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply {
NSString * request = [userInfo objectForKey:@"requestString"];
NSLog(@"howdy");
// This is just an example of what you could return. The one requirement is
// you do have to execute the reply block, even if it is just to 'reply(nil)'.
// All of the objects in the dictionary [must be serializable to a property list file][3].
// If necessary, you can covert other objects to NSData blobs first.
NSArray * objects = [[NSArray alloc] initWithObjects:[NSDate date],[NSDate date],[NSDate date], [NSDate date], nil];
NSArray * keys = [[NSArray alloc] initWithObjects:@"objectAName", @"objectdName", @"objectBName", @"objectCName", nil];
NSDictionary * replyContent = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
reply(replyContent);
}
What I get in the console:
2015-04-16 14:43:44.034 FanLink WatchKit Extension[3324:142904] <InterfaceController: 0x608000081fe0> initWithContext
2015-04-16 14:44:04.848 FanLink WatchKit Extension[3324:142904] sennding message..
2015-04-16 14:44:04.854 FanLink WatchKit Extension[3324:142904]
Reply info: {
objectAName = "2015-04-16 13:44:04 +0000";
objectBName = "2015-04-16 13:44:04 +0000";
objectCName = "2015-04-16 13:44:04 +0000";
objectdName = "2015-04-16 13:44:04 +0000";
}
Error: (null)
回答1:
This is because you are only debugging the WatchKit extension target. If you wanted to see in the console your main application logs as well you will need to attach the main application's process through the Xcode debugger. Go to Debug-->Attach a process-->(Then select by identifier and type you applications name)
For a better walk through I found this great resource for you specially for WatchKit/WatchKit extension debugging: https://mkswap.net/m/blog/How+to+debug+an+iOS+app+while+the+WatchKit+app+is+currently+running+in+the+simulator
回答2:
- Your console logs are correct because you are currently debugging only WatchKit Extension target So you will receive logs which are written in WatchKit extension. NSLog written in iOS App will not be printed in console.
回答3:
don't know if off topic but there is a simple mistake in the code above in watch kit there is initWithObjects:@[requestString] forKeys:@[@"theRequestString"]
and in appdelegate NSString * request = [userInfo objectForKey:@"requestString"];
"theRequestString" do not corespondent to "requestString"
took me some time to figure out why my function doesn't fire up
thanks for the code it helped
来源:https://stackoverflow.com/questions/29677132/understanding-handlewatchkitextensionrequest