就像很多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, 希望對你們有所幫助