6

I would like to know whether there is a Swift equivalent of the following Objective-C code

NSURL *appURL = [NSURL URLWithString: @"myapp://"];
if ([app canOpenURL: appURL] {
 NSLog(@"The app is this URL Scheme is installed on the device");
}
Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167
Romain
  • 181
  • 1
  • 4
  • 7

2 Answers2

15

Before reading this answer you must solemnly swear not to do any of the activities on the page you linked to. (Looking for dating apps? Seriously?)

The method is essentially the same:

if let appURL = NSURL(string: "myapp://test/url/") {
    let canOpen = UIApplication.sharedApplication().canOpenURL(appURL)
    println("Can open \"\(appURL)\": \(canOpen)")
}
Nate Cook
  • 92,417
  • 32
  • 217
  • 178
  • Here is a small change: if UIApplication.sharedApplication().canOpenURL(uberURLScheme!) { let canOpen = UIApplication.sharedApplication().openURL(url!) } – Romain Oct 23 '14 at 05:47
  • note `sharedApplication` has been renamed to just `shared` and is nolonger a function. – JCutting8 Jan 10 '22 at 05:57
5

let we take two app with name A and B.

Now i want to open app B from app A, so for that first create URLSchema in app B.

Than add below code to info.plist file in app A.

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>app b schema Name</string>
</array>

Now add below code to app A from where you want to open app B.

UIApplication.shared.canOpenURL(URL(string:"app b schema Name://")! as URL)

thank you.

Alvin Butani
  • 51
  • 1
  • 3