How can I open the Messages app to the compose screen, with the message body preloaded with specific text?
Asked
Active
Viewed 1,178 times
6
-
@RMenke I checked if MessageUI was available for watchOS (it isn't), and looked for any frameworks that might be the equivalent, but came up empty. – Benjy Wiener Dec 20 '15 at 00:20
-
whereEver you are triggering this from, can you invoke opening of this url: sms:123-456-8900, but I don't think you can populate a message!https://developer.apple.com/library/watchos/featuredarticles/iPhoneURLScheme_Reference/SMSLinks/SMSLinks.html – Jatin Dec 24 '15 at 15:39
-
@Jatin 1) I do not believe you can open links on Apple Watch (UIApplication does not seem to be available). 2) I know it's possible to populate the message, baca use I have seen it done. – Benjy Wiener Dec 24 '15 at 15:47
-
https://discussions.apple.com/thread/7370937?start=0&tstart=0, it crashes accordign to this user, lets dig some documentation.....how else are you populating then, there has to be a urlscheme or some new API's unknown to us – Jatin Dec 24 '15 at 16:02
-
i was right found it: https://developer.apple.com/library/watchos/documentation/WatchKit/Reference/WKExtension_class/index.html#//apple_ref/occ/instm/WKExtension/openSystemURL: – Jatin Dec 24 '15 at 16:07
-
this is how you open messages app: func openSystemURL(_ url: NSURL) your NSURL will be sms:234234342 – Jatin Dec 24 '15 at 16:14
2 Answers
7
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)
}

Community
- 1
- 1
0
Thanks to @Jatin for finding the openSystemURL(url: NSURL)
function.
Here's the code:
let messageBody = "Hello World!"
let urlSafeBody = messageBody.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
WKExtension.sharedExtension().openSystemURL(NSURL(string: "sms:&body=\(urlSafeBody)")!)

Benjy Wiener
- 1,085
- 2
- 9
- 27
-
I'm sure it is something dumb, but I am not getting this code to work. When I execute it, it shows this error: fatal error: unexpectedly found nil while unwrapping an Optional value. How is urlSafeBody nil?! – Charlie Apr 11 '16 at 14:58
-
1It's not `urlSafeBody` that is nil. You're force-unwrapping `NSURL` which happens to be nil because its URL string is malformed. See [my answer](http://stackoverflow.com/a/36567031/4151918) for details. – Apr 12 '16 at 08:00
-
-
Swift 5.0 let urlSafeBody = msgBody.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) if let urlSafeBody = urlSafeBody, let url = NSURL(string: "sms:&body=\(urlSafeBody)") { WKExtension.shared().openSystemURL(url as URL) } – PlusInfosys Jan 28 '21 at 13:11