使用WhatsApp和FB的UIActivityViewController或UIDocumentInteractionController

时间:2014-01-02 14:24:04

标签: ios iphone objective-c whatsapp uiactivity

我需要将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];

我希望将它们都放在一个popover中,但是我不知道如何实现它。请问任何提示?

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

2 个答案:

答案 0 :(得分: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。

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

答案 1 :(得分:0)

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

activityController.excludedActivityTypes = @[UIActivityTypeAssignToContact,
                                                 UIActivityTypePrint,
                                                 UIActivityTypeAddToReadingList,
                                                 UIActivityTypeAirDrop];