To today implement the feature, the app is imported by itunes file, and the app is edited in this document.
Just as we often use PDFs to read such things, we are able to import our e-books ourselves.
Source code 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
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvagl0d2h5bho=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "/>
Figure 3. After implementing the function, execute.
Well, through the picture, we can see the effect of the implementation.
Features include: Consent to import files via itunes.
Ability to view all 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. Detailed 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: storage of 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 directory (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 directory (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];//here to get the file and directory list in the application documents directory Nsarray * Documentpaths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString *documentdir = [Documentpaths objectatindex:0]; Nserror *error = nil; Nsarray *filelist = [[Nsarray alloc] init];//filelist is an array of file names and directory names that include all files in the directory 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 = [s]Elf.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
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
iOS Development-file sharing (use itunes to import files and display existing files)