Nurdin
Nurdin

Reputation: 23883

Swift, can't open second app using 'deep link'

It seems I can't open the second app using my method. Nothing happened. Is there any silly mistakes here?

My second app .plist file

enter image description here

My first app code

@IBAction func btnCRM(sender: AnyObject) {

        var customURL: NSString = "CRM://"

        if (UIApplication.sharedApplication().canOpenURL(NSURL(fileURLWithPath: customURL as String)!)){
            UIApplication.sharedApplication().openURL(NSURL(fileURLWithPath: customURL as String)!)
        }
    }

Upvotes: 3

Views: 5035

Answers (4)

Hastler
Hastler

Reputation: 45

Swift 5.7 2023

The code below opens the main application

    private func openMainApp() {
        self.extensionContext?.completeRequest(returningItems: nil, completionHandler: { _ in
            guard let url = URL(string: self.appURL) else {
                return
            }
            _ = self.openURL(url)
        })
    }

    // Courtesy: https://stackoverflow.com/a/44499222/13363449 👇🏾
    // Function must be named exactly like this so a selector can be found by the compiler!
    // Anyway - it's another selector in another instance that would be "performed" instead.
    @objc private func openURL(_ url: URL) -> Bool {
        var responder: UIResponder? = self
        while responder != nil {
            if let application = responder as? UIApplication {
                return application.perform(#selector(openURL(_:)), with: url) != nil
            }
            responder = responder?.next
        }
        return false
    }

Upvotes: 0

Alex Shoshiashvili
Alex Shoshiashvili

Reputation: 489

'openURL' was deprecated in iOS 10.0

Updated version:

guard let url = URL(string: "CRM://"), UIApplication.shared.canOpenURL(url) else {
    return
}
UIApplication.shared.open(url, options: [:], completionHandler: nil)

Upvotes: 2

isaced
isaced

Reputation: 1804

try this code:

let url = NSURL(string: "CRM://")
if (UIApplication.sharedApplication().canOpenURL(url!)) {
    UIApplication.sharedApplication().openURL(url!)
}

Upvotes: 2

duncanc4
duncanc4

Reputation: 1201

In addition to the URL Schemes under Item 0, you need to add URL identifier which is CFBundleURLName, as outlined here.

enter image description here

Upvotes: 2

Related Questions