我想在我的应用程序中使用 WhatsApp 给其他手机发送消息。我在 GitHub 上看到了这个公共 API,在这里。但是我没有找到适用于 iOS 的该 API。因此,是否可能在 iOS 应用程序中使用 WhatsApp?是否合法?在哪里可以找到适用于 iOS 的 WhatsApp 公共 API?
是的,你可以使用WhatsApp通过你的iOS应用程序发送文本/图片。 有两种方法可以这样做: 1. URL方案
NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=Hello%2C%20World!"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
[[UIApplication sharedApplication] openURL: whatsappURL];
}
您可以通过以下方式实现:
正如@NSAnant所提到的那样,如果您正在编写混合应用程序(Cordova,phonegap等),则使用Whatsapp FAQ中的URL Scheme创建一个简单的锚点即可解决问题(将WhatsApp集成到我的应用程序中)。
<a href="whatsapp://send?text=666isthenumberofthebeast" id="my_button" class="button_default button_color">Send Whatsapp Friendly Message</a>
在 iOS 9 中:您需要在 info.plist 中设置密钥
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fbauth2</string>
<string>fbshareextension</string>
<string>fb-messenger-api</string>
<string>twitter</string>
<string>whatsapp</string>
<string>wechat</string>
<string>line</string>
<string>instagram</string>
<string>kakaotalk</string>
<string>mqq</string>
<string>vk</string>
<string>comgooglemaps</string>
</array>
在设置密钥之后,以下代码可以在iOS 9中正常工作。
NSString * msg = @"mahesh";
NSString * urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=%@",msg];
NSURL * whatsappURL = [NSURL URLWithString:[urlWhats stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
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];
}
就是这么简单。希望能帮到你 ;-)
NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=Hello%20world"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
//WhatsApp is installed in your device and you can use it.
[[UIApplication sharedApplication] openURL: whatsappURL];
} else {
//WhatsApp is not installed or is not available
}
不行,这是不合法的。你不能在你的应用程序中使用WhatsApp。