今天給自己的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];
});