今天给自己的APP新增了一个小功能 可以打开iOS其他APPTXT 文件,一个很小的功能,做阅读APP的小伙伴不要错过。

附上APP地址: 一阅阅读

有想看小说的小伙伴可以试下 支持换源 支持自定义书源

效果如下

1.编辑info.plst

<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeIconFiles</key>
<array>
<string>icon.png</string>
<string>icon@2x.png</string>
</array>
<key>CFBundleTypeName</key>
<string>com.myapp.common-data</string>
<key>LSItemContentTypes</key>
<array>
//我只导入txt 所以只加了text
<string>public.text</string>
//以下区域是我查到的但是我没有加
<string>public.data</string>
<string>com.microsoft.powerpoint.ppt</string>
<string>public.item</string>
<string>com.microsoft.word.doc</string>
<string>com.adobe.pdf</string>
<string>com.microsoft.excel.xls</string>
<string>public.image</string>
<string>public.content</string>
<string>public.composite-content</string>
<string>public.archive</string>
<string>public.audio</string>
<string>public.movie</string>
</array>
</dict>
</array>

2.在APPdelegate接收文件地址

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
if (self.window) {
if (url) {
NSString *fileName = url.lastPathComponent; // 从路径中获得完整的文件名(带后缀)
// path 类似这种格式:file:///private/var/mobile/Containers/Data/Application/83643509-E90E-40A6-92EA-47A44B40CBBF/Documents/Inbox/jfkdfj123a.pdf
NSString *path = url.absoluteString; // 完整的url字符串
path = [self URLDecodedString:path]; // 解决url编码问题 NSMutableString *string = [[NSMutableString alloc] initWithString:path]; if ([path hasPrefix:@"file://"]) { // 通过前缀来判断是文件
// 去除前缀:/private/var/mobile/Containers/Data/Application/83643509-E90E-40A6-92EA-47A44B40CBBF/Documents/Inbox/jfkdfj123a.pdf
[string replaceOccurrencesOfString:@"file://" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, path.length)]; // 此时获取到文件存储在本地的路径,就可以在自己需要使用的页面使用了
NSDictionary *dict = @{@"fileName":fileName,
@"filePath":string};
[[NSNotificationCenter defaultCenter] postNotificationName:TJBookStackWillImportTXTNotification object:nil userInfo:dict]; return YES;
}
}
}
return YES;
} // 当文件名为中文时,解决url编码问题
- (NSString *)URLDecodedString:(NSString *)str {
NSString *decodedString=(__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)str, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)); return decodedString;
}

3.在主页面对接收到的数据进行处理

不建议在APPdelegate进行处理 会拖慢进入APP速度

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addLoacalBookWithFilePath:) name:TJBookStackWillImportTXTNotification object:nil];

-(void)addLoacalBookWithFilePath:(NSNotification *)notification{

    [SVProgressHUD showInfoWithStatus:@"正在后台解析书籍"];
//启动线程进行处理
dispatch_queue_t conCurrentQueue = dispatch_queue_create("importBook", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(conCurrentQueue, ^{
NSDictionary *dic = notification.userInfo;
NSString *filePath = dic[@"filePath"]; NSString *bookId = [NSString stringWithFormat:@"%@", @([[NSDate date] timeIntervalSince1970] * 1000)]; [TJReadParser parseLocalBookWithFilePath:filePath bookId :bookId success:^(NSArray<TJChapterModel *> * _Nonnull chapters) {
// 创建书籍模型
TJBookModel *bookModel = [[TJBookModel alloc] init];
bookModel.bookType = TJBookTypeLocal;
bookModel.bookName = filePath.lastPathComponent;
// 本地书随机生成ID
bookModel.bookId = bookId;
bookModel.chapterCount = chapters.count;
for (TJChapterModel *chapter in chapters) {
chapter.bookId = bookModel.bookId;
}
[TJReadHelper addToBookStackWithBook:bookModel complete:^{
[TJChapterDataManager insertChaptersWithModels:chapters];
[SVProgressHUD showSuccessWithStatus:@"书籍已经成功加入书架"];
[[NSNotificationCenter defaultCenter] postNotificationName:TJBookStackDidChangeNotification object:nil userInfo:nil]; }];
} failure:^(NSError *error) {
[SVProgressHUD showErrorWithStatus:error.userInfo[NSUnderlyingErrorKey]];
}]; }); }

4.更新页面UI

记得回归线程

 dispatch_async(dispatch_get_main_queue(), ^{
self.dataSource = [[TJReadRecordManager allBooksInStack] copy];
[self.booksTableView reloadData];
});

最新文章

  1. iOS开发CoreGraphics核心图形框架之一——CGPath的应用
  2. 天气预报API开发
  3. CYQ.Data+EasyUI开发:几个相关的问题CheckBox、Tree、TreeGrid
  4. 网站移植到linux上后常犯的错误
  5. 一个Activity掌握Design新控件 (转)
  6. EditText小技巧
  7. hdu 4739 Zhuge Liang&#39;s Mines
  8. Java中I/O的分析
  9. Spring HTTP invoker 入门
  10. python笔记之Cmd模块
  11. org.springframework.core.Ordered接口
  12. NOIP2017SummerTraining0713
  13. Fork/Join-Java并行计算框架
  14. directX显示采集源(摄像头)filter
  15. 【git】git hello world
  16. zabbix3.2添加web页面监控(Web monitoring)
  17. JAVA实现概率计算(数字不同范围按照不同几率产生随机数)
  18. Atitit http2 新特性
  19. Codeforces 832D - Misha, Grisha and Underground
  20. Linux kernel 生成 /dev/spidev*

热门文章

  1. 搭建LAMP环境部署Ecshop电商网站
  2. 将.netcore5.0(.net5)部署在Ubuntu的docker容器中
  3. JLINK下载出现问题
  4. GO语言面向对象06---面向对象练习01
  5. Python+Selenium - 鼠标操作
  6. ML Pipelines管道
  7. AlexeyAB DarkNet YOLOv3框架解析与应用实践(三)
  8. TVM设计与构架构建
  9. CVPR2020:训练多视图三维点云配准
  10. CVPR2020:利用图像投票增强点云中的三维目标检测(ImVoteNet)