3

I'm adopting Handoff in my iOS and Mac apps. iOS -> iOS and iOS -> Mac are working flawlessly! It's great. However, Mac -> iOS never works. I've tested on both El Capitan and Sierra, in different machines. If I add a webpageURL as fallback, Safari in iOS does pickup the activity, but the native iOS app won't show up. I'm using this piece of code in the Mac app

class ViewController: NSViewController {  
    override func viewDidAppear() {  
        super.viewDidAppear()      
        self.startUserActivity()  
    }    
    func startUserActivity() {  
        let userActivity = NSUserActivity(activityType: "net.myapp.myactivitytype")  
        userActivity.isEligibleForHandoff = true  
        userActivity.isEligibleForSearch = false  
        userActivity.isEligibleForPublicIndexing = false  
        userActivity.title = "Handoff test"  
        userActivity.userInfo = ["key": "value"]  
        userActivity.requiredUserInfoKeys = Set<String>(arrayLiteral: "key")  
        userActivity.webpageURL = URL(string: "https://myapp.net/myurl") // I can pick this up in mobile safari  
        self.userActivity = userActivity  
    }    
    override func updateUserActivityState(_ userActivity: NSUserActivity) {  
        userActivity.addUserInfoEntries(from: ["key":"Updated value"])  
        super.updateUserActivityState(userActivity)  
    }  
} 

In iOS side, I already handle universal links with applinks:myapp.net , and tried adding activitycontinuation:myapp.net as well (along with the proper configuration on apple-app-site-association file. This made my Native app pickup a corresponding url from Mac's Safari, but Native Mac app still isn't able to Handoff to Native iOS app. I'm out of ideas, anyone have insights as why this can be happening ? On iOS I'm using 10.2. Btw I couldn't make Handoff work at all on iOS 9.

Best regards Rafael

Rafael Nobre
  • 5,062
  • 40
  • 40
  • I am having an issue getting handoff to broadcast with a mac -> mac situation as well. I know that sometimes you need to use userActivity.becomeCurrent() have you tried this already? – Schuuure Feb 03 '17 at 20:34

2 Answers2

1

I finally solved it. The answer had nothing to do with signing profiles like the accepted answer suggests.

I reported a bug report to Apple explaining the whole situation, and Apple told me that my iOS app's info.plist doesn't declare NSUserActivityTypes at all, and my Mac app's info.plist correctly declares NSUserActivityTypes, resulting in handoff only working from iOS to Mac and never Mac to iOS.

This issue is due to the iOS version of the app not claiming the activity type in the info.plist, resulting in the iOS device not knowing what app to give the handoff to.

In my Xcode project in the left navigation panel, I opened info.plist and NSUserActivityTypes was indeed there already. But I then realized that there are multiple info.plist files in my project when I search it using Finder in the project folder. Then I physically went into my project folder with Finder and tried to locate this info.plist file, but when I located one inside projectName/projectName it opened a different info.plist file that I couldn't initially access via Xcode project navigator. The different info.plist file did not have NSUserActivityTypes, and so I added it, and woala~ handoff finally worked from Mac to iOS! It is very odd there were fake multiple info.plist in my project. My real info plist was hidden from Xcode...

coolcool1994
  • 3,704
  • 4
  • 39
  • 43
0

In my situation, the issue was caused by a missing entitlement when code signing the Mac app. When running codesign -d --entitlements - /Applications/MyApp.app, it was missing an entry related to the team identifier. This is the key for iOS to connect the Mac Handoff to its native counterpart (and not some other rogue app that tries to use the same Handoff identifier)

<key>com.apple.developer.team-identifier</key>
<string>MY_TEAM_ID</string>

To add this entitlement, create a Provisioning Profile in Dev Center for your app, download it and import it in Xcode's manual signing. It did not work for me either in automatic, or manual without a provisioning profile (which Xcode states as optional)

Rafael Nobre
  • 5,062
  • 40
  • 40
  • I have the exact same problem. Handoff with Safari works both from iOS to Mac and vice versa, but Handoff for my app is only working from iOS to Mac. I tried putting your code in the info.plist but still handing is not working from Mac to iOS. Where did you add your block of code to make it work (I tried putting it to info.plist)? – coolcool1994 Jul 01 '18 at 07:46
  • I have enabled Automatically manage signing. Also tried adding that in the entitlement file, and it still didn't work for me. – coolcool1994 Jul 01 '18 at 08:03
  • When I add com.apple.developer.team-identifier to my macOS and set it to something random my Mac app builds just fine. But if I add that to iOS and set it to something random, app doesn't build with error that com.apple.developer.team-identifier doesn't match. So I feel like my macOS one is missing this... but adding that to the entitlement didn't fix this. Did you do anything else? – coolcool1994 Jul 01 '18 at 08:14
  • It didn't work. 1) I added com.apple.developer.team-identifier MY_TEAM_ID to my entitlement file. 2) Manually downloaded my Privisioning Profile for my Mac app and set signing to manual. They didn't make handoff to work. – coolcool1994 Jul 09 '18 at 14:16
  • Downloading provisioning profile didn't create com.apple.developer.team-identifier key in my entitlement file. I had to manually put that key in but it made no difference. – coolcool1994 Jul 09 '18 at 14:18
  • Could you help me? I am stuck really bad. I have the exact same situation where handoff works for Safari both ways, but handoff for my app only works from iOS to Mac, and never Mac to iOS. – coolcool1994 Jul 09 '18 at 14:21