Sharing image to Whatsapp & Facebook
Asked Answered
R

2

12

I'm already able to share photos to Whatsapp but the way I do this is by providing Whatsapp option in a UIActivityViewController and then showing a UIDocumentInteractionController.

From this UIDocumentInteractionController, I choose the Whatsapp option which redirects the user to Whatsapp and enables him to share the photo.

So far my code is like this:

if ([activityType isEqualToString:@"whatsappSharing"]) {
    if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"whatsapp://app"]]) {
        NSString *savePath  = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/whatsAppTmp.wai"];

        [UIImageJPEGRepresentation(finalImage, 1.0) writeToFile:savePath atomically:YES];

        weakDocumentInteraction = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
        weakDocumentInteraction.UTI = @"net.whatsapp.image";
        weakDocumentInteraction.delegate = weakSelf;

        [weakDocumentInteraction presentOpenInMenuFromRect:CGRectZero inView:weakSelf.view animated:YES];
    }
}

I want to be able to select the option from a UIActivityViewController and directly show Whatsapp.

Is there a way to jump this second part of presenting the UIDocumentInteractionController and selecting the Whatsapp app option programmatically?

Currently, the user has to select the Whatsapp option twice in order to share the image.

PS: I'm using UIActivityViewController because I'm using other activities too.

Repentant answered 28/5, 2014 at 21:14 Comment(3)
what "other activities" are you providing that forces you to use UIActivityController?Euphemia
actually I would only want facebook and save to album, is there a way to do this using only document interaction controller?Repentant
apparently there is not : see similar questionEuphemia
E
20

So basically, you want to share an image via Facebook or Whatsapp, plus provide an option to save the image.

Well... Lets break this down slowly for the 2 quickest ways.


Facebook + Whatsapp

Via UIActivityViewController

If you share image via UIActivityViewController then in order to provide Whatsapp option, you'll need to create a custom UIActivity (See Stackoverflow Question).

The problems with this are:

  1. Whatsapp has not provided a URL scheme to pass an image object (yet).
    Look at JBWhatsAppActivity if you're curious about this in general.

Via UIDocumentInteractionController

If you share image via UIDocumentInteractionController then in order to provide the Facebook option, you'll need to specify the UTI as public.image.

The problems with this are:

  1. The public.image UTI will populate your UIDocumentInteractionController with alot more options than just Facebook and Whatsapp.
  2. For Save option, rather than
    • Using -presentOpenInMenuFromRect:inView:animated: method that displays only apps that open this file type...
    • You will need to use -presentOptionsMenuFromRect:inView:animated: method in order to show the option Save to Album. But this will include other options such as Mail etc.

Summary:

If you use only UIDocumentInteractionController then you should accept the fact that you cannot show just Facebook + Whatsapp + Save (due to the reasons mentioned above)

If you use UIActivityViewController and don't want to create a custom UIActivity for Whatsapp, then you're stuck but... if you decide to use a custom UIActivity then you're still stuck because there's no provision to share an image object with Whatsapp using UIActivityViewController.

Conclusion:

There doesn't seem to be any easy way to have just Facebook + Whatsapp + Save option.

The thing that complicates it is Whatsapp which forces you to use UIDocumentInteractionController which seems like the only way to share an image on Whatsapp (currently) and due to it's limited URL scheme, there's no point providing it in UIActivityViewController.

Furthermore, using UIDocumentInteractionController in turns leads you to provide more options than you would want.

Honestly, this looks pretty bleak right now.

Euphemia answered 30/5, 2014 at 15:58 Comment(3)
So I will leave the app just as it is, it works fine sharing for whatsapp, but not the way I wanted. Anyway, thanks for solving my doubt!Repentant
@LucasDomene : you're welcome :) well... right now i see no other way around it so you're better off the way you are.Euphemia
Looks like this is a combination of both solutions : github.com/mvarie/MMMWhatsAppActivityVenom
A
1

Well, if I understand the question in a proper way, there is an option to share it easily via 1 UIDocumentInteractionController, please review my old question & answer: https://mcmap.net/q/1008121/-uiactivityviewcontroller-or-uidocumentinteractioncontroller-with-whatsapp-and-fb.

Asa answered 17/6, 2014 at 7:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.