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];
});
iOS导入其他APP下载的文件(用其他应用打开)的更多相关文章
- 下载apk文件浏览器会直接打开并显示乱码的问题
今天同事反映他的apk文件在自己的老项目中下载有问题:下载apk文件浏览器会直接打开并显示乱码,在别的项目中就没有问题. 后分析response的content-type发现,老项目的类型是text/ ...
- 微信不支持App下载的解决方案 微信跳转打开外部浏览器下载(苹果跳转商店下载)
在微信中,打开app下载链接,或者使用微信扫一扫app下载二维码,都是无法下载app的. 因为腾讯为了自身利益,屏蔽了其他app直接在微信中下载.下面给分享下,找到的2种有效的解决方案. 方案:点击链 ...
- 【IOS】iOS 企业版应用网站下载plist文件
如果想从自己公司的网站上下载安装应用,首先 准备一个 index.html文件 <!DOCTYPE html> <html lang="zh-cn"> &l ...
- ios开发,app调用资源文件到C++的方法
为了读取资源文件到cpp.供opencv处理,采取的方式是把之前的cpp文件的后缀改成:.mm 然后会出现各种报错,原因是因为命名冲突,前面加上cv::就行. const char* imagePat ...
- 分分钟解决iOS开发中App启动广告的功能
前不久有朋友需要一个启动广告的功能,我说网上有挺多的,他说,看的不是很理想.想让我写一个,于是乎,抽空写了一个,代码通俗易懂,简单的封装了一下,各种事件用block回调的,有俩种样式的广告,一种是全屏 ...
- 微信扫描打开APP下载链接提示代码优化
上一次我发了一篇文章叫“微信打开网址添加在浏览器中打开提示”,里面我发出来了三个代码,分别是纯JS.js+html.jQuery+HTML代码.今天来一个简化版带可以关闭的按钮操作.使用的是纯JS+H ...
- 微信扫描打开APP下载链接提示代码优化(转)
上一次我发了一篇文章叫“微信打开网址添加在浏览器中打开提示”,里面我发出来了三个代码,分别是纯JS.js+html.jQuery+HTML代码.今天来一个简化版带可以关闭的按钮操作.使用的是纯JS+H ...
- PHP下载远程文件的3种方法以及性能考虑
今天在做导出Excel的时候,总是要测试导出的Excel文件,频繁的下载和打开,很麻烦 就想着写段代码一气呵成 服务端导出Excel==>下载Excel文件到本地==>并打开的操作. 这 ...
- 给iOS 模拟器“安装”app文件
前言 刚刚接触iOS的时候,我就一直很好奇,模拟器上面能不能直接安装app呢?如果可以,我们就直接在模拟器上面聊QQ和微信了.直到昨天和朋友们聊到了这个话题,没有想到还真的可以给模拟器“安装”app! ...
随机推荐
- Docker Swarm(八)滚动更新、回滚服务
滚动更新.回滚服务 默认情况下, swarm一次只更新一个副本,并且两个副本之间没有等待时间,我们可以通过: # 定义并行更新的副本数量--update-parallelism# 定义滚动更新的时间间 ...
- shell基础之变量及表达式
本节内容 1. shell变量简介 2. 定义变量 3. 使用变量 4. 修改变量的值 5. 单引号和双引号的区别 6. 将命令的结果赋值给变量 7. 删除变量 8. 变量类型 9. 特殊变量列表 1 ...
- Lua中的元表与元方法学习总结
前言 元表对应的英文是metatable,元方法是metamethod.我们都知道,在C++中,两个类是无法直接相加的,但是,如果你重载了"+"符号,就可以进行类的加法运算.在Lu ...
- 用PHP爬取知乎的100万用户
http://blog.jobbole.com/88788/ 突然发现 大数据 Python的爬虫能力很强 爬取到的数据 直接可以用于维修QQ营销 精准营销
- centos7网卡配置文件详解与固定服务器ip
环境:Centos7.3(最小安装方式安装) 查看自动获取的IP地址 ip addr 更改网卡配置,配置静态IP 网卡配置文件位置:/etc/sysconfig/network-scripts/ifc ...
- 使用指定源安装python包
对于经常需要按照那个python包的同学,外网下载比较慢的话,可以使用公司内部的镜像进行安装 eg: pip install django -i http://mirrors.***.com.cn/p ...
- runtime系统的Cello
runtime系统的Cello 通过充当一个现代的.功能强大的runtime系统,Cello使许多以前在C中不切实际或笨拙的事情变得简单,例如: 通用数据结构 多态函数 接口/类型类 构造函数/析构函 ...
- 基于TensorRT优化的Machine Translation
基于TensorRT优化的Machine Translation 机器翻译系统用于将文本从一种语言翻译成另一种语言.递归神经网络(RNN)是机器翻译中最流行的深度学习解决方案之一. TensorRT机 ...
- python_selenium 框架代码的优化方向
- 【题解】【洛谷 P1967】 货车运输
目录 洛谷 P1967 货车运输 原题 题解 思路 代码 洛谷 P1967 货车运输 原题 题面请查看洛谷 P1967 货车运输. 题解 思路 根据题面,假设我们有一个普通的图: 作图工具:Graph ...