今天要实现一个功能, 通过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 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

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

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://github. ...

  4. iOS开发:利用SDWebImage实现图片加载与缓存

    iOS开发:利用SDWebImage实现图片加载与缓存 SDWebImage是一套开源框架,这个类库提供一个UIImageView类别以支持加载来自网络的远程图片.具有缓存管理.异步下载.同一个URL ...

  5. java访问本地文件_详解Java读取本地文件并显示在JSP文件中

    详解Java读取本地文件并显示在JSP文件中 当我们初学IMG标签时,我们知道通过设置img标签的src属性,能够在页面中显示想要展示的图片.其中src的值,可以是磁盘目录上的绝对,也可以是项目下的相 ...

  6. 使用poi导出excel报错-打开文件报“Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃”

    使用poi导出excel报错-打开文件报"Excel 已完成文件级验证和修复.此工作簿的某些部分可能已被修复或丢弃" 1.原本正常使用的Excel导出突然下载文件报错 2.定位时发 ...

  7. Idea 中隐藏文件夹、文件的显示 idea 忽略文件

    Idea 中隐藏文件夹.文件的显示 idea 忽略文件 一.步骤记录 1.File --- settings --- Editor --- File Types --- Ignore Files an ...

  8. iOS开发中利用AFNetWorking判读网络是否连接

    在iOS项目开发中有时候需要判断当前设备中是否有网络连接,然后再去做一些弹框提示或者其他的操作来提高用户体验.在最近的开发中利用AFNetWorking实现了网络连接判断,下面是具体操作: 1.首先需 ...

  9. iOS开发证书、bundle ID、App ID、描述文件、p12文件,企业证书打包发布,及过期处理

    文章目录 1 .iOS开发证书,描述文件,bundle ID的关系 2. Apple开发账号添加团队成员 3 .开发证书,生产证书,描述文件,AppID关系及生成. 4.证书导出p12文件 5.描述文 ...

最新文章

  1. C++中指针和void
  2. JavaScript的函数
  3. Tark钱包面向全球招募优秀上币方,千亿财富等你来拿!
  4. AgileEAS.NET平台开发实例-药店系统-报表开发(高级篇)
  5. mysql数据类型支持比较运_Mysql支持的数据类型(总结)
  6. os.path.exists判断文件是否存在
  7. GBK点阵字库制作工具说明及下载
  8. 大华服务器如何修改IP,大华摄像头更改IP地址
  9. 2019新买电脑必备软件
  10. 调节效应分析时简单斜率图或交互效应图出现负数截距?
  11. 行业上的品牌策划公司是怎么做品牌策划方案的?
  12. 用Coreldraw制作晶莹剔透苹果风格按钮(转)
  13. 开关电源MOS管如何选择,参数是核心
  14. 百度新闻推荐真的在推荐新闻吗
  15. 【UI自动化-2】UI自动化元素定位专题
  16. 支持在线预览,方便快捷
  17. 如果你到了20岁,还没到 25岁
  18. cocos2d粒子系统--粒子编辑器Particle designer属性的介绍
  19. Nginx快速入门(安装 负载均衡 动静分离 主备 原理)
  20. 树莓派 Raspbian Buster Lite版系统键盘布局修改问题

热门文章

  1. SQL语句(增删改查)
  2. 盘古开源解析:物联网时代的芯片产业新趋势
  3. 企业为什么要使用云计算,主要有哪些优势?
  4. 诗仙诗圣,你还知道诗什么
  5. 转贴:ubuntu 7.10 常用软件与编程环境搭建
  6. 字符串 匹配首尾字符串 java_java Matcher匹配头尾截取替换字符串的案例
  7. Excel职场小技巧:教你如何分页打印Excel表格
  8. RSA的dp泄露 —— 【WUST-CTF2020】leak
  9. java SpringBoot报错Servlet.service() for servlet 和No converter for的解决办法
  10. filebeat重复采集数据问题排查