IOS development-file sharing (using iTunes to import files and display existing files)

Source: Internet
Author: User

Today, we will implement a function to import a file to the application through iTunes and edit the file in the application.

Similar to the PDF Reader we usually use, we can import our own e-books.


Source code download: https://github.com/colin1994/iTunesTest.git

The following describes the implementation process.

First.

Figure 1. iTunes



Figure 2. iTunes


Figure 3. Run the function after it is implemented.



Now, we can see the effect through the image.

Functions: allows you to import files through iTunes. You can view all files in the sandbox.


Implementation process: 1. Add the UIFileSharingEnabled key to the Info. plist file of the application, and set the key value to YES.



2. Code:

ViewController. h

//// ViewController. h // iTunesTest // Created by Colin on 14-6-8. // Copyright (c) 2014 icephone. all rights reserved. // # import <UIKit/UIKit. h> // step1. import the QuickLook library and header file # import <QuickLook/QuickLook. h> // step2. Inheritance Protocol @ interface ViewController: UIViewController <UITableViewDataSource, UITableViewDelegate, QLPreviewControllerDataSource, metrics, metrics >{// step3. declare and display the list IBOutlet UITableView * readTable ;} // setp4. declaration variable // UIDocumentInteractionController: A file interaction controller that supports application management and user interaction between files in the local system. // dirArray: store all files in the sandbox @ property (nonatomic, retain) NSMutableArray * dirArray; @ property (nonatomic, strong) UIDocumentInteractionController * docInteractionController; @ end

ViewController. m

//// ViewController. m // iTunesTest // Created by Colin on 14-6-8. // Copyright (c) 2014 icephone. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController @ synthesize dirArray; @ synthesize docInteractionController;-(void) viewDidLoad {[super viewDidLoad]; // Step 5. save an image to the document folder of the device (for test convenience). UIImage * image = [UIImage imageNamed: @ "testPic.jpg"]; NSData * jpgData = UIImageJPEGRepresentation (image, 0.8); NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentsPath = [paths objectAtIndex: 0]; // Get the docs directory NSString * filePath = [documentsPath stringByAppendingPathComponent: @ "testPic.jpg"]; // Add the file name [jpgData writeToFile: filePath atomically: YES]; // Write the file // step5. save a txt file to the document folder of the device (for test convenience) char * saves = "Colin_csdn "; NSData * data = [[NSData alloc] initWithBytes: saves length: 10]; filePath = [documentsPath stringByAppendingPathComponent: @ "colin.txt"]; [data writeToFile: filePath atomically: YES]; // step6. obtain all the files in the sandbox NSFileManager * fileManager = [NSFileManager defaultManager]; // obtain the list of files and folders in the application Documents folder NSArray * documentPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentDir = [documentPaths objectAtIndex: 0]; NSError * error = nil; NSArray * fileList = [[NSArray alloc] init]; // fileList is the array fileList = [fileManager contentsOfDirectoryAtPath: documentDir error: & error]; self. dirArray = [[NSMutableArray alloc] init]; for (NSString * file in fileList) {[self. dirArray addObject: file];} // step6. refresh the list and display the data [readTable reloadData];} // step7. use the url path to open UIDocumentInteractionController-(void) setupDocumentControllerWithURL :( NSURL *) url {if (self.doc InteractionController = nil) {self.doc InteractionController = [UIDocumentInteractionController interactionControllerWithURL: url]; self.doc InteractionController. delegate = self;} else {self.doc InteractionController. URL = url ;}# pragma mark-List Operation-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {return 1;}-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * CellName = @ "CellName"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellName]; if (cell = nil) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellName]; cell. accessoryType = optional;} NSURL * fileURL = nil; NSArray * documentPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentDir = [documentPaths objectAtIndex: 0]; NSString * path = [documentDir stringByAppendingPathComponent: [self. dirArray objectAtIndex: indexPath. row]; fileURL = [NSURL fileURLWithPath: path]; [self setupDocumentControllerWithURL: fileURL]; cell. textLabel. text = [self. dirArray objectAtIndex: indexPath. row]; NSInteger iconCount = [self.doc InteractionController. icons count]; if (iconCount> 0) {cell. imageView. image = [self.doc InteractionController. icons objectAtIndex: iconCount-1];} return cell;}-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return [self. dirArray count];}-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {QLPreviewController * previewController = [[QLPreviewController alloc] init]; previewController. dataSource = self; previewController. delegate = self; // start previewing the document at the current section index previewController. currentPreviewItemIndex = indexPath. row; [[self navigationController] pushViewController: previewController animated: YES]; // [self presentViewController: previewController animated: YES completion: nil];} # pragma mark-UIDocumentInteractionControllerDelegate-(NSString *) applicationDocumentsDirectory {return [callback (NSDocumentDirectory, NSUserDomainMask, YES) lastObject];}-(UIViewController *) callback :( UIDocumentInteractionController *) interactionController {return self;} # pragma mark-QLPreviewControllerDataSource // Returns the number of items that the preview controller shocould preview-(NSInteger) rows :( QLPreviewController *) previewController {return 1 ;} -(void) previewControllerDidDismiss :( QLPreviewController *) controller {// if the preview dismissed (done button touched ), use this method to post-process previews} // returns the item that the preview controller shocould preview-(id) previewController :( QLPreviewController *) previewController previewItemAtIndex :( NSInteger) idx {NSURL * fileURL = nil; NSIndexPath * selectedIndexPath = [readTable succeeded]; NSArray * documentPaths = succeeded (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentDir = [documentPaths objectAtIndex: 0]; NSString * path = [documentDir stringByAppendingPathComponent: [self. dirArray objectAtIndex: selectedIndexPath. row]; fileURL = [NSURL fileURLWithPath: path]; return fileURL;}-(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.