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

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

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

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

先看效果图。

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

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

图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 directoryNSString *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 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

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

转载于:https://www.cnblogs.com/bhlsheji/p/4651282.html

iOS发展- 文件共享(使用iTunes导入文件, 并显示现有文件)相关推荐

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

    layout: post #iOS开发- 文件共享(利用iTunes导入文件, 并且显示已有文件) title: iOS开发- 文件共享(利用iTunes导入文件, 并且显示已有文件) #时间配置 d ...

  2. [绍棠] iOS开发- 文件共享(利用iTunes导入文件, 并且显示已有文件) 以及 iOS App与iTunes文件传输的方法和对iOS App文件结构的说明

    就像很多iOS上面的播放器App一样,本文编写一个程序可以通过iTunes往里面放文件,比如编写一个视频播放器程序,通过itune往里面放视频文件,然后通过这个App来播放这个视频.下面是通过iTun ...

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

    今天要实现一个功能, 通过iTunes导入文件到应用中, 并且在应用中对这个文件进行编辑. 类似我们平时经常使用的 PDF阅读器那样的东西, 我们可以自己导入我们的电子书. 源码下载:https:// ...

  4. 删除文件时显示该文件不在此文件夹中的原因

    问题描述 删除文件时显示该文件不在此文件夹中,在网上搜索资料都是新建一个bat通过拖拽删除. 但是具体为什么无法删除并没有说明,于是我通过排除法,一步步删除子文件夹,直到找到无法删除的文件. 原因分析 ...

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

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

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

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

  7. 《新lrc播放器2》-iPhone上可以显示lrc歌词的播放器可以在播放mp3文件时显示lrc文件中的歌词的播放器

    https://apps.apple.com/cn/app/%E6%96%B0lrc%E6%92%AD%E6%94%BE%E5%99%A82/id1535214306 以前,在iPhone上播放lrc ...

  8. html显示php文件后缀,显示html文件 css文件扩展名方法

    一.为什么重要表示扩展名? 因为很多时刻我们需要区分文件的缩减名如是.txt..js..css..php..htm..asp..aspx等文件后缀名.从扩张名便可懂得文件类型及恪守共性. 常见文件扩张 ...

  9. php显示所有文件夹,显示所有文件和文件夹 php文件及文件夹操作(创建、删除、移动、复制)...

    php/** * 操纵文件类 * * 例子: * FileUtil::createDir('a/1/2/3'); 测试建立文件夹 建一个a/1/2/3文件夹 * FileUtil::createFil ...

最新文章

  1. (转)互斥对象锁和临界区锁性能比较 .
  2. ua获取手机型号_无牌山寨手机的数据提取解决方案
  3. android红米3调用相机,红米3有什么接口?红米3有HDMI接口吗?
  4. 排序之选择排序:简单选择+堆排序
  5. SpringBoot 如何进行对象复制,老鸟们都这么玩的!
  6. django-模型类的修改-添加与删除字段
  7. Unity 初级保龄球游戏
  8. 01.QT中点击弹出新页面
  9. 传新一轮估值200亿美金 小红书回应:以老股东增持为主
  10. InstallShield安装与部署
  11. 计算机毕业设计JAVA鸿鹄教育培训mybatis+源码+调试部署+系统+数据库+lw
  12. php文字添加投影,PS如何制作文字投影效果 巧用4种方法给文字添加长投影效果...
  13. 学习记录514@react使用antd选择器设置下拉菜单宽度
  14. quartz 设计表结构
  15. 数据结构常见问题系列(二)
  16. 1.1 Linux内核代码下载、编译
  17. Nim问题和阶梯Nim(staircase nim)
  18. 少儿知识付费做好内容是关键
  19. 密歇根大学计算机科学与工程,密歇根大学计算机专业有何独到之处
  20. JDK生成证书,在Nginx中配置HTTP+SSL

热门文章

  1. CentOS 6.3下rsync服务器的安装与配置[转]
  2. 大批量插入数据如何优化
  3. ASP.NET中 Repeater嵌套
  4. gsk meaning
  5. 怎么在大学当院系负责人呢?一个case study
  6. 关于 Visual stdio 编译报错:error MSB6006: “CL.exe”已退出
  7. 安卓上为什么不能用system.io.file读取streammingAssets目录下的文件
  8. hbase的集群搭建
  9. dubbo源码解析(九)远程通信——Transport层
  10. 如何使用ABAP Restful API进行代码的全文搜索