Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIActivityViewController & UIDocumentInteractionController not showing options

I am new to UIActivityViewController and perhaps I am missing a basic understanding. What I am trying to do is attached a csv, xml and vcard file to activity controller and show dropbox, google drive etc options. I have downloaded and installed dropbox, google drive etc apps on my iPhone.

Now when I launch UIActivityViewController all I see are default message and email app in my acitivity controller. How can I have other apps show up on their too? Do I need to install each and every apps individual SDKs and somehow incorporate them in my app?

This is what I wold like to see

enter image description here

but this is what I see instead.

enter image description here

Here's the code that I have tried so far

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


}
like image 501
Sam B Avatar asked Nov 30 '13 21:11

Sam B


3 Answers

As @rmaddy said, you should use UIDocumentInteractionController to replace UIActivityViewController, just like this:

UIDocumentInteractionController *dc = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:fileNameStr]];
[dc presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
like image 200
shanegao Avatar answered Sep 18 '22 11:09

shanegao


For anyone interested in future, here's the code all in one place. Do rate it up if this helps.

In your *.h file add this

@interface v1BackupComplete : UIViewController <UIDocumentInteractionControllerDelegate>
{

    UIDocumentInteractionController *docController;

}

In your *.m file add this

/************************
 * 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;
}
like image 32
Sam B Avatar answered Sep 18 '22 11:09

Sam B


UIActivityViewController only shows standard built-in activities plus any custom activities you pass as applicationActivities.

For what you are doing, you don't want UIActivityViewController. You want a UIDocumentInteractionController. If you just want to display existing apps that can open the file, use one of the presentOpenInMenuFrom... methods.

But note that is to be used for just a single file, not three.

Passing three files makes no sense in this context.

like image 31
rmaddy Avatar answered Sep 19 '22 11:09

rmaddy