TLDR 当我将电话号码硬编码到一个URL时,它会在正确的监视消息中打开,但是当我使用一个变量字符串时,其中的数字输入方式完全相同,它不会&# 39;吨。
示例:
NSURL(string: "sms:/open?addresses=8888888888,9999999999,3333333333&body=Test")
以上代码有效,但代码如下:
let hardCode = "8888888888,9999999999,3333333333"
NSURL(string: "sms:/open?addresses=\(hardCode)&body=Test")
详细信息: 我正在使用变量创建一个URL,以便在预装内容的Apple Watch上打开消息。我从联系簿中获取电话号码并将其存储在阵列中。它们以这种格式提供:
(###)### - ####但需要##########
我通过将电话号码硬编码到网址中来测试代码,它可以与所有联系人和已完成的正文一起正常运行:
if let urlSafeBody = urlSafeBody, url = NSURL(string: "sms:/open?addresses=8888888888,9999999999,3333333333&body=\(urlSafeBody)") {
print("FINAL URL: \(url)")
WKExtension.sharedExtension().openSystemURL(url)
}
但是当我以编程方式构建电话号码值时,它不起作用:
//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
}
}
然后我添加" 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)
}
FINAL URL打印显示正确的字符串,但消息应用程序无法正常打开,并显示快速回复菜单而不是组合消息窗口。它完全匹配功能硬编码数字版本,但行为不同。
完全迷失了,希望有人可以帮忙!
更新1 以下是两个版本的URL的调试打印:
手动声明(不是从recipString创建,而是在URL字符串中明确声明):
此版本有效
最终网址:短信:/ open?地址= 0000000000,1111111111,2222222222,3333333333,4444444444& body = test
创建变量(使用recipString):
这个版本没有
最终网址:短信:/ open?地址= 0000000000,1111111111,2222222222,3333333333,4444444444& body = test
我也尝试将url编码应用于" recipString"如果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)
}
}
更新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")
}
调试打印:
hardCode匹配recipString
更新3
我已经确认:
当使用硬编码数字和我从变量中生成的数字创建URL时,检查它们之间的==会返回true。
在每个测试中,我可以在两个版本的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)
}
}
}
}
答案 0 :(得分:2)
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.