Zonily Jame
Zonily Jame

Reputation: 5359

Using a url scheme on NSURL() results to nil

Language Used: Swift 2.3

Viber's url scheme for sending a message is viber://forward?text=

Whatsapp's url scheme for sending a message is whatsapp://send?text=

The problem is NSURL seems to think that url's that don't have . on them are not urls because this code seems to result to nil

let someString = "This is some kind of long string"
print(NSURL(string: "viber://forward?text=\(someString)"))

print(NSURL(string: "viber://whatsapp://send=\(someString)"))

This results to a log which looks like this.

nil
nil

Which means I can't use UIApplication.sharedApplication().openUrl(someUrl)

Upvotes: 0

Views: 773

Answers (1)

Zonily Jame
Zonily Jame

Reputation: 5359

There are some characters that can't be placed inside a URL/NSURL.

You'd have to parse someString to replace these characters using this String extension

stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())

For example:

var someString = "This is some kind of long string"

someString = someString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())

Upvotes: 1

Related Questions