使用whatsapp分享链接

时间:2015-12-02 07:26:17

标签: ios objective-c whatsapp

我在应用程序中使用这些代码进行共享应用程序链接,但是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];
}

3 个答案:

答案 0 :(得分:43)

检查canOpenURL时显示以下错误

  

网址失败:“whatsapp://” - 错误:此应用不允许查询方案whatsapp

在iOS 9中,您必须将您的应用程序想要在LSApplicationQueriesSchemes键(字符串数组)下的Info.plist中查询的任何URL方案列入白名单:

enter image description here

Info.plist中包含的方案一切正常。当您链接到iOS 9时,您不仅限于50个不同的方案,您只需要在Info.plist中声明您需要的内容。您可以包含多少方案似乎没有限制,但如果他们认为您滥用该机制,我会期待App Store审核小组提出的问题。

  

请注意,此机制仅适用于canOpenURL而不适用于openURL。   您无需在Info.plist中列出方案即可   用openURL打开它。

NSString * msg = @"Application%20Name%20https://itunes.apple.com/in/app/myapp/id1054375332?ls=1&mt=8";

msg = [msg stringByReplacingOccurrencesOfString:@":" withString:@"%3A"];
msg = [msg stringByReplacingOccurrencesOfString:@"/" withString:@"%2F"];
msg = [msg stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"];
msg = [msg stringByReplacingOccurrencesOfString:@"," withString:@"%2C"];
msg = [msg stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"];
msg = [msg stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];

NSString * urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=%@",msg];
NSURL * whatsappURL = [NSURL URLWithString:urlWhats];

if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
    [[UIApplication sharedApplication] openURL: whatsappURL];
} else {
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"WhatsApp not installed." message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

This是WWDC 2015的应用安全官方视频。

答案 1 :(得分:7)

将此添加到您的Info.plist

<key>LSApplicationQueriesSchemes</key>
    <array>
        <string>whatsapp</string>
    </array>

将此代码实现到ViewController,您需要打开WhatsApp进行共享。 (例如说按钮动作) 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()是一个用于显示警报的自定义函数。

答案 2 :(得分:1)

如果您使用&#34; [[UIApplication sharedApplication] openURL:whatsappURL]; &#34;在字符串重复后,它将打开safari浏览器而不是whatsapp,

如果你想打开whatsapp,请不要替换字符串