Search code examples
iosswiftuiapplicationopenurl

IOS Open Url not working on some devices


@IBAction func openLinkedin() {
    let url = NSURL(string: self.currentUser.linkedin)!

    if(UIApplication.sharedApplication().canOpenURL(url) == true){
        if currentUser.linkedin.rangeOfString("linkedin") != nil{
            UIApplication.sharedApplication().openURL(url)
        }else{
             Utility.sharedInstance.showAlert("\(currentUser.display_name) heeft geen correcte linkedin link.")
        }
        }else{
            Utility.sharedInstance.showAlert("\(currentUser.display_name) heeft geen linkedin link ingevuld.")
        }
}

i have this pice of code in my project. When i test it it works flawlessly. But for some reason it works on my phone and not on my test device. Can anyone explain what is happening?

Self.currentUser.linkedin = https://nl.linkedin.com/in/username

when i tap the button on the phone where it is not working i dont get any errors from canOpenUrl()

is this a bug?

Thanks for any awnser!

Cees


Solution

  • I found the problem. On the test device safari was disabeld. This ment that open url failed silently. i fixed it by putting this above the code:

        if(!UIApplication.sharedApplication().openURL(url)){
            Utility.sharedInstance.showAlertL("Kan de link niet openen. Kopieer de onderstaande link en plak deze in de browser.", link: self.appDelegate.user.linkedin)
        }
    

    the alert looked like this:

    func showAlertL(message:String, link:String){
        let responseAlert = UIAlertView()
        responseAlert.alertViewStyle = UIAlertViewStyle.PlainTextInput
        let tekstfield = responseAlert.textFieldAtIndex(0)
        tekstfield?.text = link
        responseAlert.title = "Opes!"
        responseAlert.message = message
        responseAlert.addButtonWithTitle("OK")
        responseAlert.show()
    }
    

    if it fails silently this error message will promt and put in a textfield with the link inside it. that way the user can copy it and paste it in an brower.

    I hope it helps someone else too!

    Cees