Absolute Ripple
  • Absolute Ripple
  • Portfolio
  • Apps
    • Baby Photo
    • Baby Feed
    • Baby Birth
    • Parking Assistant
    • At Home Baker
    • My Card
    • Gourmet Cooking Timer
    • Step by Snap
    • Snap2Word
    • Memorable
    • Easy Party
    • Wedding Anniversary
    • Kris Kringle
    • EFCA
    • M2k App
  • Resources
  • Videos
  • Contact

Opening files in another app

19/3/2012

4 Comments

 
It is relatively simple to get another app (a third party or one of Apple's apps) on the iPhone or iPad to open a file. The magic is mostly done by Apple in the UIDocumentInteractionController Class.
This class is part of the UIKit framework, which is included by the default xCode project setup so there is no need to link any additional framework.
This is what you need to do in terms of code:
1. Make your relevant class file conform to the UIDocumentInteractionControllerDelegate protocol and define a variable
Assuming your class is called ViewController, then in the ViewController.h file:
        @interface ViewController : UIViewController <UIDocumentInteractionControllerDelegate>
        {
            UIDocumentInteractionController *docController;
        }
2. Add the following methods in ViewController.m:
        //- set up the UIDocumentInteraction controller and set its delegate to self so we can handle the callback events
        - (UIDocumentInteractionController *) setupControllerWithURL:(NSURL *)fileURL
                                               usingDelegate:(id <UIDocumentInteractionControllerDelegate>)         interactionDelegate {
   
            UIDocumentInteractionController *interactionController =
            [UIDocumentInteractionController interactionControllerWithURL:fileURL];
            interactionController.delegate = interactionDelegate;
   
            return interactionController;
            }
       
        //- the key instance method here is the presentOptionsMenuFromBarBUttonItem
        //- it is assumed here that there is a BarButtonItem called _btnActions
        - (void)showOptionsMenu
        {
            NSURL *fileURL = [NSURL fileURLWithPath:@"THE_FILE_URL_PATH"];
            docController = [self setupControllerWithURL:fileURL
                                   usingDelegate:self];
            bool didShow = [docController presentOptionsMenuFromBarButtonItem:_btnActions
                                                             animated:YES];
            if (!didShow) {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                        message:@"Sorry. The appropriate apps are not found on this device."
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles: nil];
                    [alert show];
            }
        }
3. Add a method to invoke the above when you want to show the apps that you could send the file to
In this example, a UIBarButton is wired up to the following IBActions:
    - (IBAction)ActionButtonClicked:(id)sender {
        [self showOptionsMenu];
    }
That is it. When the button is clicked an action sheet will appear (all powered by the Apple's UIDocumentInteractionController class) that shows the apps (if any) that you could send the file to.

You can optionally implement the following delegate methods:
- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application
- (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application
- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller
4 Comments
Morten Slott Hansen link
4/9/2012 11:05:52 am

All sounds good. I see that it should be possible to rename the file by changing the name property. However when I try this it has no effect ie. when sending a file to DropBox.
Have you any insight on this ?

Reply
Tim
3/4/2013 08:53:51 pm

Hi,

I'm pretty new with iOS development, and I'm trying the sample code out and it seems to be working correctly...the problem is when I get into the ShowOptionsMenu method, the code crashes at the call to presentOptionsMenuFromBarButtonItem.

The error I get is "Popovers cannot be presented from a view which does not have a window." Have you seen this, and do you know how to fix it?

Reply
Ray
4/4/2013 01:47:04 am

Hi Tim
I have seen this error before but not in this context. The only thing I can think of right now (without seeing all your codes) is whether the BarButtonItem has been set up correctly (i.e. the button named _btnActions). This is a UIBarButtonItem not a UIButton. It should have been created either somewhere in your code or in Interface Builder.
Like to hear how you eventually fix the problem too.

Reply
Bruno link
21/4/2013 06:50:59 pm

Hi, thanks ....

The line
NSURL *fileURL = [NSURL fileURLWithPath:@"THE_FILE_URL_PATH"];

Help me !

Reply

Your comment will be posted after it is approved.


Leave a Reply.

    Archives

    August 2013
    July 2013
    June 2013
    May 2013
    January 2013
    October 2012
    September 2012
    March 2012
    February 2012

    Categories

    All
    Apple Policy
    In App Purchase
    Ios Programming
    Our Apps

    RSS Feed

    Disclaimers

    Here are some resources that I find useful in writing iOS apps. These are not necessarily unique as they are based on various sources online.
    This is mainly to provide a source of reference to myself and my associates but if it can be of use to someone else, that is good.
    Note that information is provided here as is. Use at your own risk. There is no guarantee that it is accurate. We will update the information as time and resources permit.

    Powered by Create your own unique website with customizable templates.