通过WhatsApp URL方案将URL作为文本传递

时间:2015-08-21 13:57:42

标签: ios swift whatsapp url-scheme

我正在尝试通过Whatsapp URL方案将Google地图网址作为文本传递。

我正在尝试编码,但我不认为我做得对。

let message = "View my current location at http://maps.google.com/?q=\(location.coordinate.latitude),\(location.coordinate.longitude)"

var completewhatsappURLString = whatsappURL + message

completewhatsappURLString = completewhatsappURLString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!

let completeURL = NSURL(string: completewhatsappURLString)

1 个答案:

答案 0 :(得分:0)

let mapsURL = "comgooglemaps://?q=\(location.coordinate.latitude),\(location.coordinate.longitude)"
let message = "View my current location at \(mapsURL)" as CFString
let encodedString: String = String(CFURLCreateStringByAddingPercentEscapes(nil, message, nil, "!*'();:@&=+$,/?%#[]" as CFString, CFStringBuiltInEncodings.UTF8.rawValue))
let completewhatsappURLString = "whatsapp://send?text=" + encodedString
let completeURL = NSURL(string: completewhatsappURLString)
if (UIApplication.sharedApplication().canOpenURL(completeURL!)) {
   UIApplication.sharedApplication().openURL(completeURL!)
}