Подтвердить что ты не робот

UIActivityViewController & UIDocumentInteractionController не показывает параметры

Я новичок в UIActivityViewController и, возможно, мне не хватает базового понимания. То, что я пытаюсь сделать, это привязать файлы csv, xml и vcard к контроллеру активности и показать Dropbox, google drive etc options. Я загрузил и установил dropbox, google drive и т.д. Приложения на свой iPhone.

Теперь, когда я запускаю UIActivityViewController, все, что я вижу, - это сообщение по умолчанию и приложение электронной почты в моем контроллере. Как я могу открыть другие приложения? Нужно ли устанавливать все приложения для отдельных 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];


}
4b9b3361

Ответ 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

Я использовал ваш код здесь, чтобы открыть с Dropbox, и только после того, как я использовал метод presentPreview (ниже). Это сработало для меня. PDF был показан как предварительный просмотр, а затем щелкнуть кнопкой предварительного просмотра (справа вверху) опция dropbox ( "open in dropbox" ) выполнила задание. Поскольку это работает в приложении электронной почты в предварительном просмотре вложений.

[interactionController presentPreviewAnimated:YES];

Когда я попытался открыть с presentOpenInMenuFromRect, он разбился при выборе "open in dropbox".