在iOS应用中通过WhatsApp分享图片、文本或其他内容是否可行?我在谷歌上搜索,但只发现有关Android实现的结果。
现在可以使用这种方式:
发送文本 - Obj-C
NSString * msg = @"YOUR MSG";
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 {
// Cannot open whatsapp
}
发送文本 - Swift
let msg = "YOUR MSG"
let urlWhats = "whatsapp://send?text=\(msg)"
if let urlString = urlWhats.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()) {
if let whatsappURL = NSURL(string: urlString) {
if UIApplication.sharedApplication().canOpenURL(whatsappURL) {
UIApplication.sharedApplication().openURL(whatsappURL)
} else {
// Cannot open whatsapp
}
}
}
发送图片 - Obj-C
-- 在 .h 文件中
<UIDocumentInteractionControllerDelegate>
@property (retain) UIDocumentInteractionController * documentInteractionController;
-- 在 .m 文件中
if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"whatsapp://app"]]){
UIImage * iconImage = [UIImage imageNamed:@"YOUR IMAGE"];
NSString * savePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/whatsAppTmp.wai"];
[UIImageJPEGRepresentation(iconImage, 1.0) writeToFile:savePath atomically:YES];
_documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
_documentInteractionController.UTI = @"net.whatsapp.image";
_documentInteractionController.delegate = self;
[_documentInteractionController presentOpenInMenuFromRect:CGRectMake(0, 0, 0, 0) inView:self.view animated: YES];
} else {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"WhatsApp not installed." message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
发送图片 - Swift
let urlWhats = "whatsapp://app"
if let urlString = urlWhats.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()) {
if let whatsappURL = NSURL(string: urlString) {
if UIApplication.sharedApplication().canOpenURL(whatsappURL) {
if let image = UIImage(named: "image") {
if let imageData = UIImageJPEGRepresentation(image, 1.0) {
let tempFile = NSURL(fileURLWithPath: NSHomeDirectory()).URLByAppendingPathComponent("Documents/whatsAppTmp.wai")
do {
try imageData.writeToURL(tempFile, options: .DataWritingAtomic)
self.documentInteractionController = UIDocumentInteractionController(URL: tempFile)
self.documentInteractionController.UTI = "net.whatsapp.image"
self.documentInteractionController.presentOpenInMenuFromRect(CGRectZero, inView: self.view, animated: true)
} catch {
print(error)
}
}
}
} else {
// Cannot open whatsapp
}
}
}
由于iOS 9的新安全功能,您需要在.plist文件中添加以下行:<key>LSApplicationQueriesSchemes</key>
<array>
<string>whatsapp</string>
</array>
关于url方案的更多信息:https://developer.apple.com/videos/play/wwdc2015-703/
我没有找到一个适用于两个问题的解决方案。 有关更多信息,请参见http://www.whatsapp.com/faq/en/iphone/23559013
我制作了一个小项目来帮助一些人。 https://github.com/salesawagner/SharingWhatsApp
现在已经可能了。虽然还没有尝试过。
WhatsApp的最新版本说明显示,您可以通过共享扩展实现以下内容:
WhatsApp接受以下类型的内容:
不可能的,WhatsApp没有任何可以使用的公共API。
请注意,这个答案是针对2011年没有WhatsApp API时的情况。
现在有一个可用于与WhatsApp交互的API:http://www.whatsapp.com/faq/en/iphone/23559013
打开其中一个URL的Objective-C调用如下:
NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=Hello%2C%20World!"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
[[UIApplication sharedApplication] openURL: whatsappURL];
}
NSString * url = [NSString stringWithFormat:@"http://video...bla..bla.."];
url = (NSString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef) url, NULL,CFSTR("!*'();:@&=+$,/?%#[]"),kCFStringEncodingUTF8));
NSString * urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=%@",url];
NSURL * whatsappURL = [NSURL URLWithString:urlWhats];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
[[UIApplication sharedApplication] openURL: whatsappURL];
} else {
// can not share with whats app
}
简单代码和示例代码 ;-)
/*
//Share text
NSString *textToShare = @"Enter your text to be shared";
NSArray *objectsToShare = @[textToShare];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];
*/
//Share Image
UIImage * image = [UIImage imageNamed:@"images"];
NSArray *objectsToShare = @[image];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];
适用于Swift 4 - 完美运行
声明
var documentInteractionController:UIDocumentInteractionController!
func sharePicture() {
let urlWhats = "whatsapp://app"
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) {
if let whatsappURL = NSURL(string: urlString) {
if UIApplication.shared.canOpenURL(whatsappURL as URL) {
let imgURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let fileName = "yourImageName.jpg"
let fileURL = imgURL.appendingPathComponent(fileName)
if let image = UIImage(contentsOfFile: fileURL.path) {
if let imageData = image.jpegData(compressionQuality: 0.75) {
let tempFile = NSURL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Documents/yourImageName.jpg")
do {
try imageData.write(to: tempFile!, options: .atomicWrite)
self.documentInteractionController = UIDocumentInteractionController(url: tempFile!)
self.documentInteractionController.uti = "net.whatsapp.image"
self.documentInteractionController.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)
} catch {
print(error)
}
}
}
} else {
// Cannot open whatsapp
}
}
}
}
<key>LSApplicationQueriesSchemes</key>
<array>
<string>whatsapp</string>
</array>
祝愉快!!!
Wagner Sales的回答的Swift 3版本:
let urlWhats = "whatsapp://app"
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) {
if let whatsappURL = URL(string: urlString) {
if UIApplication.shared.canOpenURL(whatsappURL) {
if let image = UIImage(named: "image") {
if let imageData = UIImageJPEGRepresentation(image, 1.0) {
let tempFile = NSURL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Documents/whatsAppTmp.wai")
do {
try imageData.write(to: tempFile!, options: .atomic)
self.documentIC = UIDocumentInteractionController(url: tempFile!)
self.documentIC.uti = "net.whatsapp.image"
self.documentIC.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)
}
catch {
print(error)
}
}
}
} else {
// Cannot open whatsapp
}
}
}
let urlWhats = "whatsapp://app"
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlHostAllowed) {
if let whatsappURL = URL(string: urlString) {
if UIApplication.shared.canOpenURL(whatsappURL) {
if let imageData = UIImageJPEGRepresentation(image, 1.0) {
let tempFile = NSURL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Documents/whatsAppTmp.wai")!
do {
try imageData.write(to: tempFile, options: .atomic)
self.documentController = UIDocumentInteractionController(url: tempFile)
self.documentController.uti = "net.whatsapp.image"
self.documentController.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)
} catch {
print(error)
}
}
} else {
let ac = UIAlertController(title: "MessageAletTitleText".localized, message: "AppNotFoundToShare".localized, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OKButtonText".localized, style: .default))
present(ac, animated: true)
print("Whatsapp isn't installed ")
// Cannot open whatsapp
}
}
}