[绍棠] iOS开发- 文件共享(利用iTunes导入文件, 并且显示已有文件) 以及 iOS App与iTunes文件传输的方法和对iOS App文件结构的说明
来源:互联网 发布:知乎hfp护肤品怎么样 编辑:程序博客网 时间:2024/06/05 12:50
就像很多iOS上面的播放器App一样,本文编写一个程序可以通过iTunes往里面放文件,比如编写一个视频播放器程序,通过itune往里面放视频文件,然后通过这个App来播放这个视频。下面是通过iTunes往App传输文件的截图:
实现过程:
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
这个iTunes同步相对来说比较简单, 接下来可以关注我的实现WIFI局域网传输文件到iPhone, 希望对你们有所帮助
- //
- // 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
1 0
- [绍棠] iOS开发- 文件共享(利用iTunes导入文件, 并且显示已有文件) 以及 iOS App与iTunes文件传输的方法和对iOS App文件结构的说明
- iOS App与iTunes文件传输的方法和对iOS App文件结构的说明
- iOS开发- 文件共享(利用iTunes导入文件, 并且显示已有文件)
- iOS开发- 文件共享(利用iTunes导入文件, 并且显示已有文件)
- iOS开发- 文件共享(利用iTunes导入文件, 并且显示已有文件)
- iOS开发- 文件共享(利用iTunes导入文件, 并且显示已有文件)
- iOS开发- 文件共享(利用iTunes导入文件, 并且显示已有文件)
- iOS App通过itunes共享文件
- IOS App开启iTunes文件共享
- IOS App开启iTunes文件共享
- IOS App开启iTunes文件共享 去documen的内容
- 让自己开发的iOS App允许用户通过itunes共享文件
- 自己开发的iOS App允许用户通过itunes共享文件
- 让自己开发的iOS App通过iTunes共享文件到Document目录
- app itunes 文件共享
- itunes 共享app文件
- iOS开启iTunes文件共享
- iOS开发:iTunes导入本地视频(文件共享)
- 用类描述坦克
- Ping++SDK接入
- 安卓在Activity中获取Fragment中的控件
- 工作中最全最常用的正则表达式
- c++ 常量表达式
- [绍棠] iOS开发- 文件共享(利用iTunes导入文件, 并且显示已有文件) 以及 iOS App与iTunes文件传输的方法和对iOS App文件结构的说明
- 孤儿进程与僵尸进程[总结](转)
- Navicat中常用到的实用工具
- 如何自定义验证器?
- Sublime Text 关闭自动更新提示
- abort()函数
- hibernate addEntity setResultTransformer
- 目标检测(1)
- Activity卡顿、变慢的原因