TL; DR . When I hard code the phone numbers in the URL, it opens correctly in the form of messages, but when I use a variable string with numbers entered exactly the same as inside it, i.e.
Example:
NSURL(string: "sms:/open?addresses=8888888888,9999999999,3333333333&body=Test")
It works on the code, but there is no code below:
let hardCode = "8888888888,9999999999,3333333333" NSURL(string: "sms:/open?addresses=\(hardCode)&body=Test")
FULL DETAILS: I am making a URL out of variables to open messages in Apple Watch with pre-populated content. I get phone numbers from the contact book and store them in an array. They are presented in this format:
(###) ### - ####, but must be ##########
I tested the code using hardcoded phone numbers in the url and it works correctly with all contacts and the completed body:
if let urlSafeBody = urlSafeBody, url = NSURL(string: "sms:/open?addresses=8888888888,9999999999,3333333333&body=\(urlSafeBody)") { print("FINAL URL: \(url)") WKExtension.sharedExtension().openSystemURL(url) }
But when I randomly create phone numbers, this does not work:
//holds phone numbers without special chars var tempArray: [String] = [] //if I can access the unformatted numbers if let recips = saveData["recips"] as? [String] { //for each number provided recips.forEach { (person: String) in //remove all non-numerical digits //person is now (-
Then I add the variable "recipString" to NSURL in the following code:
let messageBody = "test" let urlSafeBody = messageBody.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLHostAllowedCharacterSet()) if let urlSafeBody = urlSafeBody, url = NSURL(string: "sms:/open?addresses=\(recipString)&body=\(urlSafeBody)") { print("FINAL URL: \(url)") WKExtension.sharedExtension().openSystemURL(url) }
The FINAL URL printout shows the correct line, but the message application does not open properly and displays the quick reply menu instead of the configured message window. It exactly matches the functioning version with a hard coded number, but it behaves differently.
Totally lost, hope someone can help!
UPDATE 1 Here are the debugging fingerprints for both versions of the URL:
Manually declared (not created from recipString, but explicitly declared in the URL string explicitly):
This version works
FINAL URL: sms: / open? addresses = 0000000000,1111111111,2222222222,3333333333,4444444444 & body = test
Created variable (using recipString):
This version is not
FINAL URL: sms: / open? addresses = 0000000000,1111111111,2222222222,3333333333,4444444444 & body = test
I also tried applying url encoding to the variable "recipString", using below if let:
if let urlSafeRecip = recipString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()) { if let urlSafeBody = urlSafeBody, url = NSURL(string: "sms:/open?addresses=\(urlSafeRecip)&body=\(urlSafeBody)") { print("FINAL URL: \(url)") WKExtension.sharedExtension().openSystemURL(url) } }
UPDATE 2 I tested to check whether the version of the hardcode number matches the recipString method through this code:
let hardCode = "0000000000,1111111111,2222222222,3333333333,4444444444" let isEqual = (hardCode == recipString) if isEqual { print("hardCode matches recipString") } else { print("hardCode does not match recipString") }
Debug prints:
hardCode matches recipString
UPDATE 3
I have confirmed that:
When a URL is created with hard-coded numbers or the numbers I make from variables, checking == between them returns true.
In each test, I can make between two versions of the URL it matches.
NOTES AFTER USER FROM THE CORRECT RESPONSE:
This type of URL formatting will ONLY work with multiple URL addresses. If you do not have multiple addresses, you will need to do the following, which is undocumented, but nothing works. I found this by breaking my face on the keyboard for several hours, so if this helps you get the edge :)
follow the answer below and then use this type of boolean check before doing the url in the doItButton () function that he mentioned:
func setupAndSendMsg(saveData: NSDictionary) { if let urlSafeBody = createBody(saveData) { let theNumbers = createNumbers(saveData).componentsSeparatedByString(",") print(theNumbers.count-1) if theNumbers.count-1 > 0 { if let url = NSURL(string: "sms:/open?addresses=\(createNumbers(saveData))&body=\(urlSafeBody)") { print(url) WKExtension.sharedExtension().openSystemURL(url) } } else { if let url = NSURL(string: "sms:/open?address=\(createNumbers(saveData)),&body=\(urlSafeBody)") { print(url) WKExtension.sharedExtension().openSystemURL(url) } } } }