示例代码:
NoClassDefFoundError:org/mockito/exceptions/Reporter
添加到.plist文件中
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.
}
}
}
面对问题是与&
之后的&文本共享链接时对于上面的url文本,例如 http:// Hello / site / link?id = MTk =
预先感谢
答案 0 :(得分:1)
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
答案 1 :(得分:0)
尝试直接初始化为 URL 而不是 NSURL
let whatsappURL = URL(string: urlString)
,也许可以使用完成处理程序进行进一步调试
UIApplication.shared.open(whatsappURL, options: [:], completionHandler: nil)
答案 2 :(得分:0)
经过研究和测试,我发现在.urlQueryAllowed
中使用 AllowedCharacters
时,将允许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)
}
}
}