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

实现过程:

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

2。具体代码:

ViewController.h

#import <UIKit/UIKit.h>

//step1. 导入QuickLook库和头文件

#import <QuickLook/QuickLook.h>

//step2. 继承协议

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,QLPreviewControllerDataSource,QLPreviewControllerDelegate,UIDocumentInteractionControllerDelegate>

{

//step3. 声明显示列表

UITableView *readTable;

}

//setp4. 声明变量

//UIDocumentInteractionController : 一个文件交互控制器,提供应用程序管理与本地系统中的文件的用户交互的支持

//dirArray : 存储沙盒子里面的所有文件

@property(nonatomic,retain) NSMutableArray *dirArray;

@property (nonatomic, strong) UIDocumentInteractionController *docInteractionController;

@end

ViewController.m

#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];

readTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64 -49)];

readTable.dataSource = self;

readTable.delegate = self;

[self.view addSubview:readTable];

//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

{

[tableView deselectRowAtIndexPath:indexPath animated:YES];

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.

}

这个iTunes同步相对来说比较简单, 接下来可以关注我的实现WIFI局域网传输文件到iPhone, 希望对你们有所帮助

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

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

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

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

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

  3. iOS App与iTunes文件传输的方法和对iOS App文件结构的说明

    转:http://www.xiaoyaoli.com/?p=368 就像很多iOS上面的播放器App一样,本文编写一个程序可以通过iTunes往里面放文件,比如编写一个音乐播放器程序,通过itune往 ...

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

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

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

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

  6. iOS开发之利用苹果系统自带地图进行地图开发

    了解更多关于移动开发,欢迎到悦卓3G孵化园:http://www.91train.com iOS中有一个系统自带的 完整的一套地图框架--MapKit.Framework和CoreLocation.F ...

  7. iOS开发,利用PanoramaGL生成360度全景预览图,附选择本地全景图片并生成全景预览...

    前言 初做全景项目,涉及到了360度全景展示(也可以是720度的旋转),查找了很多资料,很多都是用PanoramaGL这个库实现的,本人也踩了一下坑,下面我简单的总结一下. 初识PanoramaGL ...

  8. 【微信小程序云开发】利用excel导入到云数据库时乱码

    需要将excel文件另存为CSV UTF-8格式 同样的如果要查看云数据库导出的文件,也需要转化 选择数据,获取外部数据,导入文本文件,根据提示操作

  9. iPhone 和 iPad的ios 开发中 利用 WebViewJavascriptBridge组件,通过 UIWebView 对Html进行双向通讯...

    本文转载至 http://blog.csdn.net/remote_roamer/article/details/7261490 WebViewJavascriptBridge 项目的 官网 http ...

最新文章

  1. pythonselect a valied_python 11期 第五天
  2. 关于union的那些事儿
  3. linux auditd 审计 简介
  4. python队列学习笔记
  5. 【数据结构与算法】之深入解析“合并区间”的求解思路与算法示例
  6. SQL Server 的通用分页显示存储过程
  7. 【声入人心:音频新体验】
  8. 计算机基础知识预备知识,计算机预备知识详解.ppt
  9. Linux备份策略(第二版)
  10. python pandas 排序_python – pandas:单独对每列进行排序
  11. H5实现多图片预览上传,可点击可拖拽控件介绍
  12. “刑不上大夫,礼不下庶人”真实意思是什么
  13. Web前端学习 | Ajax
  14. 指派问题匈牙利解法以及其优化
  15. 自带作弊功能的老虎机--一样会让你输光,呵呵
  16. Win7无线网络共享教程:解决所有问题
  17. RISC-V扩展指令示例
  18. drupal 中基本的数据库操作
  19. 个人练习-Leetcode-909. Snakes and Ladders
  20. WIDER FACE AND PEDESTRIAN CHALLENGE - WIDER CHALLENGE

热门文章

  1. 【过关斩将】面试官:小伙子你都有哪些业余爱好?
  2. prometheus告警功能
  3. 龙芯小本debian无线(wifi)连接设置
  4. 计算机存在其他连接设备错误,USB设备连接电脑失败怎么办
  5. python数据质量检查
  6. Golang DNS 随便写写
  7. 云服务器到底是什么?云服务器的优势有哪些
  8. windows聚焦壁纸不更新_Win10系统下聚焦锁屏壁纸无法自动更换怎么解决
  9. 前端开发的流程与规范
  10. 复旦情商课魅力女教师上课实录