iOS开发- 文件共享(利用iTunes导入文件, 并且显示已有文件)
layout: post
#iOS开发- 文件共享(利用iTunes导入文件, 并且显示已有文件)
title: iOS开发- 文件共享(利用iTunes导入文件, 并且显示已有文件)
#时间配置
date: 2016-08-31 15:30:50 +0800
#大类配置
categories: 知识
#小类配置
tag: object-c
相关链接
- iOS开发- 文件共享(利用iTunes导入文件, 并且显示已有文件)
- 【iOS功能实现】之利用UIDocumentInteractionController打开和预览文档
- iOS Document Interaction 编程指南
创建过程
1. 首先在plist文件中进行设置,加入 UIFileSharingEnabled
使项目能够实现ituns共享功能
2. 引入QuickLook框架 #import <QuickLook/QuickLook.h>
其中引入头#improt <string.h>
为的使用 strlen(txt) 来计算char长度
3. 创建tableView,用来承载从共享文件夹中获取的文件列表名
4. 导入各种代理方法
<UITableViewDelegate,UITableViewDataSource,QLPreviewControllerDelegate,QLPreviewControllerDataSource,UIDocumentInteractionControllerDelegate>
5. 书写沙盒文件相关的处理方法
#pragma mark **************** 沙盒中文件相关处理 begin ****************
#pragma mark 保存图片的本地方法
- (void)saveToDocumentsWithImage:(UIImage *)image andImageName:(NSString *)imageName {NSData *imageData = UIImageJPEGRepresentation(image, 0.8); // 转换类型NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; // 获取沙盒路径NSString *filePath = [documentPath stringByAppendingPathComponent:imageName];[imageData writeToFile:filePath atomically:YES]; // 将数据写书沙盒
}#pragma mark 保存文字相关内容
- (void)saveToDocumentsWithTxT:(char *)txt andTxtFileName:(NSString *)txtFileName {NSData *txtData = [[NSData alloc] initWithBytes:txt length:strlen(txt)];NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; // 获取沙盒路径NSString *filePath = [documentPath stringByAppendingPathComponent:txtFileName];[txtData writeToFile:filePath atomically:YES];
}#pragma mark 直接获取ituns文件夹中列表
- (NSArray *)getFileListFromDocument {NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; // 获取沙盒路径NSError *error = nil;NSArray *fileList = [NSArray array];fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentPath error:&error]; // 获取内容return fileList;
}#pragma mark 获取沙盒中文件路径url
- (NSURL *)getFileUrlOfDocumentsWithIndex:(NSInteger)index {NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; // 获取沙盒路径NSArray *fileList = [self getFileListFromDocument];NSString *path = [documentPath stringByAppendingPathComponent:[fileList objectAtIndex:index]];return [NSURL fileURLWithPath:path];
}
#pragma mark **************** 沙盒中文件相关处理 end ****************
6. 根据方法获取进行相关数据的添加
1> 获取列表信息
self.dirArray = [self getFileListFromDocument]; // 获取沙盒文件列表名字
[self.readTableView reloadData];
2> 在cellForRow里边设置各种数据,即可显示列表
#pragma mark cellForRow方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{static NSString *cellIdentifier = @"cell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];if (cell == nil) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;}cell.textLabel.text = [self.dirArray objectAtIndex:indexPath.row];NSURL *fileURL = [self getFileUrlOfDocumentsWithIndex:indexPath.row];// 根据fileURL创建DocumentController[self setupDocumentControllerWithURL:fileURL];// 获取页面中的iconsNSInteger iconCount = [self.documentInteractionController.icons count];if (iconCount > 0) {// 将icon作为cell上的图片cell.imageView.image = [self.documentInteractionController.icons objectAtIndex:iconCount-1];}// 设置选择时的状态cell.selectionStyle = UITableViewCellSelectionStyleDefault;// 设置cell背景色为透明色cell.backgroundColor = [UIColor clearColor];return cell;
}
7. 创建QLPreviewController部分东西
#pragma mark 添加点击cell的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {QLPreviewController *previewController = [[QLPreviewController alloc] init];previewController.dataSource = self;previewController.delegate = self;previewController.currentPreviewItemIndex = indexPath.row;[self.navigationController pushViewController:previewController animated:YES];//点击以后直接恢复正常状态[tableView deselectRowAtIndexPath:indexPath animated:YES];
}#pragma mark **************** previewController方法/代理方法部分 begin ****************
- (void)setupDocumentControllerWithURL:(NSURL *)url {if (self.documentInteractionController == nil) {self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:url];self.documentInteractionController.delegate = self;} else {self.documentInteractionController.URL = url;}
}#pragma mark 设置previewController中item的个数
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {return 1;
}#pragma mark 从previewController页面返回时的代理方法
- (void)previewControllerDidDismiss:(QLPreviewController *)controller {// 可以处理返回的数据
}#pragma mark 点击previewItem是点击获取内容相关信息
- (id)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {NSIndexPath *selectIndexPath = [self.readTableView indexPathForSelectedRow];NSURL *fileURL = [self getFileUrlOfDocumentsWithIndex:selectIndexPath.row];return fileURL;
}#pragma mark **************** previewController方法/代理方法部分 end ****************
全部代码
//
// ViewController.m
// ituns共享文件练习
//
// Created by 赵宏亚 on 16/8/31.
// Copyright © 2016年 赵宏亚. All rights reserved.
//#import "ViewController.h"
#import <string.h>
#import <QuickLook/QuickLook.h>@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,QLPreviewControllerDelegate,QLPreviewControllerDataSource,UIDocumentInteractionControllerDelegate>@property (weak, nonatomic) IBOutlet UITableView *readTableView; //用于显示共享文件夹中文件
@property (nonatomic,strong) UIDocumentInteractionController *documentInteractionController; // 系统沙盒文件交互VC
@property (nonatomic,strong) NSArray *dirArray; //存储路径名称@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.self.dirArray = [self getFileListFromDocument]; // 获取沙盒文件列表名字NSLog(@"self.dirArray ===== %@",self.dirArray);self.readTableView.delegate = self;self.readTableView.dataSource = self;// 设置tableView背景色为透明色self.readTableView.backgroundColor = [UIColor clearColor];// 分割线样式// self.tabelView.separatorStyle = UITableViewCellSeparatorStyleNone;//去掉空白cellself.readTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];[self.readTableView reloadData];
}#pragma mark **************** tableView代理 begin ****************
#pragma mark 设置每行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {return 60;
}#pragma mark 设置section的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{return 1;
}#pragma mark 设置每个section的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{return self.dirArray.count;
}#pragma mark cellForRow方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{static NSString *cellIdentifier = @"cell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];if (cell == nil) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;}cell.textLabel.text = [self.dirArray objectAtIndex:indexPath.row];NSURL *fileURL = [self getFileUrlOfDocumentsWithIndex:indexPath.row];// 根据fileURL创建DocumentController[self setupDocumentControllerWithURL:fileURL];// 获取页面中的iconsNSInteger iconCount = [self.documentInteractionController.icons count];if (iconCount > 0) {// 将icon作为cell上的图片cell.imageView.image = [self.documentInteractionController.icons objectAtIndex:iconCount-1];}// 设置选择时的状态cell.selectionStyle = UITableViewCellSelectionStyleDefault;// 设置cell背景色为透明色cell.backgroundColor = [UIColor clearColor];return cell;
}#pragma mark 添加点击cell的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {QLPreviewController *previewController = [[QLPreviewController alloc] init];previewController.dataSource = self;previewController.delegate = self;previewController.currentPreviewItemIndex = indexPath.row;[self.navigationController pushViewController:previewController animated:YES];//点击以后直接恢复正常状态[tableView deselectRowAtIndexPath:indexPath animated:YES];
}#pragma mark **************** tableView代理 end ****************#pragma mark **************** previewController方法/代理方法部分 begin ****************
- (void)setupDocumentControllerWithURL:(NSURL *)url {if (self.documentInteractionController == nil) {self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:url];self.documentInteractionController.delegate = self;} else {self.documentInteractionController.URL = url;}
}#pragma mark 设置previewController中item的个数
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {return 1;
}#pragma mark 从previewController页面返回时的代理方法
- (void)previewControllerDidDismiss:(QLPreviewController *)controller {// 可以处理返回的数据
}#pragma mark 点击previewItem是点击获取内容相关信息
- (id)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {NSIndexPath *selectIndexPath = [self.readTableView indexPathForSelectedRow];NSURL *fileURL = [self getFileUrlOfDocumentsWithIndex:selectIndexPath.row];return fileURL;
}#pragma mark **************** previewController方法/代理方法部分 end ****************#pragma mark **************** 沙盒中文件相关处理 begin ****************
#pragma mark 保存图片的本地方法
- (void)saveToDocumentsWithImage:(UIImage *)image andImageName:(NSString *)imageName {NSData *imageData = UIImageJPEGRepresentation(image, 0.8); // 转换类型NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; // 获取沙盒路径NSString *filePath = [documentPath stringByAppendingPathComponent:imageName];[imageData writeToFile:filePath atomically:YES]; // 将数据写书沙盒
}#pragma mark 保存文字相关内容
- (void)saveToDocumentsWithTxT:(char *)txt andTxtFileName:(NSString *)txtFileName {NSData *txtData = [[NSData alloc] initWithBytes:txt length:strlen(txt)];NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; // 获取沙盒路径NSString *filePath = [documentPath stringByAppendingPathComponent:txtFileName];[txtData writeToFile:filePath atomically:YES];
}#pragma mark 直接获取ituns文件夹中列表
- (NSArray *)getFileListFromDocument {NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; // 获取沙盒路径NSError *error = nil;NSArray *fileList = [NSArray array];fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentPath error:&error]; // 获取内容return fileList;
}#pragma mark 获取沙盒中文件路径url
- (NSURL *)getFileUrlOfDocumentsWithIndex:(NSInteger)index {NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; // 获取沙盒路径NSArray *fileList = [self getFileListFromDocument];NSString *path = [documentPath stringByAppendingPathComponent:[fileList objectAtIndex:index]];return [NSURL fileURLWithPath:path];
}
#pragma mark **************** 沙盒中文件相关处理 end ****************- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}@end
iOS开发- 文件共享(利用iTunes导入文件, 并且显示已有文件)相关推荐
- iOS开发- 文件共享 利用iTunes导入文件 并且显示已有文件
今天要实现一个功能, 通过iTunes导入文件到应用中, 并且在应用中对这个文件进行编辑. 类似我们平时经常使用的 PDF阅读器那样的东西, 我们可以自己导入我们的电子书. 源码下载:https:// ...
- [绍棠] iOS开发- 文件共享(利用iTunes导入文件, 并且显示已有文件) 以及 iOS App与iTunes文件传输的方法和对iOS App文件结构的说明
就像很多iOS上面的播放器App一样,本文编写一个程序可以通过iTunes往里面放文件,比如编写一个视频播放器程序,通过itune往里面放视频文件,然后通过这个App来播放这个视频.下面是通过iTun ...
- iOS发展- 文件共享(使用iTunes导入文件, 并显示现有文件)
到今天实现功能, 由iTunes导入文件的应用程序, 并在此文档进行编辑的应用. 就像我们平时经常使用 PDF阅读这样的事情, 们能够自己导入我们的电子书. 源代码下载:https://github. ...
- iOS开发:利用SDWebImage实现图片加载与缓存
iOS开发:利用SDWebImage实现图片加载与缓存 SDWebImage是一套开源框架,这个类库提供一个UIImageView类别以支持加载来自网络的远程图片.具有缓存管理.异步下载.同一个URL ...
- java访问本地文件_详解Java读取本地文件并显示在JSP文件中
详解Java读取本地文件并显示在JSP文件中 当我们初学IMG标签时,我们知道通过设置img标签的src属性,能够在页面中显示想要展示的图片.其中src的值,可以是磁盘目录上的绝对,也可以是项目下的相 ...
- 使用poi导出excel报错-打开文件报“Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃”
使用poi导出excel报错-打开文件报"Excel 已完成文件级验证和修复.此工作簿的某些部分可能已被修复或丢弃" 1.原本正常使用的Excel导出突然下载文件报错 2.定位时发 ...
- Idea 中隐藏文件夹、文件的显示 idea 忽略文件
Idea 中隐藏文件夹.文件的显示 idea 忽略文件 一.步骤记录 1.File --- settings --- Editor --- File Types --- Ignore Files an ...
- iOS开发中利用AFNetWorking判读网络是否连接
在iOS项目开发中有时候需要判断当前设备中是否有网络连接,然后再去做一些弹框提示或者其他的操作来提高用户体验.在最近的开发中利用AFNetWorking实现了网络连接判断,下面是具体操作: 1.首先需 ...
- iOS开发证书、bundle ID、App ID、描述文件、p12文件,企业证书打包发布,及过期处理
文章目录 1 .iOS开发证书,描述文件,bundle ID的关系 2. Apple开发账号添加团队成员 3 .开发证书,生产证书,描述文件,AppID关系及生成. 4.证书导出p12文件 5.描述文 ...
最新文章
- 新时代的网络工程师需要掌握哪些技能
- 其实,我是一名程序员!
- 2021数字化转型下银行发展供应链金融研究报告(上篇)
- Centos7的iso everything与DVD以及Live的区别
- Deeplabv3+ 环境配置-Anaconda3 + Pytorch1.8 + Cuda10.1 + opencv3.2.0
- Shell脚本笔记(二)Shell变量
- 拓端tecdat|R语言进行数值模拟:模拟泊松回归模型的数据
- (转)Matlab映射表数据结构(containers.Map)
- 硅谷大佬们屡次推荐的10本书,你看过几本?
- 如何画 软件工程 流程图
- 预测科技未来发展趋势的10个定律
- Linux挂载报错:Mount is denied because the NTFS volume is already exclusively opened. The volume may be a
- 神经网络井字棋AI对战版的开发与测试
- SQL 获取年度第几周
- c语言坦克大战程序设计,用纯C语言实现坦克大战
- HarmonyOS开发-路由组件体验
- Echart 仪表盘 样式调整
- 三分钟教会你用Python爬取到喜欢的小姐姐图片
- Web3 网络效应:五种心智模型
- android开发之上传头像