Skip to main content

Open a file in another app using a UIDocumentInteractionController (Xcode/iOS)


A Swift version of this post with added detail.

(1) The first step is to create a button by dragging one out from within the storyboard and placing it on your view.

(2) Once you've done this, wire the button up to your view controller's header file.

(3) Navigate to the .m file and within your new method place the following code:

- (IBAction)exportFile:(id)sender {
 
    // Create an NSURL for the file you want to send to another app
    NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"MyFile" withExtension:@"txt"];
 
    // Create the interaction controller
    sendFile = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
 
    // Figure out the CGRect of the button that was pressed
    UIButton *button = sender;
    CGRect buttonFrame = button.frame;
 
    // Present the app picker display
    [sendFile presentOptionsMenuFromRect:buttonFrame inView:self.view animated:YES];

}

You'll notice I called my method exportFile.

(4) Next add a property for the UIDocumentIterationController within the interface section of your .m file, like this:

@interface ExportFileViewController ()

// Retain a pointer to the UIDocumentInteractionController
@property UIDocumentInteractionController *sendFile;

@end


(5) Finally, @synthesize the property within the implementation section of the .m file.

Your entire .m file will look like this:

//
//  ExportFileViewController.m
//  ExportFile
//
//  Created by Anthony Levings on 02/03/2014.
//  Copyright (c) 2014 Gylphi. All rights reserved.
//

#import "ExportFileViewController.h"

@interface ExportFileViewController ()

// Retain a pointer to the UIDocumentInteractionController
@property UIDocumentInteractionController *sendFile;


@end

@implementation ExportFileViewController

// Synthesize UIDocumentInteractionController
@synthesize sendFile;

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)exportFile:(id)sender {
 
// Create an NSURL for the file you want to send to another app
    NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"MyFile" withExtension:@"txt"];
 
    // Create the interaction controller
    sendFile = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
 
    // Figure out the CGRect of the button that was pressed
    UIButton *button = sender;
    CGRect buttonFrame = button.frame;
 
    // Present the app picker display
    [sendFile presentOptionsMenuFromRect:buttonFrame inView:self.view animated:YES];

}
@end


(6) Last of all create a new file in your app called MyFile.txt and add some "Hello World" text.

You're now ready to build, run and test.


Endorse on Coderwall

Comments

  1. I am expecting more interesting topics from you. And this was nice content and definitely it will be useful for many people.

    Mobile App Development Company in Chennai
    Android app Development Company in Chennai
    ios app development Company in Chennai

    ReplyDelete
  2. It's interesting that many of the bloggers your tips helped to clarify a few things for me as well as giving.. very specific nice content. And tell people specific ways to live their lives.Sometimes you just have to yell at people and give them a good shake to get your point across.
    Mobile App Development Company
    Android app Development Company
    ios app development Company
    Mobile App Development Companies

    ReplyDelete

Post a comment