BalestraPatrick BalestraPatrick - 6 months ago 26
Swift Question

NSURLComponents builds scheme without //

I'm trying to create an URL scheme to open the Tweetbot app in this way.

var urlComponents = NSURLComponents()
urlComponents.scheme = "tweetbot"
urlComponents.path = "BalestraPatrick/user_profile/\(user)"


My expected result would be:
tweetbot://BalestraPatrick/user_profile/username


The result is instead:
tweetbot:BalestraPatrick/user_profile/username


The // are not added automatically. If I try to add the characters to the scheme or to the path, the URL becomes nil.

Any idea for a workaround?

Answer

The // won't appear if you don't specify host. I think the ideal solution would be:

let user = "test"

var urlComponents = NSURLComponents()
urlComponents.scheme = "tweetbot"
urlComponents.host = "BalestraPatrick"
urlComponents.path = "/user_profile/\(user)"

print("URL: \(urlComponents.string!)") // URL: tweetbot://BalestraPatrick/user_profile/test