Search code examples
iosios8handoff

How does one do a Native App to Web Browser Handoff?


I have read the documentation and I understand that using Handoff I can exchange data between a specific website and app.

I have a curated list of items from various RSS feeds, all having links pointing to different websites. I would like to give the user the ability to open the link for an item (like a "More" button) in Safari on their mac instead of Safari on their iPhone.

Since all the links would be from different domains, certificates don't really apply. Is it possible to open Safari on a mac with a specific URL from an iOS app using Handoff? I couldn't really understand from the documentation if this was a possibility or not.


Solution

  • Create an NSUserActivity object and specify the webPageURL property. Use an activityType specific to your app. If there is no app on the mac which supports that particular activityType, then Safari will pick it up.

    NSUserActivity* myActivity = [[NSUserActivity alloc]
                      initWithActivityType: @"com.company.acme.myapp"];
    
    myActivity.webpageURL = [NSURL URLWithString:@"http://www.google.co.uk"];
    

    As per the docs for NSUSerActivity.webPageURL:

    When no suitable application is installed on a resuming device and the webpageURL property is set, the specified webpage is loaded and the user activity is continued in a web browser.

    Both flows (app-to-browser and browser-to-app) are documented at https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/Handoff/AdoptingHandoff/AdoptingHandoff.html

    Your question comes under "Native App-to-Web Browser Handoff".