NSUseractivity not working from apple watch to iphone app

807 views Asked by At

I am implementing handoff from an apple watch app to the destination app on my iphone.

On one of the WatchKit Extension viewcontrollers i have the following code:

myActivity = [[NSUserActivity alloc]
                              initWithActivityType: @"com.newsApp.shownews"];
myActivity.userInfo = @{@"newsID":[self.newsID objectAtIndex:0]};
myActivity.title = @"shownews";
myActivity.delegate = self;
[myActivity becomeCurrent];

Then i add the following code in the continueUserActivity inside the appDelegate of my destination iphone app:

-(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *))restorationHandler
{
    if ([userActivity.activityType  isEqual: @"com.newsApp.shownews"]) {
        UIStoryboard *mainStoryboard = self.window.rootViewController.storyboard;
        ShownewsViewController *detailViewController = (ShownewsViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"ShownewsViewController"];

        detailViewController.post_ID = [userActivity.userInfo objectForKey:@"newsID"];

        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController];

        [self.window.rootViewController presentViewController:navigationController animated:YES completion:NULL];

    }   
    return YES;
}

The problem is that userActivity.userInfo is empty but the userActivity.activityType is correctly set to com.newsApp.shownews.

2

There are 2 answers

0
lehn0058 On

Try setting the requiredUserInfoKeys on your NSUserActivity object.

0
Shrawan On

For continue the Activity from Watch to iPhone App use NSUserActivity Xcode 8 Swift-3 :-

enter image description here

Watch Target:

@IBAction func navigateToiPhoneApp() {
      //  let userActivity = NSUserActivity(activityType: "WatchKitHandoff.Notification.com")
        //userActivity.ac
        self.updateUserActivity("com.WatchKit.Notification.", userInfo:["key":"value"], webpageURL:nil)
    }

iPhone Target:

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
            print("continue activity2")
            restorationHandler([self.window!.rootViewController!])
            return true
        }

Update info.plist enter image description here

Simulator do not support Handoff in till Xcode-8.1