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

今天要实现一个功能, 通过iTunes导入文件到应用中, 并且在应用中对这个文件进行编辑。

类似我们平时经常使用的 PDF阅读器那样的东西, 我们可以自己导入我们的电子书。


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

下面具体介绍下实现过程。

先看效果图。

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



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


图3. 实现功能后, 运行截图。



好了, 通过图片, 我们可以看到实现的效果。

功能包括: 允许通过iTunes导入文件。 可以查看沙盒下所有文件。


实现过程:

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



2。具体代码:

ViewController.h

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  ViewController.h  
  3. //  iTunesTest  
  4. //  
  5. //  Created by Colin on 14-6-8.  
  6. //  Copyright (c) 2014年 icephone. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. //step1. 导入QuickLook库和头文件  
  12. #import <QuickLook/QuickLook.h>  
  13.   
  14. //step2. 继承协议  
  15. @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,QLPreviewControllerDataSource,QLPreviewControllerDelegate,UIDocumentInteractionControllerDelegate>  
  16. {  
  17.     //step3. 声明显示列表  
  18.     IBOutlet UITableView *readTable;  
  19. }  
  20.   
  21. //setp4. 声明变量  
  22. //UIDocumentInteractionController : 一个文件交互控制器,提供应用程序管理与本地系统中的文件的用户交互的支持  
  23. //dirArray : 存储沙盒子里面的所有文件  
  24. @property(nonatomic,retain) NSMutableArray *dirArray;  
  25. @property (nonatomic, strong) UIDocumentInteractionController *docInteractionController;  
  26. @end  

