iOS Development-file sharing (using itunes to import files and displaying existing files)

Source: Internet
Author: User

To implement a feature today, import the file into the app via itunes and edit the file in the app.

Similar to the kind of PDF reader We usually use, we can import our own ebook.


SOURCE Download: Https://github.com/colin1994/iTunesTest.git

The following is a detailed introduction to the implementation process.

Look first.

Figure 1. Before the feature is implemented, ITunes



Figure 2. After the feature is implemented, ITunes


Figure 3. After the feature is implemented, run.



Well, through the picture, we can see the effect of the implementation.

Features include: Allow files to be imported via itunes. You can view all the files under the sandbox.


Implementation process: 1. Add the Uifilesharingenabled key to the application's Info.plist file and set the key value to Yes.



2. Specific 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 QuickLook libraries and header files #import <quicklook/quicklook.h>//step2. Inheritance protocol @interface Viewcontroller:uiviewcontroller<uitableviewdatasource,uitableviewdelegate, qlpreviewcontrollerdatasource,qlpreviewcontrollerdelegate,uidocumentinteractioncontrollerdelegate>{    // Step3. Declaration Display list    iboutlet UITableView *readtable;} SETP4. Declaring variable//uidocumentinteractioncontroller: A file interaction controller that provides support for application management of user interaction with files in the Local System//dirarray: Store all files inside 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]; Step5.    Save a picture to the device document folder (for testing 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 Device document folder (for testing convenience) char *saves = "COLIN_CSDN";    NSData *data = [[NSData alloc] initwithbytes:saves length:10]; FilePath = [Documentspath stringbyappendingpathcomponent:@ "Colin.txt"];            [Data Writetofile:filepath Atomically:yes]; Step6. Get all the files in the sandbox nsfilemanager *filemanager = [Nsfilemanager defaultmanager];//get the file and folder list in the Application Documents folder here Nsarray * Documentpaths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString *documentdir = [Documentpaths objectatindex:0]; Nserror *error = nil; Nsarray *filelist = [[Nsarray alloc] init];//filelist is an array containing the file name and folder name of all files under the folder fileList = [FileManager Contentsofdirectoryatpath:documentdir Error:&error];self.dirarray = [[Nsmutablearray alloc] init];for (NSString * File in fileList) {[Self.dirarray addobject:file];} Step6. Refreshes the list and displays the data [readtable Reloaddata];} Step7. Use the URL path to open uidocumentinteractioncontroller-(void) Setupdocumentcontrollerwithurl: (Nsurl *) url{if ( Self.docinteractioncontroller = = nil) {Self.docinteractioncontroller = [Uidocumentinteractioncontroller Interac        Tioncontrollerwithurl:url]; Self.docInteractioncontroller.delegate = self;    } else {Self.docInteractionController.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 = Uitableviewcellaccessorydisclosureindicator;} 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.docInteractionController.icons count]; if (Iconcount > 0) {cell.imageView.image = [self.docInteractionController.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 [Nssearchpathfordirectoriesindomains (NSDocumentDirectory, NSUserDomainMask, YES) Lastobject];} -(Uiviewcontroller *) Documentinteractioncontrollerviewcontrollerforpreview: (Uidocumentinteractioncontroller *) interactioncontroller{return self;} #pragma mark-qlpreviewcontrollerdatasource//Returns The number of items that the preview controller should preview-(NS Integer) Numberofpreviewitemsinpreviewcontroller: (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, the preview controller should preview-(ID) previewcon    Troller: (Qlpreviewcontroller *) Previewcontroller Previewitematindex: (nsinteger) idx{nsurl *fileURL = nil; Nsindexpath *selectedindexpath = [readtable indexpathforselectedrow]; Nsarray *documentpaths = Nssearchpathfordirectoriesindomains (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 didreceivememorywarning]; Dispose of any resources the 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.