Search code examples
ios8handoffcontinuitynsuseractivity

Handoff not working from native app to website


My devices:

  • iPad Mini (latest), iOS 8 dp5.
  • Macbook Air, Yosemite dp5.

I have Handoff working between the two above devices. Safari, Mail, Messages, Calendar, etc. all handoff with no problems.

I can even handoff between my website on the Air and my native app on the iPad.

What I can't do yet is go from my native app on the iPad to my website in Safari on my Air.

For the first view controller that loads in my native app, I have this:

- (void)viewDidLoad

{
    [super viewDidLoad];

    NSUserActivity *webHandoff = [[NSUserActivity alloc] initWithActivityType:@"com.myApp.iphone.staging.webbrowsing"];

    webHandoff.webpageURL = [NSURL URLWithString:@"http://staging.myApp.com"];

    [webHandoff becomeCurrent];
}

In my app's Info.plist file, I have this:

<key>NSUserActivityTypes</key>
<array>
      <string>com.myApp.iphone.staging.webbrowsing</string>
</array>

Am I missing something or do I have something configured incorrectly?

Thanks for any help!


Solution

  • I made two significant changes to my code:

    1) configure/destroy and set the NSUserActivity object in viewDidAppear/disappear as opposed to viewDidLoad:

    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
    
        NSUserActivity *webHandoff = [[NSUserActivity alloc] initWithActivityType:@"com.myApp.iphone.staging.web-browsing"];
        webHandoff.webpageURL = self.handoffWebpageURL;
        [self setUserActivity:webHandoff];
    }
    
    - (void)viewDidDisappear:(BOOL)animated
    {
        [super viewDidDisappear:animated];
    
        [self.userActivity invalidate];
    }
    

    2) Since UIViewController is a subclass of UIResponder and UIResponders have a userActivity property, instead of calling [webHandoff becomeCurrent] I simply called [self setUserActivity:webHandoff];

    Not sure why moving it out of viewDidLoad had any impact, and not sure why I need to set it to the viewController's instance of NSUserActivity, but the changes above give me a solid and reliable handoff experience throughout my entire app.