UIActivityViewController or UIDocumentInteractionController with WhatsApp and FB

Nat picture Nat · Jan 2, 2014 · Viewed 11.2k times · Source

I need to have Facebook and WhatsApp as sharing options for my image. I've already implemented UIActivityViewController, where i can share via Facebook and UIDocumentInteractionController where i can share via WhatsApp. I don't know how to merge these things.

UIActivityViewController:

UIActivityViewController *activityViewContoller = [[UIActivityViewController alloc] 
       initWithActivityItems:@[@"Test", image] applicationActivities:nil];
[self presentViewController:activityViewContoller animated:YES completion:nil];

UIDocumentInteractionController:

NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/whatsAppTmp.wai"];
    [UIImageJPEGRepresentation(image, 1.0) writeToFile:savePath atomically:YES];

    _documentInteractionController = [UIDocumentInteractionController 
                 interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
    _documentInteractionController.UTI = @"net.whatsapp.image";
    _documentInteractionController.delegate = self;
    [_documentInteractionController presentOpenInMenuFromRect:CGRectZero 
                                    inView:self.view animated:YES];

I want to have both of them in one popover, however I have no idea how to achieve it. Any tip please?

I've checked out StackOverFlow question 1, but it doesn't help me at all. My file is .wai (for WhatsApp) so when i try to send it via FB file is unable to open. Also it shows all options, while i want only 2(FB+WhatsApp) to be visible. Following the StackOverFlow question 2 I can show only FB (working one, because i set normal image) but can't add WhatsApp (no .wai file, i don't know what to do with UTI). Is there any way to solve this issue?

Answer

Nat picture Nat · Jan 2, 2014

To change type of file:

- (void)share {
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/tmptmpimg.jpg"];
    [UIImageJPEGRepresentation(_img, 1.0) writeToFile:path atomically:YES];

    _documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:path]];
    _documentInteractionController.delegate = self;
    [_documentInteractionController presentOptionsMenuFromRect:CGRectZero inView:self.view animated:YES];
}

- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application {
    if ([self isWhatsApplication:application]) {
        NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/tmptmpimg.wai"];
        [UIImageJPEGRepresentation(_img, 1.0) writeToFile:savePath atomically:YES];
        controller.URL = [NSURL fileURLWithPath:savePath];
        controller.UTI = @"net.whatsapp.image";
    }
}

- (BOOL)isWhatsApplication:(NSString *)application {
    if ([application rangeOfString:@"whats"].location == NSNotFound) { // unfortunately, no other way...
         return NO;
    } else {
         return YES;
    }
}

This way we can use all options- Facebook, Twitter and custom WhatsApp.

The problem with showing only selected options is still not solved, but it's the minor one.