2013-11-30 3 views
11

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

Теперь, когда я запускаю 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]; 


} 

ответ

15

Как @rmaddy сказал, вы должны использовать UIDocumentInteractionController заменить UIActivityViewController, так же, как это:

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

Благодарю вас за помощь. Я застрял, это именно тот недостающий код, который мне нужен. Вы отвечаете. –

3

UIActivityViewController показывает только стандартные встроенные деятельности плюс любые пользовательские действия, которые вы передаете в качестве applicationActivities.

Для чего вы не хотите UIActivityViewController. Вы хотите UIDocumentInteractionController. Если вы просто хотите отображать существующие приложения, которые могут открыть файл, используйте один из методов presentOpenInMenuFrom....

Но обратите внимание, что это должно использоваться только для одного файла, а не для трех.

Передача трех файлов не имеет смысла в этом контексте.

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

я использовал свой код здесь, чтобы открыть с раздаточной и только после того, как я использовал presentPreview method (внизу) Это было сработано для меня. PDF был показан как предварительный просмотр, а затем на кнопке предварительного просмотра нажмите кнопку (вверху справа), опция dropbox («open in dropbox») выполнила задание. Как это работает в приложении электронной почты в предварительном просмотре вложений.

[interactionController presentPreviewAnimated:YES]; 

Когда я попытался открыть с presentOpenInMenuFromRect он разбился на выбор «открыт в раздаточной».

Смежные вопросы