Swift编码WhatsApp URL
我想在我的应用程序中打开WhatsApp URL,这是我在Swift中创建的。WhatsApp的URL方案是Swift编码WhatsApp URL,swift,Swift,我想在我的应用程序中打开WhatsApp URL,这是我在Swift中创建的。WhatsApp的URL方案是 whatsapp://send?text= 我的代码如下所示: let originalMessage = NSLocalizedString("Open following url ", comment: "") + " " + "http://<url>.<com>/?=test" let escapedMessage =
whatsapp://send?text=
我的代码如下所示:
let originalMessage = NSLocalizedString("Open following url ", comment: "") + " " + "http://<url>.<com>/?=test"
let escapedMessage = originalMessage.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLHostAllowedCharacterSet())!
// Check if url will be opened
guard let whatsAppUrl = NSURL(string: "whatsapp://send?text=" + escapedMessage) else {
print("error")
return
}
// Open whatsapp url
if UIApplication.sharedApplication().canOpenURL(whatsAppUrl) {
print("ok")
}
但是如果我在WhatsApp中打开URL,就会得到一个空字符串。有人能帮我或给我一个提示吗?尝试发送如下静态URL: 打开其中一个URL的Objective-C调用如下所示:
NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=Hello%2C%20World!"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
[[UIApplication sharedApplication] openURL: whatsappURL];
}
迅速:
let whatsappURL = NSURL(string:"whatsapp://send?text=Hello%2C%20World!")
if (UIApplication.sharedApplication().canOpenURL(whatsappURL!)) {
UIApplication.sharedApplication().openURL(whatsappURL!);
}
从链接:
尝试发送如下静态URL: 打开其中一个URL的Objective-C调用如下所示:
NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=Hello%2C%20World!"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
[[UIApplication sharedApplication] openURL: whatsappURL];
}
迅速:
let whatsappURL = NSURL(string:"whatsapp://send?text=Hello%2C%20World!")
if (UIApplication.sharedApplication().canOpenURL(whatsappURL!)) {
UIApplication.sharedApplication().openURL(whatsappURL!);
}
从链接:
我认为URLComponent是解决这个问题的最好方法
let whatsAppHookup = "whatsapp://send"
let address = "https://www.swift.org/?page=1"
var urlComponent = URLComponents(string: whatsAppHookup)
let queryItem = URLQueryItem(name: "text", value: address)
urlComponent?.queryItems = [queryItem]
let url = urlComponent?.url
print(url?.absoluteString ?? "")
我认为URL组件是解决这个问题的最好方法
let whatsAppHookup = "whatsapp://send"
let address = "https://www.swift.org/?page=1"
var urlComponent = URLComponents(string: whatsAppHookup)
let queryItem = URLQueryItem(name: "text", value: address)
urlComponent?.queryItems = [queryItem]
let url = urlComponent?.url
print(url?.absoluteString ?? "")
”whatsapp://send?text=\“+escapedMessage+”\“
永远不要这样做。像这样手工构造URL字符串总是错误的。使用NSURLComponents和NSURLQuery;这就是他们的目的。“whatsapp://send?text=\“+escapedMessage+”\“
永远不要这样做。像这样手工构造URL字符串总是错误的。使用NSURLComponents和NSURLQuery;这就是他们的目的。