WhatsApp NSURL 在共享文本时返回 nil - Swift
WhatsApp NSURL returning nil When sharing text - Swift
我正在尝试与 WhatsApp 共享文本,但 NSURL 始终返回 nil,但文本编码正确!看看我的代码:
var msg : NSString = "to the world of none";
var titlewithoutspace = msg.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
var urlWhats = NSString(string: "whatsapp://send?text=\(titlewithoutspace)")
println(urlWhats)
var whatsappURL = NSURL(string: urlWhats as String)
println(whatsappURL)
当我打印结果时,字符串等于:
whatsapp://send?text=Optional("to%20the%20world%20of%20none")
whatsappURL 总是返回 nil:
nil
使用 \(...)
应保留用于写入控制台。如您所见,它在您的字符串周围添加了 Optional(....)
并且在 URL.
中是无效的
强制解包 "whatsapp://send?text=\(titlewithoutspace!)"
或更好,使用
String(format: "whatsapp://send?text=%@", titlewithoutspace)
stringByAddingPercentEscapesUsingEncoding:
returns 一个可选的 String
这就是 urlWhats
包含 Optional("")
的原因。为避免这种情况,您只需要像这样打开可选的:
var msg: NSString = "to the world of none";
var titlewithoutspace = msg.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
if let titlewithoutspace = titlewithoutspace {
var urlWhats = NSString(string: "whatsapp://send?text=\(titlewithoutspace)")
var whatsappURL = NSURL(string: urlWhats as String)
println(whatsappURL)
} else {
// Unwrapping failed because titlewithoutspace is nil (might be because stringByAddingPercentEscapesUsingEncoding failed).
}
此外,我建议您直接使用 String 类型,因为 NSString
对您来说毫无用处(stringByAdding…
除外):
var msg: NSString = "to the world of none";
var titlewithoutspace = msg.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
if let titlewithoutspace = titlewithoutspace {
var urlWhats = "whatsapp://send?text=\(titlewithoutspace)"
var whatsappURL = NSURL(string: urlWhats)
println(whatsappURL)
} else {
// Unwrapping failed because titlewithoutspace is nil (might be because stringByAddingPercentEscapesUsingEncoding failed).
}
另请注意,NSURL(string:)
可能会失败,因此它也是 returns 一个可选的 NSURL
对象。要使用它,您可能需要像我对 titlewithoutspace
.
所做的那样打开它
对于Swift 2.0/2.1
func openURL(urlString: String, message: String) {
var formattedURL: String = urlString
if let strippedMessage = message.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet()) {
formattedURL = "\(urlString)\(strippedMessage)"
}
if let url = NSURL(string: formattedURL) as NSURL? {
print(url)
UIApplication.sharedApplication().openURL(url)
}
}
这是swift
中经过测试的分享代码
let url = NSURL(string: "whatsapp://send?text=https%3A%2F%2Fitunes.apple.com%2Fin%2Fapp%2Fwhatsapp-messenger%2Fid310633997%3Fmt%3D8")
if UIApplication.sharedApplication().canOpenURL(url!) {
UIApplication.sharedApplication().openURL(url!)
}
else {
let errorAlert = UIAlertView(title: "Cannot Send Message", message: "Your device is not able to send WhatsApp messages.", delegate: self, cancelButtonTitle: "OK")
errorAlert.show()
}
注意:您只需将 url 转换为编码格式
我正在尝试与 WhatsApp 共享文本,但 NSURL 始终返回 nil,但文本编码正确!看看我的代码:
var msg : NSString = "to the world of none";
var titlewithoutspace = msg.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
var urlWhats = NSString(string: "whatsapp://send?text=\(titlewithoutspace)")
println(urlWhats)
var whatsappURL = NSURL(string: urlWhats as String)
println(whatsappURL)
当我打印结果时,字符串等于:
whatsapp://send?text=Optional("to%20the%20world%20of%20none")
whatsappURL 总是返回 nil:
nil
使用 \(...)
应保留用于写入控制台。如您所见,它在您的字符串周围添加了 Optional(....)
并且在 URL.
强制解包 "whatsapp://send?text=\(titlewithoutspace!)"
或更好,使用
String(format: "whatsapp://send?text=%@", titlewithoutspace)
stringByAddingPercentEscapesUsingEncoding:
returns 一个可选的 String
这就是 urlWhats
包含 Optional("")
的原因。为避免这种情况,您只需要像这样打开可选的:
var msg: NSString = "to the world of none";
var titlewithoutspace = msg.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
if let titlewithoutspace = titlewithoutspace {
var urlWhats = NSString(string: "whatsapp://send?text=\(titlewithoutspace)")
var whatsappURL = NSURL(string: urlWhats as String)
println(whatsappURL)
} else {
// Unwrapping failed because titlewithoutspace is nil (might be because stringByAddingPercentEscapesUsingEncoding failed).
}
此外,我建议您直接使用 String 类型,因为 NSString
对您来说毫无用处(stringByAdding…
除外):
var msg: NSString = "to the world of none";
var titlewithoutspace = msg.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
if let titlewithoutspace = titlewithoutspace {
var urlWhats = "whatsapp://send?text=\(titlewithoutspace)"
var whatsappURL = NSURL(string: urlWhats)
println(whatsappURL)
} else {
// Unwrapping failed because titlewithoutspace is nil (might be because stringByAddingPercentEscapesUsingEncoding failed).
}
另请注意,NSURL(string:)
可能会失败,因此它也是 returns 一个可选的 NSURL
对象。要使用它,您可能需要像我对 titlewithoutspace
.
对于Swift 2.0/2.1
func openURL(urlString: String, message: String) {
var formattedURL: String = urlString
if let strippedMessage = message.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet()) {
formattedURL = "\(urlString)\(strippedMessage)"
}
if let url = NSURL(string: formattedURL) as NSURL? {
print(url)
UIApplication.sharedApplication().openURL(url)
}
}
这是swift
中经过测试的分享代码let url = NSURL(string: "whatsapp://send?text=https%3A%2F%2Fitunes.apple.com%2Fin%2Fapp%2Fwhatsapp-messenger%2Fid310633997%3Fmt%3D8")
if UIApplication.sharedApplication().canOpenURL(url!) {
UIApplication.sharedApplication().openURL(url!)
}
else {
let errorAlert = UIAlertView(title: "Cannot Send Message", message: "Your device is not able to send WhatsApp messages.", delegate: self, cancelButtonTitle: "OK")
errorAlert.show()
}
注意:您只需将 url 转换为编码格式