CallKit find number used to start app from native phone app

时光总嘲笑我的痴心妄想 提交于 2019-12-21 20:24:33

问题


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)


回答1:


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.



来源:https://stackoverflow.com/questions/39789495/callkit-find-number-used-to-start-app-from-native-phone-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!