How can I programmatically compose a message on Watch?
Benjy's answer is almost correct, but has one issue.
Since urlSafeBody
isn't unwrapped, string interpolation yields
sms:&body=Optional("Hello%20World!")
which is causing NSURL
initialization to return nil, since the URL string is malformed.
Here's a working example which conditionally unwraps optionals. This removes any possibility of crashes related to nil optionals being force-unwrapped.
let messageBody = "Hello World!"
let urlSafeBody = messageBody.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLHostAllowedCharacterSet())
if let urlSafeBody = urlSafeBody, url = NSURL(string: "sms:&body=\(urlSafeBody)") {
WKExtension.sharedExtension().openSystemURL(url)
}