Share image/text through WhatsApp in an iOS app

2019-01-02 15:09发布

Is it possible to share images, text or whatever you want through Whatsapp in a iOS app? I'm searching on google but I only found results talking about Android implementations.

Thanks in advance!

标签: iphone ios
13条回答
唯独是你
2楼-- · 2019-01-02 15:16

Yes it's possible :

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];
查看更多
荒废的爱情
3楼-- · 2019-01-02 15:20

No this is not possible, whatsapp does not have any public API you can use.

Please note that this answer is correct for 2011 when there was no API for WhatsApp.

Now there is an api available for interacting with WhatsApp: http://www.whatsapp.com/faq/en/iphone/23559013

The Objective-C call to open one of these URLs is as follows:

NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=Hello%2C%20World!"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
    [[UIApplication sharedApplication] openURL: whatsappURL];
}
查看更多
大哥的爱人
4楼-- · 2019-01-02 15:20

Swift 3 version of Wagner Sales' answer:

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
    }
  }
}
查看更多
梦该遗忘
5楼-- · 2019-01-02 15:25

Is now possible in this way:

Send Text - 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
}

Send Text - 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
        }
    }
}

Send Image - Obj-C

-- in .h file

<UIDocumentInteractionControllerDelegate>

@property (retain) UIDocumentInteractionController * documentInteractionController;

-- in .m file

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

Send Image - 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
        }
    }
}

Because a new security feature of iOS 9, you need add this lines on .plist file:

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

More information about url sheme: https://developer.apple.com/videos/play/wwdc2015-703/

I did not find a single solution for both. More information on http://www.whatsapp.com/faq/en/iphone/23559013

I made a small project to help some. https://github.com/salesawagner/SharingWhatsApp

查看更多
浅入江南
6楼-- · 2019-01-02 15:25
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];
}
查看更多
深知你不懂我心
7楼-- · 2019-01-02 15:27

Simple code and Sample code ;-)

Note:- You can only share text or image, both sharing together in whatsApp is not working from whatsApp side

 /*
    //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];
查看更多
登录 后发表回答