通过iOS应用程序中的WhatsApp分享图片/文字

是否有可能在iOS应用程序中通过WhatsApp分享图像,文字或任何你想要的东西? 我正在谷歌search,但我只发现有关Android实现的结果。

提前致谢!

不,这是不可能的,whatsapp没有任何公共的API,你可以使用。

请注意,这个答案在2011年没有Wha​​tsApp的API时是正确的。

现在有一个可用于与WhatsApp交互的api: http : //www.whatsapp.com/faq/en/iphone/23559013

Objective-C调用来打开这些URL之一如下:

NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=Hello%2C%20World!"]; if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) { [[UIApplication sharedApplication] openURL: whatsappURL]; } 

现在可以这样做:

发送文本 – 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 sheme的更多信息: https : //developer.apple.com/videos/play/wwdc2015-703/

我没有find一个单一的解决scheme。 有关http://www.whatsapp.com/faq/en/iphone/23559013的更多信息;

我做了一个小项目来帮助一些。 https://github.com/salesawagner/SharingWhatsApp

现在是可能的。 还没有尝试过呢。

WhatsApp的最新发行说明表明您可以通过共享扩展:

WhatsApp接受以下types的内容:

  • 文字(UTI:public.plain-text)
  • 照片(UTI:public.image)
  • video(UTI:public.movi​​e)
  • audio笔记和音乐文件(UTI:public.audio)
  • PDF文件(UTI:com.adobe.pdf)
  • 联系人卡片(UTI:public.vcard)
  • url(UTI:public.url)

这是共享链接到什么应用程序的用户正确的代码。

 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 } 

我向ShareKit添加了一个WhatsApp的分享器。

看看这里: https : //github.com/heringb/ShareKit

WhatsApp为您的iPhone应用程序提供了两种与WhatsApp互动的方式:

  • 通过自定义的URLscheme
  • 通过iOS文档交互API

欲了解更多信息请访问此链接

谢谢。

简单的代码和示例代码;-)

注意: – 你只能分享文字或图片,两者共享在whatsApp不是从whatsApp方面工作

  /* //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]; 

是的,这是可能的:

 NSMutableArray *arr = [[NSMutableArray alloc]init]; NSURL *URL = [NSURL fileURLWithPath:path]; NSString *textToShare = [NSString stringWithFormat:@"%@ \n",_model.title]; NSString *SchoolName= [[AppUtility sharedUtilityInstance]getAppConfigInfoByKey:@"SchoolName" SecondKeyorNil:Nil]; [arr addObject:textToShare]; [arr addObject:URL]; [arr addObject:_model.body]; [arr addObject:SchoolName]; TTOpenInAppActivity *openInAppActivity = [[TTOpenInAppActivity alloc] initWithView:_parentController.view andRect:((UIButton *)sender).frame]; UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:arr applicationActivities:@[openInAppActivity]]; // Store reference to superview (UIActionSheet) to allow dismissal openInAppActivity.superViewController = activityViewController; // Show UIActivityViewController [_parentController presentViewController:activityViewController animated:YES completion:NULL]; 

瓦格纳销售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 } } } 

Swift 3发送文本的版本:

 func shareByWhatsapp(msg:String){ let urlWhats = "whatsapp://send?text=\(msg)" if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) { if let whatsappURL = NSURL(string: urlString) { if UIApplication.shared.canOpenURL(whatsappURL as URL) { UIApplication.shared.openURL(whatsappURL as URL) } else { let alert = UIAlertController(title: NSLocalizedString("Whatsapp not found", comment: "Error message"), message: NSLocalizedString("Could not found a installed app 'Whatsapp' to proceed with sharing.", comment: "Error description"), preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: NSLocalizedString("Ok", comment: "Alert button"), style: UIAlertActionStyle.default, handler:{ (UIAlertAction)in })) self.present(alert, animated: true, completion:nil) // Cannot open whatsapp } } } } 

另外,您需要在Info.plist LSApplicationQueriesSchemes添加到LSApplicationQueriesSchemes

 NSString *shareText = @"http:www.google.com"; NSArray *objectsToShare = @[shareText]; UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil]; if (isIphone) { [self presentViewController:activityVC animated:YES completion:nil]; } else { UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:activityVC]; [popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/4, 0, 0)inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; }