8

I am developing an iOS application in which i have to share image on WhatsApp from my application. I found this code but it deals with only text sharing https://github.com/jberlana/JBWhatsAppActivity

Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
IronMan
  • 1,505
  • 1
  • 12
  • 17

1 Answers1

8

That can be Possible using documentationInteractionController. Recently I have done this using bellow code to share image From our App to whatsApp, Line, WeChat but while you click on WhatsApp icon, then you are navigation WhatsApp app from you app and you must return you app manually. That does not redirect again after ImageSharing.

in .h file:-

@interface ViewController : UIViewController<UIDocumentInteractionControllerDelegate>
{
}

@property(nonatomic,retain) UIDocumentInteractionController *documentationInteractionController;

in .m file

- (IBAction)bocClick:(UIButton *)sender {


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"]; //here i am fetched image path from document directory and convert it in to URL and use bellow


    NSURL *imageFileURL =[NSURL fileURLWithPath:getImagePath];
    NSLog(@"imag %@",imageFileURL);

    self.documentationInteractionController.delegate = self;
    self.documentationInteractionController.UTI = @"net.whatsapp.image";
    self.documentationInteractionController = [self setupControllerWithURL:imageFileURL usingDelegate:self];
    [self.documentationInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];


}

- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL*) fileURL

                                               usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate {



    self.documentationInteractionController =

    [UIDocumentInteractionController interactionControllerWithURL: fileURL];

    self.documentationInteractionController.delegate = interactionDelegate;



    return self.documentationInteractionController;

}
Armel Larcier
  • 15,747
  • 7
  • 68
  • 89
Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
  • Where are we setting the UTI for UIDocumentInteractionController in this piece of code. Because once these 2 lines self.documentationInteractionController.UTI = @"net.whatsapp.image"; self.documentationInteractionController = [self setupControllerWithURL:imageFileURL usingDelegate:self]; overwrite it – IronMan Dec 06 '13 at 06:11
  • first test this code using button click even – Nitin Gohel Dec 06 '13 at 06:21
  • 1
    @IronMan: `self.documentationInteractionController.UTI = @"net.whatsapp.image";` by this you are setting UTI ! No ? – Maulik Dec 06 '13 at 06:25
  • actually i use this above code in my project and i test it well and i can able to share image. this is working code i dont understand where did you stuck – Nitin Gohel Dec 06 '13 at 06:27
  • 2
    Can sharing be possible through UIActivityViewController – IronMan Dec 06 '13 at 06:38
  • i dont think that will be possibel wia UIactivityViewcontroller but i m not sure if some one done using this.. i done using above one :) – Nitin Gohel Dec 06 '13 at 06:45
  • Does this method open WhatsApp? I mean redirect the app? – Matrosov Oleksandr Feb 04 '14 at 03:40
  • @NitinGohel I am not sure how it works. I know how works url scheme. Is this the same one. Or it is some internal feature. Will app open WhatsApp in your case? – Matrosov Oleksandr Feb 04 '14 at 06:23
  • Would you be so kind to share the UTI for WeChat and Line? thanks – Pochi May 08 '14 at 02:28
  • if you device installed wechat and line then you can also able to share with wechat or LINE as well – Nitin Gohel May 08 '14 at 04:45
  • It's possible to share image and text at the same time? – Mikel Sanchez Dec 30 '14 at 11:22
  • @Nitin Gohel in iOS 9 not working. in this code in ios 9 1)open Share Actionsheet 2) display Whatsapp icon . and than i click on this icon than display contact list for 1-2 second and than dismiss the contactView automatically. so bro. Guide me and help me – ramesh bhuja Apr 22 '16 at 14:45