iOS导入其他APP下载的文件(用其他应用打开)
今天给自己的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];
});