link 中包含“&”时 WhatsApp 共享无法正常工作
WhatsApp share not working properly while "&" is contain in link
示例代码:
let sampleURL = "http://Hello/site/link?id=MTk=&fid=MTA="
let urlWhats = "whatsapp://send?text=\(sampleURL)"
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
if let whatsappURL = NSURL(string: urlString) {
if UIApplication.shared.canOpenURL(whatsappURL as URL) {
UIApplication.shared.open(whatsappURL as URL)
}
else {
// Open App Store.
}
}
}
添加到 .plist 文件
<key>LSApplicationQueriesSchemes</key>
<array>
<string>whatsapp</string>
</array>
面临的问题是在与 & 分享 link 时文本没有出现在 &
之后
对于以上 url 文本即将出现 http://Hello/site/link?id=MTk=
提前致谢
尝试直接初始化为 URL 而不是 NSURL
let whatsappURL = URL(string: urlString)
并可能使用完成处理程序进一步调试
UIApplication.shared.open(whatsappURL, options: [:], completionHandler: nil)
经过一些研究和测试后,我发现在 AllowedCharacters
中使用 .urlQueryAllowed
将允许 URL 并且在 URL 这就是为什么会出现这个问题。
只需在您的文本中使用 .init()
,而不包含在该编码中 whatsapp://send?text=
如下图
let sampleURL = "http://Hello/site/link?id=MTk=&fid=MTA="
if let urlString = sampleURL.addingPercentEncoding(withAllowedCharacters: .init()) {
let urlWhats = "whatsapp://send?text=\(urlString)"
if let whatsappURL = NSURL(string: urlWhats) {
if UIApplication.shared.canOpenURL(whatsappURL as URL) {
UIApplication.shared.open(whatsappURL as URL)
}
}
}
一个URL的不同部分有不同的编码规则。 addingPercentEncoding
很难正确使用。通常,您不应尝试对自己的 URL 进行编码。让系统为你做 URLComponents:
//For the constant part of the link, it's fine to just use a string
var sample = URLComponents(string: "whatsapp://send")!
// Then add a query item
sample.queryItems = [URLQueryItem(name: "text", value: "http://Hello/site/link?id=MTk=&fid=MTA=")]
// Extract the URL, which will have the correct encoding
print(sample.url!)
// whatsapp://send?text=http://Hello/site/link?id%3DMTk%3D%26fid%3DMTA%3D
示例代码:
let sampleURL = "http://Hello/site/link?id=MTk=&fid=MTA="
let urlWhats = "whatsapp://send?text=\(sampleURL)"
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
if let whatsappURL = NSURL(string: urlString) {
if UIApplication.shared.canOpenURL(whatsappURL as URL) {
UIApplication.shared.open(whatsappURL as URL)
}
else {
// Open App Store.
}
}
}
添加到 .plist 文件
<key>LSApplicationQueriesSchemes</key>
<array>
<string>whatsapp</string>
</array>
面临的问题是在与 & 分享 link 时文本没有出现在 &
之后对于以上 url 文本即将出现 http://Hello/site/link?id=MTk=
提前致谢
尝试直接初始化为 URL 而不是 NSURL
let whatsappURL = URL(string: urlString)
并可能使用完成处理程序进一步调试
UIApplication.shared.open(whatsappURL, options: [:], completionHandler: nil)
经过一些研究和测试后,我发现在 AllowedCharacters
中使用 .urlQueryAllowed
将允许 URL 并且在 URL 这就是为什么会出现这个问题。
只需在您的文本中使用 .init()
,而不包含在该编码中 whatsapp://send?text=
如下图
let sampleURL = "http://Hello/site/link?id=MTk=&fid=MTA="
if let urlString = sampleURL.addingPercentEncoding(withAllowedCharacters: .init()) {
let urlWhats = "whatsapp://send?text=\(urlString)"
if let whatsappURL = NSURL(string: urlWhats) {
if UIApplication.shared.canOpenURL(whatsappURL as URL) {
UIApplication.shared.open(whatsappURL as URL)
}
}
}
一个URL的不同部分有不同的编码规则。 addingPercentEncoding
很难正确使用。通常,您不应尝试对自己的 URL 进行编码。让系统为你做 URLComponents:
//For the constant part of the link, it's fine to just use a string
var sample = URLComponents(string: "whatsapp://send")!
// Then add a query item
sample.queryItems = [URLQueryItem(name: "text", value: "http://Hello/site/link?id=MTk=&fid=MTA=")]
// Extract the URL, which will have the correct encoding
print(sample.url!)
// whatsapp://send?text=http://Hello/site/link?id%3DMTk%3D%26fid%3DMTA%3D