AppleWatch Messages URL works hard coded but not with variables

帅比萌擦擦* 提交于 2019-12-05 07:30:36

My guess is that it is not the acctual openSystemUrl call that is the problem. I believe there must be something with the code that is building the number string programmatically.

The code bellow is a simplified version of all the code you have posted. I have confirmed that it is working on my Apple Watch. It opens the Messages app with pre-populated numbers & body text.

Take one more look at your code and see if there is something your missing. If you can't find anything, just delete the code and re-write it, probably will be faster then spotting the weird issue.

Once again the code bellow is confirmed working as expected, so you should be able to get it to work. (or just copy & 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 above said I would recommend against using undocumented Apple features for anything you plan on putting on the App Store for the reasons already mentioned in comments.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!