我使用这些代码在 what's app 中共享应用程序链接,但 whatsapp 的文本字段中没有任何内容。如果使用简单的文本那么它的工作。任何人都可以建议最终结果。
NSString *theTempMessage = @"whatsapp://send?text=https://itunes.apple.com/in/app/myapp/id1054375332?ls=1&mt=8";
NSString *theFinalMessage;
theTempMessage = [theTempMessage stringByReplacingOccurrencesOfString:@":" withString:@"%3A"];
theTempMessage = [theTempMessage stringByReplacingOccurrencesOfString:@"/" withString:@"%2F"];
theTempMessage = [theTempMessage stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"];
theTempMessage = [theTempMessage stringByReplacingOccurrencesOfString:@"," withString:@"%2C"];
theTempMessage = [theTempMessage stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"];
theFinalMessage = [theTempMessage stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];
NSString * stringToSend=theFinalMessage;
NSURL *whatsappURL = [NSURL URLWithString:stringToSend];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL])
{
[[UIApplication sharedApplication] openURL: whatsappURL];
}
最佳答案
将此添加到您的 Info.plist
<key>LSApplicationQueriesSchemes</key>
<array>
<string>whatsapp</string>
</array>
将此代码实现到您需要打开 WhatsApp 进行共享的 ViewController。 (例如说一个按钮 Action ) swift 3 版本 (Xcode 8.x) 的更新:针对弃用进行了更新:
var str = "This is the string which you want to share to WhatsApp"
str=str.addingPercentEncoding(withAllowedCharacters: (NSCharacterSet.urlQueryAllowed))!
let whatsappURL = URL(string: "whatsapp://send?text=\(str)")
if UIApplication.shared.canOpenURL(whatsappURL) {
UIApplication.shared.open(whatsappURL!, options: [:], completionHandler: nil)
} else {
showAlert(message: "Whatsapp is not installed on this device. Please install Whatsapp and try again.")
}
这里的 showAlert() 是一个用于显示警报的自定义函数。
关于ios - 使用 whatsapp 分享链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34037066/