分享链接使用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]; } 

以下错误显示检查canOpenURL

url失败:“whatsapp://” – 错误:此应用程序不允许查询schemewhatsapp

在iOS 9中,您必须将您的应用程序想要在Info.plist中查询的任何URLscheme列入LSApplicationQueriesSchemes项(string数组):

在这里输入图像说明

通过Info.plist中包含的scheme,一切都像以前一样工作。 当你链接到iOS 9时,你不仅限于50个不同的scheme,你只需要在Info.plist中声明你需要的东西。 对于您可以包含多less计划似乎没有限制,但是如果他们认为您滥用了这种机制,我会期待App Store审查小组提出的问题。

请注意,此机制仅适用于canOpenURL而不适用于openURL。 您不需要在Info.plist中列出可以使用openURL打开的scheme。

 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]; } 

这是WWDC 2015的官方应用安全video。

如果您使用“ [[UIApplication sharedApplication] openURL:whatsappURL]; ”在stringreplcement之后它将打开safari浏览器而不是whatsapp,

如果你想打开whatsapp不replacestring

将此添加到您的Info.plist

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

将此代码实现到您需要打开WhatsApp以供共享的ViewController。 (例如说一个button操作)更新为swift 3版本(Xcode 8.x): 更新为deprecations

 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()是一个显示警报的自定义函数。