ViewController.m

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  ViewController.m  
  3. //  iTunesTest  
  4. //  
  5. //  Created by Colin on 14-6-8.  
  6. //  Copyright (c) 2014年 icephone. All rights reserved.  
  7. //  
  8.   
  9. #import "ViewController.h"  
  10.   
  11. @interface ViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation ViewController  
  16. @synthesize dirArray;  
  17. @synthesize docInteractionController;  
  18.   
  19. - (void)viewDidLoad  
  20. {  
  21.     [super viewDidLoad];  
  22.       
  23.       
  24.     //step5. 保存一张图片到设备document文件夹中(为了测试方便)  
  25.     UIImage *image = [UIImage imageNamed:@"testPic.jpg"];  
  26.     NSData *jpgData = UIImageJPEGRepresentation(image, 0.8);  
  27.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  28.     NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory  
  29.     NSString *filePath = [documentsPath stringByAppendingPathComponent:@"testPic.jpg"]; //Add the file name  
  30.     [jpgData writeToFile:filePath atomically:YES]; //Write the file  
  31.       
  32.       
  33.     //step5. 保存一份txt文件到设备document文件夹中(为了测试方便)  
  34.     char *saves = "Colin_csdn";  
  35.     NSData *data = [[NSData alloc] initWithBytes:saves length:10];  
  36.     filePath = [documentsPath stringByAppendingPathComponent:@"colin.txt"];  
  37.     [data writeToFile:filePath atomically:YES];  
  38.       
  39.       
  40.     //step6. 获取沙盒里所有文件  
  41.     NSFileManager *fileManager = [NSFileManager defaultManager];  
  42.     //在这里获取应用程序Documents文件夹里的文件及文件夹列表  
  43.     NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  44.     NSString *documentDir = [documentPaths objectAtIndex:0];  
  45.     NSError *error = nil;  
  46.     NSArray *fileList = [[NSArray alloc] init];  
  47.     //fileList便是包含有该文件夹下所有文件的文件名及文件夹名的数组  
  48.     fileList = [fileManager contentsOfDirectoryAtPath:documentDir error:&error];  
  49.       
  50.     self.dirArray = [[NSMutableArray alloc] init];  
  51.     for (NSString *file in fileList)  
  52.     {  
  53.         [self.dirArray addObject:file];  
  54.     }  
  55.       
  56.     //step6. 刷新列表, 显示数据  
  57.     [readTable reloadData];  
  58. }  
  59.   
  60. //step7. 利用url路径打开UIDocumentInteractionController  
  61. - (void)setupDocumentControllerWithURL:(NSURL *)url  
  62. {  
  63.     if (self.docInteractionController == nil)  
  64.     {  
  65.         self.docInteractionController = [UIDocumentInteractionController interactionControllerWithURL:url];  
  66.         self.docInteractionController.delegate = self;  
  67.     }  
  68.     else  
  69.     {  
  70.         self.docInteractionController.URL = url;  
  71.     }  
  72. }  
  73.   
  74. #pragma mark- 列表操作  
  75. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  76. {  
  77.     return 1;  
  78. }  
  79.   
  80.   
  81. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  82. {  
  83.     static NSString *CellName = @"CellName";  
  84.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellName];  
  85.     if (cell == nil)  
  86.     {  
  87.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellName];  
  88.         cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;  
  89.     }  
  90.       
  91.     NSURL *fileURL= nil;  
  92.     NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  93.     NSString *documentDir = [documentPaths objectAtIndex:0];  
  94.     NSString *path = [documentDir stringByAppendingPathComponent:[self.dirArray objectAtIndex:indexPath.row]];  
  95.     fileURL = [NSURL fileURLWithPath:path];  
  96.       
  97.     [self setupDocumentControllerWithURL:fileURL];  
  98.     cell.textLabel.text = [self.dirArray objectAtIndex:indexPath.row];  
  99.     NSInteger iconCount = [self.docInteractionController.icons count];  
  100.     if (iconCount > 0)  
  101.     {  
  102.         cell.imageView.image = [self.docInteractionController.icons objectAtIndex:iconCount - 1];  
  103.     }  
  104.       
  105.     return cell;  
  106. }  
  107.   
  108. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  109. {  
  110.     return [self.dirArray count];  
  111. }  
  112.   
  113.   
  114. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  115. {  
  116.     QLPreviewController *previewController = [[QLPreviewController alloc] init];  
  117.     previewController.dataSource = self;  
  118.     previewController.delegate = self;  
  119.       
  120.     // start previewing the document at the current section index  
  121.     previewController.currentPreviewItemIndex = indexPath.row;  
  122.     [[self navigationController] pushViewController:previewController animated:YES];  
  123.     //  [self presentViewController:previewController animated:YES completion:nil];  
  124. }  
  125.   
  126.   
  127.   
  128. #pragma mark - UIDocumentInteractionControllerDelegate  
  129.   
  130. - (NSString *)applicationDocumentsDirectory  
  131. {  
  132.     return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];  
  133. }  
  134.   
  135. - (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)interactionController  
  136. {  
  137.     return self;  
  138. }  
  139.   
  140.   
  141. #pragma mark - QLPreviewControllerDataSource  
  142.   
  143. // Returns the number of items that the preview controller should preview  
  144. - (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)previewController  
  145. {  
  146.     return 1;  
  147. }  
  148.   
  149. - (void)previewControllerDidDismiss:(QLPreviewController *)controller  
  150. {  
  151.     // if the preview dismissed (done button touched), use this method to post-process previews  
  152. }  
  153.   
  154. // returns the item that the preview controller should preview  
  155. - (id)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)idx  
  156. {  
  157.     NSURL *fileURL = nil;  
  158.     NSIndexPath *selectedIndexPath = [readTable indexPathForSelectedRow];  
  159.     NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  160.     NSString *documentDir = [documentPaths objectAtIndex:0];  
  161.     NSString *path = [documentDir stringByAppendingPathComponent:[self.dirArray objectAtIndex:selectedIndexPath.row]];  
  162.     fileURL = [NSURL fileURLWithPath:path];  
  163.     return fileURL;  
  164. }  
  165.   
  166.   
  167. - (void)didReceiveMemoryWarning  
  168. {  
  169.     [super didReceiveMemoryWarning];  
  170.     // Dispose of any resources that can be recreated.  
  171. }  
  172.   
  173. @end  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值