Unable to open phone application in IOS 9 swift

My info.plist


<key>LSApplicationQueriesSchemes</key>

<array>

<string>telprompt</string>

<string>tel</string>

</array>


This piece of code in objective c works.

NSString *phoneNumber = @"telprompt:0426155885";
    NSURL *url = [NSURL URLWithString:phoneNumber];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    }


This piece of code in swift DOESTN WORK!

let urlString = "telprompt:0426155885";
let url = NSURL(fileURLWithPath: urlString);
if UIApplication.sharedApplication().canOpenURL(url) {
      UIApplication.sharedApplication().openURL(url);
}

the error say


-canOpenURL: failed for URL: "telprompt:0426155885 -- file:///" - error: "This app is not allowed to query for scheme file


They are the in the same project in 2 different controller. Im slowly migrating my code to swift.


The question is why doesnt work in Swift? and how to make it work?

Answered by junkpile in 100645022

Point taken. You're right that the "tel:" scheme doesn't require slashes. Sorry about that. "telprompt" is undocumented.


Anyway I think the underlying problem is the use of NSURL(fileURLWithPath:) instead of NSURL(string:).

It's not a file URL; it's a regular URL. You're using the wrong NSURL constructor. And the string you have there is not a valid URL. It is missing the slashes. URLs start with "scheme colon slash slash..."

(The forums force converting mailto: URLs to a link, so rewritten...)

URLs start with "scheme colon slash slash..."

Whether requiring double slashes or not, depends on the scheme. For example mailto: URL does not require double slashes.

Accepted Answer

Point taken. You're right that the "tel:" scheme doesn't require slashes. Sorry about that. "telprompt" is undocumented.


Anyway I think the underlying problem is the use of NSURL(fileURLWithPath:) instead of NSURL(string:).

the underlying problem is the use of NSURL(fileURLWithPath:) instead of NSURL(string:).

Surely.

Yes


Thank you very much.


Im too quick to blame the API 😝


I should have double check it.

Unable to open phone application in IOS 9 swift
 
 
Q