惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
U
Unit 42
T
Tailwind CSS Blog
罗磊的独立博客
WordPress大学
WordPress大学
小众软件
小众软件
Recent Announcements
Recent Announcements
博客园 - 聂微东
Jina AI
Jina AI
云风的 BLOG
云风的 BLOG
博客园 - 【当耐特】
爱范儿
爱范儿
Microsoft Azure Blog
Microsoft Azure Blog
GbyAI
GbyAI
V
V2EX
博客园 - 三生石上(FineUI控件)
I
InfoQ
雷峰网
雷峰网
G
Google Developers Blog
阮一峰的网络日志
阮一峰的网络日志
B
Blog
腾讯CDC
A
About on SuperTechFans
博客园 - 叶小钗

少数派

派早报:Google 发布 Fitbit Air 等 - 少数派 「新人报到」確認需求,再開始 - 少数派 从 SOLO 独立开发者社区,我看到了越来越多开发者开始做自己的产品 - 少数派 我怎么管理那些"不常做,但总会忘"的生活事项 - 少数派 人形机器人量产元年,数据才是具身智能的“生死线” - 少数派 BuhoLaunchpad 高度还原 Mac 启动台:开发历程与思考 - 少数派 五年陪伴依然不舍,DIY 换壳后让罗技 MX Master 3 继续服役 - 少数派 新玩意 240|少数派的编辑们最近买了啥? - 少数派 一日一技|为什么你应该关闭 iOS 的键盘声音 - 少数派 我做了个插件和 Skills,一键提取任何网站的设计规范 Design.md - 少数派 住在三四线城市的你,该开始录播客了 - 少数派 甘南秘境,大白高国 - 少数派 AI的审美:谁让把我变成川内倫子 - 少数派 返工怎能不烦恼,打工人片单总有一部是你的「嘴替」 - 少数派 为了让「上厕所」更健康,我做了一个小工具 - 少数派 AI + Skill,能够让生成的文章去除 AI 味吗? - 少数派 新玩意|韶音OpenDots ONE 耳夹式耳机 - 少数派 《美满》| 在每一个春天的晚上相爱(362) - 少数派 新玩意|优篮子 PS01 MagSnap 磁吸支架 - 少数派 自我整合手记 | 我开始早睡了:用稳定规则,为自由托底 - 少数派 用龙虾(OpenClaw)两个多月,我最深的12个体会 - 少数派 听歌时间到,12 张你可能错过的 2025 华语乐坛好专辑 - 少数派 承诺能追吗 - 少数派 macOS 26启动台没了? 我做了个不一样的App启动器 - Keboard - 少数派 《四海为家的人》| INTJ对话INTJ(361) - 少数派 你发过的那些黑历史,是时候一次清干净了 - 少数派 新玩意:安安静静玩,越玩越专注:计客密码机 - 少数派 iPad 用户首次体验 Android 平板:vivo Pad6 Pro - 少数派 数据逻辑强 - 少数派 极北行+ | 一路向北,探访日本至北之地 | 001 - 少数派
iOS导入其他APP下载的文件(用其他应用打开) - 少数派
2021-08-19 · via 少数派

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