I have an app that generates a small amount of information in a WatchKit app. I then want to hand the user off to Safari on another device to pursue more information at a website, with specific information in a url query string. I've written this:
var QueryVar = SomeArray.description
var website = NSURL(string: "http://www.example.com/index.html?query="+QueryVar)
updateUserActivity("com.example.myapp.anactivity", userInfo: nil, webpageURL: website)
When I println(website)
it comes up nil
, and the console warns that both userInfo
and webpageURL
cannot be nil
. I've tried a number of different ways of formatting that URL as a string, nothing seems to work.
The Swift documentation is good but lacks examples.
Most of the examples out there don't bother with the webpageURL
element.
What's worse is even if I get this to not generate an error message, I won't be sure that it works because webpageURL
isn't supported in the IOS simulator. Does anyone have some working code, ideally tested on an actual watch, they can share?
Thanks to Schemetrical who pointed out my error in thinking that
QueryVar.description
would yield only the elements of an array stringified. As that variable contained brackets and commas it was illegal content for an NSURL type. I cleaned up the code and got to a working solution:
var URLwithQuery = "http://www.example.com?query="+QueryVar
var urlPath: String = URLwithQuery
var website: NSURL = NSURL(string: urlPath)!
updateUserActivity("com.example.myapp.anactivity", userInfo: nil, webpageURL: website)