ios - 使用 whatsapp 分享链接

标签 ios objective-c whatsapp

我使用这些代码在 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/

相关文章:

ios - UITableView 在禁用滚动的情况下动态增加高度?

ios - 如何将配置文件添加到包 ID 列表中

ios - 构建在下载 pod 时删除实现 (.m) 文件的 Pod 规范

ios - 以编程方式选择显示在 UIScrollView 中的当前按钮

ios - 使用 UITableView 与 UIcollectionView (iOS) 聊天应用程序?

android - Whatsapp 注册后不显示登录屏幕

ios - Swift:通用应用程序上的 Split View Controller 不是初始 View Controller

ios - 如何将 UILabel 文本颜色更改回程序首次在 Xcode for iOS 7 中启动时的颜色?

Android 画廊选择像 Whatsapp

ios - 为表格标题 View 设置图像