热门标签 | HotTags
当前位置:  开发笔记 > 后端 > 正文

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

今天要实现一个功能,通过iTunes导入文件到应用中,并且在应用中对这个文件进行编辑。类似我们平时经常使用的PDF阅读器那样的东西,我们可以自己导入我们的电子书。源码下载:http

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

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


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

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

先看效果图。

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

];    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

           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow


推荐阅读
author-avatar
飞天6585_439
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有