iOS发展- 文件共享(使用iTunes导入文件, 并显示现有文件)
生活随笔
收集整理的這篇文章主要介紹了
iOS发展- 文件共享(使用iTunes导入文件, 并显示现有文件)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
到今天實(shí)現(xiàn)功能, 由iTunes導(dǎo)入文件的應(yīng)用程序, 并在此文檔進(jìn)行編輯的應(yīng)用。
就像我們平時(shí)經(jīng)常使用 PDF閱讀這樣的事情, 們能夠自己導(dǎo)入我們的電子書(shū)。
源代碼下載:https://github.com/colin1994/iTunesTest.git
以下詳細(xì)介紹下實(shí)現(xiàn)過(guò)程。
先看效果圖。
圖1. 未實(shí)現(xiàn)功能前, iTunes截圖
圖2. 實(shí)現(xiàn)功能后, iTunes截圖
圖3. 實(shí)現(xiàn)功能后, 執(zhí)行截圖。
好了, 通過(guò)圖片, 我們能夠看到實(shí)現(xiàn)的效果。
功能包括: 同意通過(guò)iTunes導(dǎo)入文件。
能夠查看沙盒下全部文件。
實(shí)現(xiàn)過(guò)程:
1。
在應(yīng)用程序的Info.plist文件里加入U(xiǎn)IFileSharingEnabled鍵,并將鍵值設(shè)置為YES。
2。詳細(xì)代碼:
ViewController.h
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. 保存一張圖片到設(shè)備document目錄中(為了測(cè)試方便)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 directoryNSString *filePath = [documentsPath stringByAppendingPathComponent:@"testPic.jpg"]; //Add the file name[jpgData writeToFile:filePath atomically:YES]; //Write the file//step5. 保存一份txt文件到設(shè)備document目錄中(為了測(cè)試方便)char *saves = "Colin_csdn";NSData *data = [[NSData alloc] initWithBytes:saves length:10];filePath = [documentsPath stringByAppendingPathComponent:@"colin.txt"];[data writeToFile:filePath atomically:YES];//step6. 獲取沙盒里全部文件NSFileManager *fileManager = [NSFileManager defaultManager];//在這里獲取應(yīng)用程序Documents目錄里的文件及目錄列表NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentDir = [documentPaths objectAtIndex:0];NSError *error = nil;NSArray *fileList = [[NSArray alloc] init];//fileList便是包括有該目錄下全部文件的文件名稱(chēng)及目錄名的數(shù)組fileList = [fileManager contentsOfDirectoryAtPath:documentDir error:&error];self.dirArray = [[NSMutableArray alloc] init];for (NSString *file in fileList){[self.dirArray addObject:file];}//step6. 刷新列表, 顯示數(shù)據(jù)[readTable reloadData]; }//step7. 利用url路徑打開(kāi)UIDocumentInteractionController - (void)setupDocumentControllerWithURL:(NSURL *)url {if (self.docInteractionController == nil){self.docInteractionController = [UIDocumentInteractionController interactionControllerWithURL:url];self.docInteractionController.delegate = self;}else{self.docInteractionController.URL = url;} }#pragma mark- 列表操作 - (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 indexpreviewController.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 - (NSInteger)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 that the preview controller should preview - (id)previewController:(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 that can be recreated. }@end
版權(quán)聲明:本文博客原創(chuàng)文章,博客,未經(jīng)同意,不得轉(zhuǎn)載。
轉(zhuǎn)載于:https://www.cnblogs.com/bhlsheji/p/4651282.html
與50位技術(shù)專(zhuān)家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的iOS发展- 文件共享(使用iTunes导入文件, 并显示现有文件)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 数据结构之栈的应用:括号匹配问题
- 下一篇: ECMAScript 6教程 (一)