3

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)!)
        }
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
Nurdin
  • 23,382
  • 43
  • 130
  • 308

4 Answers4

2

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

duncanc4
  • 1,191
  • 1
  • 9
  • 17
1

try this code:

let url = NSURL(string: "CRM://")
if (UIApplication.sharedApplication().canOpenURL(url!)) {
    UIApplication.sharedApplication().openURL(url!)
}
isaced
  • 1,735
  • 3
  • 16
  • 24
1

'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)
0

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
    }
Hastler
  • 5
  • 5