Search code examples
swiftios9

Swift canOpenURL() error iOS9 privacy issue tel:// and telprompt://


    @IBAction func callfriendbutton(sender: AnyObject) {

            let phoneNumberstring = NSUserDefaults.standardUserDefaults().stringForKey("phoneNum");

        var escapednum: String = phoneNumberstring!.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!


        let urlString = "telprompt://\(escapednum)";

        let url = NSURL(string: urlString);
        if UIApplication.sharedApplication().canOpenURL(url!) {
        UIApplication.sharedApplication().openURL(url!);
        }

}

I tried everything and it still won't work, I also added to my info.plist allowing both tel and telprompt to work but this is the error I get

2016-03-27 03:18:24.013 TACTAC[64525:4775035] -canOpenURL: failed for URL: "telprompt://1234567891" - error: "(null)"


Solution

  • Why you need to add stringByAddingPercentEscapesUsingEncoding this is generally used in Web Url

    you can try the following code if it works for you

    let strCallNo: String = "1234567891"
            let trimmedString = strCallNo.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
            let telUrl:NSURL? = NSURL(string:trimmedString)
            if ((telUrl) != nil){
                if(UIApplication.sharedApplication().canOpenURL(telUrl!)){
                    UIApplication.sharedApplication().openURL(NSURL(string: "telprompt://"+strCallNo)!)
                }else
                {
                    print("Call not available")
                }
            }