AppleWatch Messages URL可以硬编码,但不能使用变量

Cha*_*lie 6 nsurl swift apple-watch

TLDR当我将电话号码硬编码到URL中时,它会正确地在监视消息中打开,但是当我使用变量字符串时,其中输入的数字完全相同,但事实并非如此.

例:

 NSURL(string: "sms:/open?addresses=8888888888,9999999999,3333333333&body=Test")
Run Code Online (Sandbox Code Playgroud)

上面的代码有效但下面的代码没有:

 let hardCode = "8888888888,9999999999,3333333333"
 NSURL(string: "sms:/open?addresses=\(hardCode)&body=Test")
Run Code Online (Sandbox Code Playgroud)

详细信息: 我正在使用变量创建一个URL,以便在预装内容的Apple Watch上打开消息.我从联系簿中获取电话号码并将其存储在阵列中.它们以这种格式提供:

(###)### - ####但需要##########

我通过将电话号码硬编码到URL中来测试代码,并且它适用于所有联系人和已完成的正文:

if let urlSafeBody = urlSafeBody, url = NSURL(string: "sms:/open?addresses=8888888888,9999999999,3333333333&body=\(urlSafeBody)") {
        print("FINAL URL: \(url)")
        WKExtension.sharedExtension().openSystemURL(url)
    }
Run Code Online (Sandbox Code Playgroud)

但是,当我以编程方式构建电话号码值时,它不起作用:

//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
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我在下面的代码中将"recipString"变量添加到NSURL:

    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)
    }
Run Code Online (Sandbox Code Playgroud)

FINAL URL打印显示正确的字符串,但消息应用程序无法正常打开,并显示快速回复菜单而不是组合消息窗口.它完全匹配功能硬编码数字版本,但行为不同.

完全迷失了,希望有人可以帮忙!

更新1 以下是两个版本的URL的调试打印:

手动声明(不是从recipString创建,而是在URL字符串中明确声明):

这个版本有效

最终网址:短信:/开?地址= 0000000000,1111111111,2222222222,3333333333,4444444444&body = test

变量创建(使用recipString):

这个版本没有

最终网址:短信:/开?地址= 0000000000,1111111111,2222222222,3333333333,4444444444&body = test

我还尝试通过使用下面的if来将url编码应用于"recipString"变量:

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)
        }
    }
Run Code Online (Sandbox Code Playgroud)

更新2 我通过以下代码测试了硬编码版本的数字是否与recipString完全匹配:

    let hardCode = "0000000000,1111111111,2222222222,3333333333,4444444444"

    let isEqual = (hardCode == recipString)

    if isEqual {
        print("hardCode matches recipString")
    }
    else {
        print("hardCode does not match recipString")
    }
Run Code Online (Sandbox Code Playgroud)

调试打印:

hardCode匹配recipString

更新3

我已经确认:

当使用硬编码数字和我从变量生成的数字创建URL时,检查它们之间的==返回true.

在每个测试中,我可以在两个版本的url之间进行匹配.

发现正确答案后的说明:

这种类型的URL格式化仅适用于URL中的多个地址.如果您没有多个地址,则需要执行以下操作,这些操作未记录,但不会少.我发现这是通过在键盘上砸我的脸几个小时,所以如果它帮助你upvote是值得的:)

按照下面标记的答案,然后使用这种类型的逻辑检查,然后在他提到的doItButton()函数中创建URL:

    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)
            }
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

Mik*_*man 2

我的猜测是,openSystemUrl问题不在于实际的调用。我相信代码中一定存在以编程方式构建数字字符串的内容。

下面的代码是您发布的所有代码的简化版本。我已经确认它可以在我的 Apple Watch 上运行。它会打开带有预先填充的数字和正文的消息应用程序。

再看一下您的代码,看看是否缺少某些内容。如果你找不到任何东西,只需删除代码并重新编写它,可能会比发现奇怪的问题更快。

再次确认下面的代码可以按预期工作,因此您应该能够让它工作。(或者只是复制并粘贴我的代码):)

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
  }
}
Run Code Online (Sandbox Code Playgroud)

如上所述,出于评论中已经提到的原因,我建议不要对您计划放在 App Store 上的任何内容使用未记录的 Apple 功能。