Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 231
djibouti33 Avatar asked Aug 04 '14 23:08

djibouti33


1 Answers

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.

like image 117
djibouti33 Avatar answered Nov 14 '22 00:11

djibouti33