[绍棠] iOS开发- 文件共享(利用iTunes导入文件, 并且显示已有文件) 以及 iOS App与iTunes文件传输的方法和对iOS App文件结构的说明
就像很多iOS上面的播放器App一样,本文编写一个程序可以通过iTunes往里面放文件,比如编写一个视频播放器程序,通过itune往里面放视频文件,然后通过这个App来播放这个视频。下面是通过iTunes往App传输文件的截图:
实现过程:
1。在应用程序的Info.plist文件中添加UIFileSharingEnabled键,并将键值设置为YES。
2。具体代码:
#import <QuickLook/QuickLook.h>
//UIDocumentInteractionController : 一个文件交互控制器,提供应用程序管理与本地系统中的文件的用户交互的支持
@property(nonatomic,retain) NSMutableArray *dirArray;
@property (nonatomic, strong) UIDocumentInteractionController *docInteractionController;
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文件结构的说明相关推荐
- iOS开发- 文件共享(利用iTunes导入文件, 并且显示已有文件)
layout: post #iOS开发- 文件共享(利用iTunes导入文件, 并且显示已有文件) title: iOS开发- 文件共享(利用iTunes导入文件, 并且显示已有文件) #时间配置 d ...
- iOS开发- 文件共享 利用iTunes导入文件 并且显示已有文件
今天要实现一个功能, 通过iTunes导入文件到应用中, 并且在应用中对这个文件进行编辑. 类似我们平时经常使用的 PDF阅读器那样的东西, 我们可以自己导入我们的电子书. 源码下载:https:// ...
- iOS App与iTunes文件传输的方法和对iOS App文件结构的说明
转:http://www.xiaoyaoli.com/?p=368 就像很多iOS上面的播放器App一样,本文编写一个程序可以通过iTunes往里面放文件,比如编写一个音乐播放器程序,通过itune往 ...
- iOS开发:利用SDWebImage实现图片加载与缓存
iOS开发:利用SDWebImage实现图片加载与缓存 SDWebImage是一套开源框架,这个类库提供一个UIImageView类别以支持加载来自网络的远程图片.具有缓存管理.异步下载.同一个URL ...
- iOS开发中利用AFNetWorking判读网络是否连接
在iOS项目开发中有时候需要判断当前设备中是否有网络连接,然后再去做一些弹框提示或者其他的操作来提高用户体验.在最近的开发中利用AFNetWorking实现了网络连接判断,下面是具体操作: 1.首先需 ...
- iOS开发之利用苹果系统自带地图进行地图开发
了解更多关于移动开发,欢迎到悦卓3G孵化园:http://www.91train.com iOS中有一个系统自带的 完整的一套地图框架--MapKit.Framework和CoreLocation.F ...
- iOS开发,利用PanoramaGL生成360度全景预览图,附选择本地全景图片并生成全景预览...
前言 初做全景项目,涉及到了360度全景展示(也可以是720度的旋转),查找了很多资料,很多都是用PanoramaGL这个库实现的,本人也踩了一下坑,下面我简单的总结一下. 初识PanoramaGL ...
- 【微信小程序云开发】利用excel导入到云数据库时乱码
需要将excel文件另存为CSV UTF-8格式 同样的如果要查看云数据库导出的文件,也需要转化 选择数据,获取外部数据,导入文本文件,根据提示操作
- iPhone 和 iPad的ios 开发中 利用 WebViewJavascriptBridge组件,通过 UIWebView 对Html进行双向通讯...
本文转载至 http://blog.csdn.net/remote_roamer/article/details/7261490 WebViewJavascriptBridge 项目的 官网 http ...
最新文章
- pythonselect a valied_python 11期 第五天
- 关于union的那些事儿
- linux auditd 审计 简介
- python队列学习笔记
- 【数据结构与算法】之深入解析“合并区间”的求解思路与算法示例
- SQL Server 的通用分页显示存储过程
- 【声入人心:音频新体验】
- 计算机基础知识预备知识,计算机预备知识详解.ppt
- Linux备份策略(第二版)
- python pandas 排序_python – pandas:单独对每列进行排序
- H5实现多图片预览上传,可点击可拖拽控件介绍
- “刑不上大夫,礼不下庶人”真实意思是什么
- Web前端学习 | Ajax
- 指派问题匈牙利解法以及其优化
- 自带作弊功能的老虎机--一样会让你输光,呵呵
- Win7无线网络共享教程:解决所有问题
- RISC-V扩展指令示例
- drupal 中基本的数据库操作
- 个人练习-Leetcode-909. Snakes and Ladders
- WIDER FACE AND PEDESTRIAN CHALLENGE - WIDER CHALLENGE