AppleWatch Post URL Works Hard But Not With Variables

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 (###) ###-#### let newPerson = person.digitsOnly() //newPerson is ########## print(person) print("->\(newPerson)") //add formatted number to tempArray tempArray.append(newPerson) } } //combine all numbers with "," between as a string let recipString = tempArray.joinWithSeparator(",") //recipString contains ##########,##########,##########... extension String { func digitsOnly() -> String{ let stringArray = self.componentsSeparatedByCharactersInSet( NSCharacterSet.decimalDigitCharacterSet().invertedSet) let newString = stringArray.joinWithSeparator("") return newString } } 

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) } } } } 
+6
source share
1 answer

My hunch is that the problem is not in calling openSystemUrl . I believe there should be something with code that programs a number string.

The code below is a simplified version of all the code you posted. I confirmed that he is working on my Apple Watch. He opens the Messages application with pre-filled numbers and text.

Take another look at your code and see if there is something you are missing. If you can’t find anything, just delete the code and rewrite it will probably be faster than detecting a strange problem.

The newly confirmed code will work as expected, so you can get it to work. (or just copy and paste my code) :)

 class InterfaceController: WKInterfaceController { @IBAction func doItButton() { if let urlSafeBody = createBody() { if let url = NSURL(string: "sms:/open?addresses=\(createNumbers())&body=\(urlSafeBody)") { print(url) WKExtension.sharedExtension().openSystemURL(url) } } } private func createBody() -> String? { let messageBody = "hello test message" return messageBody.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLHostAllowedCharacterSet()) } private func createNumbers() -> String { let numbers = ["(111) 222-3333", "(444) 555-6666"] var tempArray: [String] = [] numbers.forEach { (number: String) in tempArray.append(number.digitsOnly()) } return tempArray.joinWithSeparator(",") } } extension String { func digitsOnly() -> String{ let stringArray = self.componentsSeparatedByCharactersInSet( NSCharacterSet.decimalDigitCharacterSet().invertedSet) let newString = stringArray.joinWithSeparator("") return newString } } 

With the above, I would recommend not using Apple's undocumented features for anything you plan to put on the App Store for the reasons already mentioned in the comments.

+2
source

All Articles