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];
}
// 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
// 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
// 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.
Comments
Post a Comment