到今天实现功能, 由iTunes导入文件的应用程序, 并在此文档进行编辑的应用。

就像我们平时经常使用 PDF阅读这样的事情, 们能够自己导入我们的电子书。

源代码下载:https://github.com/colin1994/iTunesTest.git

以下详细介绍下实现过程。

先看效果图。

图1. 未实现功能前, iTunes截图

图2. 实现功能后, iTunes截图

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGl0d2h5bHo=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

图3. 实现功能后, 执行截图。

好了, 通过图片, 我们能够看到实现的效果。

功能包括: 同意通过iTunes导入文件。

能够查看沙盒下全部文件。

实现过程:

1。

在应用程序的Info.plist文件里加入UIFileSharingEnabled键,并将键值设置为YES。

2。详细代码:

ViewController.h

//
// ViewController.h
// iTunesTest
//
// Created by Colin on 14-6-8.
// Copyright (c) 2014年 icephone. All rights reserved.
// #import <UIKit/UIKit.h> //step1. 导入QuickLook库和头文件
#import <QuickLook/QuickLook.h> //step2. 继承协议
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,QLPreviewControllerDataSource,QLPreviewControllerDelegate,UIDocumentInteractionControllerDelegate>
{
//step3. 声明显示列表
IBOutlet UITableView *readTable;
} //setp4. 声明变量
//UIDocumentInteractionController : 一个文件交互控制器,提供应用程序管理与本地系统中的文件的用户交互的支持
//dirArray : 存储沙盒子里面的全部文件
@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. 保存一张图片到设备document目录中(为了測试方便)
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. 保存一份txt文件到设备document目录中(为了測试方便)
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];
//在这里获取应用程序Documents目录里的文件及目录列表
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [documentPaths objectAtIndex:0];
NSError *error = nil;
NSArray *fileList = [[NSArray alloc] init];
//fileList便是包括有该目录下全部文件的文件名称及目录名的数组
fileList = [fileManager contentsOfDirectoryAtPath:documentDir error:&error]; self.dirArray = [[NSMutableArray alloc] init];
for (NSString *file in fileList)
{
[self.dirArray addObject:file];
} //step6. 刷新列表, 显示数据
[readTable reloadData];
} //step7. 利用url路径打开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 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
- (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

版权声明:本文博客原创文章,博客,未经同意,不得转载。

iOS发展- 文件共享(使用iTunes导入文件, 并显示现有文件)的更多相关文章

  1. iOS开发- 文件共享(利用iTunes导入文件, 并且显示已有文件)

    实现过程: 1.在应用程序的Info.plist文件中添加Application supports iTunes file sharing键,并将键值设置为YES. - (void)viewDidLo ...

  2. svn图标显示不正常,文件夹显示但文件不显示svn图标

    svn图标显示不正常,文件夹显示但文件不显示svn图标   这个问题的引发是自己造成的,使用myEclipse时progress会卡在 refresh svn status cache (0%)这里, ...

  3. eclipse svn新增文件不显示在文件列表,只有修改文件可以提交!

      1.情景展示 eclipse修改的文件可以正常提交,但是新增的文件没有显示在提交列表中,导致无法提交! 2.解决方案 选中要提交的文件-->右键-->Team-->提交 勾选上这 ...

  4. SpringBoot 无法显示html文件 找不到html文件 如果显示html文件

    两种情况: 1.如果使用了 thymeleaf 模板引擎,html文件可以放在 template文件夹中,如果不是一定不要放进去,否则找不到,因为html是静态页面,所以放在把此类文件放在了stati ...

  5. vs2015中将复制过来的文件夹显示目录文件

    先将文件夹和文件复制到VS程序所在的位置,点击解决方案资源管理器上的“显示所有文件”按纽,展开这个文件夹,这样你就可以看到这个文件或者文件夹了,这时,这个文件或者文件夹是虚线构成的.你右击这个文件或者 ...

  6. VC6在win7环境下无法添加以及打开现有文件的解决办法

       在VC6.0中使用键盘快捷键或者是文件菜单打开现有文件以及添加文件出现编辑器停止响应,弹出内容为Microsoft(R) Developer Studio已停止工作 Windows正在检查解决该 ...

  7. 慧自文档:代替 Everything 来快速查找文件的,实现文件显示在文件夹的层次结构中

    1. 搜索功能和Everything一样快和强大 具有 Everything 搜索快.搜索功能强等优点, 解决了不能方便选择搜索哪个文件夹, 解决了不能同一个画面进行预览等问题 2.文件直接显示到文件 ...

  8. IOS 使用cocoapods后无法导入头文件问题

    IOS 使用cocoapods后无法导入头文件问题 这时候如果你发现import的时候没有提示AFN e t wo r k i n g.h的文件,可以在target-Build Settings下修改 ...

  9. iOS开发:创建真机调试证书及描述文件

    iOS开发:创建真机调试证书及描述文件 关于苹果iOS开发,笔者也是从小白过来的,经历过各种困难和坑,其中就有关于开发证书,生产证书,in_house证书,add_Hoc证书申请过程中的问题,以及上架 ...

随机推荐

  1. ecshop 调用其他数据库中的商品

    ecshop中修改includes/cls_ecshop.php中第53行 function table($str) { /* if($str=='goods'){ return '`ecshop3' ...

  2. javascript UniqueID属性

        在Web页中的每一个HTML元素都一个ID属性,ID作为其标示,在我们的普通理解中它应该是unique的.但是HTML元素的ID属性是可写的,这就造成了我们非常可能人为的使ID的反复.按么假设 ...

  3. Unity3D之挥动武器产生的剑痕特效

    网维教程网 观看很多其它教程 眼下已知3种方法能够做这样的剑痕特效 1.尾随特效 2.程序实现动态面来处理剑痕动画. 3.美术实现剑痕动画,直接坐在模型动画里面 (由于我不会美术所以这个忽略 嘿嘿) ...

  4. Xamarin for android:为button设置click事件的几种方法

    原文:Xamarin for android:为button设置click事件的几种方法 在Xamarin中一个最基础的事情,就是为一个button指定click事件处理方法,可是即使是这么一件事也有 ...

  5. cisco(思科)交换机配置篇【两】

    上一页大家津津乐道cisco基本操作命令开关,而端午假期,该cisco简单的开关配置,并希望请您分享"端午节快乐"!Ok,配置交换机,首先,你必须进入全局配置模式Switch,在成 ...

  6. Trie图

    AC自动机是KMP的多串形式,当文本串失配时,AC自动机的fail指针告诉我们应该跳到哪里去继续匹配(跳到当前匹配串的最长后缀去),所以AC自动机的状态是有限的 但是AC自动机具有不确定性, 比如要求 ...

  7. Follow your hear(跟着心走)

    端午三天的哈尔滨之旅已经over,非常开心真的非常开心.听了刘四风老师的"为爱开讲.我爱这世界"的论坛,尽管.这三天老师讲的不多.可是句句是精华.Follow your heart ...

  8. Oracle外键(Foreign Key)使用详细的说明(一)

    Oracle外键(Foreign Key)使用详细的说明(一) 1.目标 演示如何Oracle使用外键数据库 2.什么是外键? 1)在Oracle数据库中,外键是用来实现參照完整性的方法之中的一个.打 ...

  9. swift笔记 (三) —— 字符和字符串

    字符串和字符 苹果要是不提供了unicode的字符串和字符,那就是他们公司全部人的脑袋都被门夹过 他自己家都要发非常多国家的版本号的软件,怎么可能不用unicode呢 此处略去30字... 这里能够拿 ...

  10. hdu4771 Stealing Harry Potter&#39;s Precious

    注意--你可能会爆内存-- 假设一个直接爆搜索词-- 队列存储器元件被减少到-- #include<iostream> #include<map> #include<st ...