UIActivityViewController or UIDocumentInteractionController with WhatsApp and FB

对着背影说爱祢 提交于 2020-01-10 11:47:11

问题


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?


回答1:


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.




回答2:


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];


来源:https://stackoverflow.com/questions/20885042/uiactivityviewcontroller-or-uidocumentinteractioncontroller-with-whatsapp-and-fb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!