Rasmus Höglund Rasmus Höglund - 28 days ago 7
iOS Question

CallKit find number used to start app from native phone app

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)

Answer

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