UIActivityViewController or UIDocumentInteractionController with WhatsApp and FB
Asked Answered
C

2

2

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?

Cerussite answered 2/1, 2014 at 14:24 Comment(1)
It would be even better to implement WhatsApp as UIActivity. The problem is that i have no idea how to treat UTI and where to put this savePath part.Cerussite
C
4

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.

Cerussite answered 2/1, 2014 at 16:29 Comment(3)
what is isWhatsApplication here?Asafetida
Isn't this will create an extra file in Home Directory after sharing?Gide
So whats the UTI for Facebook, Twitter and Instagram?Ringworm
C
0

To exclude non-desired sharing options (the second part of your question), assuming your UIActivityViewController object is called activityController, set the excludedActivityTypes property, like so:

activityController.excludedActivityTypes = @[UIActivityTypeAssignToContact,
                                                 UIActivityTypePrint,
                                                 UIActivityTypeAddToReadingList,
                                                 UIActivityTypeAirDrop];
Content answered 21/5, 2014 at 5:25 Comment(1)
It's a UIDocumentInteractionController not UIActivityViewController.Sizeable

© 2022 - 2024 — McMap. All rights reserved.