2

我需要将 Facebook 和 WhatsApp 作为我的图像的共享选项。我已经实现了 UIActivityViewController,我可以在其中通过 Facebook 分享,UIDocumentInteractionController也可以通过 WhatsApp 分享。我不知道如何合并这些东西。

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

我想将它们都放在一个弹出窗口中,但是我不知道如何实现。请问有什么小费吗?

我已经检查了StackOverFlow question 1,但它根本没有帮助我。我的文件是 .wai(用于 WhatsApp)所以当我尝试通过 FB 文件发送它时无法打开。它还显示所有选项,而我只希望 2(FB+WhatsApp) 可见。在StackOverFlow 问题 2之后,我只能显示 FB(工作一个,因为我设置了普通图像)但不能添加 WhatsApp(没有 .wai 文件,我不知道如何处理 UTI)。有没有办法解决这个问题?

4

2 回答 2

4

要更改文件类型:

- (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;
    }
}

这样我们就可以使用所有选项——Facebook、Twitter 和自定义 WhatsApp。

仅显示选定选项的问题仍未解决,但这是次要问题。

于 2014-01-02T16:29:35.230 回答
0

要排除不需要的共享选项(问题的第二部分),假设您的UIActivityViewController对象被调用activityController,请设置 excludeActivityTypes 属性,如下所示:

activityController.excludedActivityTypes = @[UIActivityTypeAssignToContact,
                                                 UIActivityTypePrint,
                                                 UIActivityTypeAddToReadingList,
                                                 UIActivityTypeAirDrop];
于 2014-05-21T05:25:27.327 回答