UIActivityViewController& UIDocumentInteractionController不显示选项

时间:2013-11-30 21:57:29

标签: ios objective-c uiactivityviewcontroller

我是UIActivityViewController的新手,也许我缺少一个基本的理解。我想要做的是将csv,xml和vcard文件附加到活动控制器并显示dropbox,google drive等选项。我已经在iPhone上下载并安装了Dropbox,google drive等应用程序。

现在,当我启动UIActivityViewController时,我看到的是我的acitivity控制器中的默认消息和电子邮件应用程序。我怎样才能将其他应用程序显示在他们身上?我是否需要安装每个应用程序各个SDK并以某种方式将它们合并到我的应用程序中?

这就是我想看到的

enter image description here

但这是我所看到的。

enter image description here

这是我到目前为止尝试过的代码

-(IBAction) dropBoxAction
{

    paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask ,YES);
    NSString* documentsPath = [paths objectAtIndex:0];

    //CSV
    NSMutableString *fileNameStr = [NSMutableString stringWithFormat:@"test_CSV_Backup.csv"];
    NSString* csvDataFileStr = [documentsPath stringByAppendingPathComponent:fileNameStr];
    NSData *csvData = [NSData dataWithContentsOfFile:csvDataFileStr];

    //EXCEL
    NSMutableString *fileNameStr2 = [NSMutableString stringWithFormat:@"test_EXCEL_Backup.xml"];
    NSString* excelDataFileStr = [documentsPath stringByAppendingPathComponent:fileNameStr2];
    NSData *excelData = [NSData dataWithContentsOfFile:excelDataFileStr];

    //VCARD
    NSMutableString *fileNameStr3 = [NSMutableString stringWithFormat:@"test_VCARD_Backup.vcf"];
    NSString* vcardDataFileStr = [documentsPath stringByAppendingPathComponent:fileNameStr3];
    NSData *vcardData = [NSData dataWithContentsOfFile:vcardDataFileStr];


    //adding them all together
    NSMutableArray *sharingItems = [NSMutableArray new];
    [sharingItems addObject:csvData];
    [sharingItems addObject:excelData];
    [sharingItems addObject:vcardData];

    UIActivity *activity = [[UIActivity alloc] init];
    NSArray *applicationActivities = @[activity];

    UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:sharingItems applicationActivities:applicationActivities];
    [self presentViewController:activityController animated:YES completion:nil];


}

4 个答案:

答案 0 :(得分:15)

正如@rmaddy所说,您应该使用UIDocumentInteractionController替换UIActivityViewController,就像这样:

UIDocumentInteractionController *dc = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:fileNameStr]];
[dc presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];

答案 1 :(得分:4)

对于任何对未来感兴趣的人,这里的代码都在一个地方。如果这有帮助,请对其进行评分。

在* .h文件中添加此

@interface v1BackupComplete : UIViewController <UIDocumentInteractionControllerDelegate>
{

    UIDocumentInteractionController *docController;

}

在* .m文件中添加此

/************************
 * Dropbox ACTION
 ************************/
-(IBAction) dropBoxAction2
{
    NSLog(@"dropBoxAction2 ...");

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask ,YES);
    NSString* documentsPath = [paths objectAtIndex:0];
    NSMutableString *fileNameStr3 = [NSMutableString stringWithFormat:@"test_VCARD_Backup.vcf"];
    NSString* vcardDataFileStr = [documentsPath stringByAppendingPathComponent:fileNameStr3];


    NSURL *fileURL = [NSURL fileURLWithPath:vcardDataFileStr];
    docController = [self setupControllerWithURL:fileURL
                                   usingDelegate:self];

    bool didShow = [docController presentOpenInMenuFromRect:self.view.bounds inView:self.view animated:YES];

    NSLog(@"didShow %d ...", didShow);

    if (!didShow)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR"
                                                        message:@"Sorry. The appropriate apps are not found on this device."
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles: nil];
        [alert show];
    }
}


#pragma mark - UIDocumentInteractionControllerDelegate
- (UIDocumentInteractionController *) setupControllerWithURL:(NSURL *)fileURL
                                               usingDelegate:(id <UIDocumentInteractionControllerDelegate>)         interactionDelegate {

    UIDocumentInteractionController *interactionController =
    [UIDocumentInteractionController interactionControllerWithURL:fileURL];
    interactionController.delegate = interactionDelegate;

    return interactionController;
}

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
    return self;
}

- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller
{
    return self.view;
}

- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller
{
    return self.view.frame;
}

答案 2 :(得分:3)

UIActivityViewController仅显示标准内置活动以及您作为applicationActivities传递的所有自定义活动。

对于你正在做的事情,你不想要UIActivityViewController。你想要一个UIDocumentInteractionController。如果您只想显示可以打开文件的现有应用,请使用presentOpenInMenuFrom...方法之一。

但请注意,它仅用于单个文件,而不是三个。

在这种情况下,传递三个文件是没有意义的。

答案 3 :(得分:0)

我在这里使用你的代码用dropbox打开,只有在我使用了presentPreview方法之后(吼叫)这对我有用。 pdf显示为预览,然后在预览共享按钮上单击(右上角)Dropbox选项(&#34;在Dropbox中打开&#34;)完成了这项工作。因为它适用于附件预览中的邮件应用程序。

[interactionController presentPreviewAnimated:YES];

当我尝试使用presentOpenInMenuFromRect打开时,它在选择&#34;在dropbox&#34;中打开时崩溃。