2011-12-02 81 views

回答

15

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

请注意,当没有Wha​​tsApp的API时,此答案对于2011是正确的。

现在有可与WhatsApp的交互的API:http://www.whatsapp.com/faq/en/iphone/23559013

Objective-C的调用打开其中一个网址如下:

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

现在任何API该附加whatsappLSApplicationQueriesSchemes,请帮帮我吧,我已经花费更多时间为 – SampathKumar

+1

不,仍然没有与iOS中的WhatsApp互动的API。 – rckoenes

+0

你可以使用url方案与whatsapp应用程序交谈!请参阅@Wagner销售答案。 – JHNeves

21

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

最新release notes for whatsapp表明您可以通过共享扩展:

的WhatsApp接受下列类型的内容:

  • 文本(UTI:public.plain文本)
  • 照片(UTI :public.image)
  • 视频(UTI:public.movi​​e)
  • 音频笔记和音乐文件(UTI:public.audio)
  • PDF文档(UTI:com.adobe.pdf)
  • 联系卡(UTI:public.vcard)
  • 网页的URL(UTI:public.url)
+2

它不可能只是打开应用程序,但不共享图像等:) :) – Rushabh

+0

上面的链接是好的。这现在是可能的。这应该成为最好的答案。 –

+1

上面的链接不允许在不显示菜单的情况下将媒体共享到WhatsApp。在使用WhatsApp之前,您必须始终显示文档控制器的菜单,这在屁股上很痛苦。 – user3344977

112

现在可以以这种方式:

发送文本 - 对象 -

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 
} 

发送文本 - 斯威夫特

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

发送图像 - 斯威夫特

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> 

更多形成关于网址sheme:https://developer.apple.com/videos/play/wwdc2015-703/

我没有找到一个单一的解决方案。 关于http://www.whatsapp.com/faq/en/iphone/23559013的更多信息

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

+0

是否有可能与android应用程序集成whatsapp – BeingMIAkashs

+0

可以通过whatsapp将文本发送到特定的号码?意味着不需要从什么应用程序联系人列表中选择联系人? –

+0

您需要将“ID地址簿”与联系人关联,只需添加参数“abid = ”即可。我不知道直接发送到电话号码,但我正在寻找解决方案。 链接有一个更好的解释。 http://www.whatsapp.com/faq/en/iphone/23559013 –

3

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

  • 通过自定义URL方案
  • 通过iOS的文件交流的API

欲了解更多信息,请访问this link

谢谢。

1

是的,它是可能的:

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

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

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 
} 
+0

它的工作! –

2

斯威夫特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 
    } 
    } 
} 
0
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]; 
} 
+2

你需要添加一些解释。你为什么认为这个代码有效?与以前的答案有什么不同? –

2

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

注: - 您只可以共享文本或图像,两者共享在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]; 
1

斯威夫特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

相关问题