CallKit find number used to start app from native

2020-07-18 11:12发布

I've implemented CallKit in our app. Calls our app makes are displayed in the native phone app's recents list.

When tapping an entry for our app in the recents list, our app is started. Is there a way to find out which number(/entry) was used to start our app? (openURL or something)

标签: ios callkit
1条回答
forever°为你锁心
2楼-- · 2020-07-18 11:37

You'll want to implement application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool on the AppDelegate. For this particular action, userActivity will have a interaction property, with an intent property that is an instance of INStartAudioCallIntent (or INStartVideoCallIntent if your app does video).

The intent object has an array of INPerson contact objects on it, which can be used to determine information about what was dialed.

If your app doesn't load any contacts into the system, your calls are one-to-one (as opposed to group) and you just want access to the "dialed number", you'll probably find what you want at intent.contacts?.first?.personHandle?.value.

Also note that you'll need to link and import the Intents framework.

Update

The previous answer was only half-right. There are two ways of getting this information, and both should be implemented.

The continueUserActivity variant above will be called when an item in recents is tapped (or a contact is selected), and your app is already running.

However, if your app is not currently running, then it will be launched and continueUserActivity will not be called. Instead, the system will invoke your AppDelegate's didFinishLaunchingWithOptions with a UIApplicationLaunchOptionsKey.userActivityDictionary, which can be used like:

if let activityOptions = launchOptions?[UIApplicationLaunchOptionsKey.userActivityDictionary] as? [String: AnyObject],
   let activity = activityOptions["UIApplicationLaunchOptionsUserActivityKey"] as? NSUserActivity {
  self.launchWithActivity(activity)
}

Once you have the NSUserActivity instance, the behavior is the same as it is in continueUserActivity

Update Again

Per @vivek takrani in the comments below, it appears that continueUserActivity may be called at all times, whether the app was previously open or not. I would test this on earlier versions of iOS 10 if you intend to support it, as I don't believe this was the case at the time this answer was written.

查看更多
登录 后发表回答