UIActivityViewController or UIDocumentInteractionC

2020-02-26 02:07发布

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?

2条回答
孤傲高冷的网名
2楼-- · 2020-02-26 03:05

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.

查看更多
家丑人穷心不美
3楼-- · 2020-02-26 03:09

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