【问题标题】:UIActivityViewController & UIDocumentInteractionController not showing optionsUIActivityViewController & UIDocumentInteractionController 不显示选项
【发布时间】:2013-11-30 21:57:29
【问题描述】:

我是 UIActivityViewController 的新手,也许我缺少基本的了解。我想要做的是将 csv、xml 和 vcard 文件附加到活动控制器并显示保管箱、谷歌驱动器等选项。我已经在我的 iPhone 上下载并安装了 Dropbox、Google Drive 等应用程序。

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

这是我最想看到的

但这是我看到的。

这是我目前尝试过的代码

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


}

【问题讨论】:

    标签: ios objective-c uiactivityviewcontroller


    【解决方案1】:

    正如@rmaddy所说,你应该用UIDocumentInteractionController代替UIActivityViewController,就像这样:

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

    【讨论】:

    • 感谢您的帮助。我被卡住了,这正是我需要的缺失代码。您的回答已被标记。
    【解决方案2】:

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

    在你的 *.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;
    }
    

    【讨论】:

      【解决方案3】:

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

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

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

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

      【讨论】:

        【解决方案4】:

        我在这里使用您的代码使用保管箱打开,并且只有在我使用了 presentPreview 方法(如下)之后它才对我有用。 pdf 显示为预览,然后在预览共享按钮上单击(右上角)保管箱选项(“在保管箱中打开”)完成了这项工作。因为它在附件预览中的邮件应用程序中工作。

        [interactionController presentPreviewAnimated:YES];
        

        当我尝试使用 presentOpenInMenuFromRect 打开时,它在选择“在保管箱中打开”时崩溃。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-17
          • 1970-01-01
          相关资源
          最近更新 更多