Is it possible to launch any app from within another app?
For example, in my application, I want the user to push a button and launch another app (don't close the current app, just open another app and switch to it).
How can I do it in iOS 8 using Swift?
The only way to do this is to use a deep link, which the developer of an app must have created.
The iOS SDK does not allow your app to interact with other apps unless it uses the new Extensions framework introduced with iOS 8. However, this extension only allows you to provide content and capabilities of your own app within another: you cannot force another app to open.
What you've described is only possible with deep links that are fairly uncommon and must be defined by the developer of the app you are trying to open.
So, for example, a link in your app could open the Pocket application, which allows you to save articles for later reading, with a pocket://
link (as opposed to http://
or https://
) and, similarly, the Pebble Smartwatch application can be opened with a pebble://
link. However, these are links that are defined by the developers of those applications and this technique does not apply to all apps.
I think you are looking for Apple URL Schemes. If the third-party app have any URL Schemes defined, then you can use the following code to open it:
if let url = URL(string: "http://stackoverflow.com/questions/24728854/is-it-possible-to-launch-another-app-in-ios-8-using-swift/") // Change the URL with your URL Scheme
{
if UIApplication.shared.canOpenURL(url)
{
UIApplication.shared.openURL(url)
}
}
Others have described the right idea already here but with some issues around handling the URL and the function to call. UIApplication.shared.open(_ url:)
is deprecated, and replaced with a new version with a completion handler.
As others have mentioned, you need the recipient app to have defined a URL scheme. Provided they have, you can do something like this:
func launchTwitterApp() {
guard let url = URL(string: "twitter://user?id=12345") else {
preconditionFailure("There was something wrong with our url, this shouldn't happen")
}
UIApplication.shared.open(url, completion: { success in
if success {
print("We opened the Twitter app.")
}
else {
print("Something went wrong. We might not have the necessary app or the right url.")
}
})
}
No matter what language you are using (Obj-C/Swift), you can always use openURL
to open a URL scheme to launch a app (if the URL is provided).
We can do this by creating custom URL of that app and after that we can open custom URL on click event of a button.
let kCustomURLScheme = "DemoDriver://Daffomac.DemoDriver"
func openCustomApp() {
let Url = NSURL(string: kCustomURLScheme)
if UIApplication.shared.canOpenURL(Url! as URL)
{
UIApplication.shared.openURL(Url! as URL)
} else {
//redirect to safari because the user doesn't have Instagram
UIApplication.shared.openURL(NSURL(string: kCustomURLScheme)! as URL)
}
}