ios-打开其他app里的文件

1.info.plist里面配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>downloadPDF</string>
<key>LSHandlerRank</key>
<string>Default</string>
<key>LSItemContentTypes</key>
<array>
<string>com.adobe.pdf</string> //pdf
<string>com.microsoft.word.doc</string> //doc
<string>org.openxmlformats.wordprocessingml.document</string> //docx
</array>
</dict>
</array>

2.appdelegate
在- (BOOL)application:(UIApplication )app openURL:(NSURL *)url options:(NSDictionary<NSString, id> *)options方法里

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  if (url && [url isFileURL]) {
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 = [path stringByRemovingPercentEncoding]; // 解决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)];
if( [DXAccountSave account]){
unsigned long long size = [[NSFileManager defaultManager] attributesOfItemAtPath:string error:nil].fileSize;
size = (size*1.0)/(1024*1024);
if(size >= 2){
[SVProgressHUD showErrorWithStatus:@"上传简历请小于2M"];
[SVProgressHUD dismissWithDelay:4];
}else{
AppDelegate * delegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
[delegate toSelectRoot:0];
AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
NSString *importUrl = [H5_SERVER stringByAppendingString:@"/interface/files/can_upload_file"];
[manager POST:importUrl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSString*info= [responseObject objectForKey:@"info"];
if([info isEqualToString:@"can_create"]){
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"上传简历" message:@"将简历上传到超级简历" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[alert dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[SVProgressHUD showWithStatus:@"正在上传"];
AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
NSMutableDictionary *parameter = [NSMutableDictionary dictionary];
parameter[@"file"] = [[NSFileManager defaultManager] contentsAtPath:string];
parameter[@"file_name"] = fileName;
NSString *importUrl = [H5_SERVER stringByAppendingString:@"/interface/files/upload_file"];
[manager POST:importUrl parameters:parameter constructingBodyWithBlock:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
importFileViewController *importFile = [[importFileViewController alloc] init];
importFile.fileName = fileName;
importFile.currentRoute = [[responseObject objectForKey:@"file"] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
UINavigationController * navC = [[UINavigationController alloc]initWithRootViewController:importFile];
[self.window.rootViewController presentViewController:navC animated:YES completion:nil];

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[SVProgressHUD showErrorWithStatus:@"网络繁忙, 请稍后再试"];
}];
}];
[alert addAction:defaultAction];
[alert addAction:confirmAction];
UIWindow *alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
alertWindow.rootViewController = [[UIViewController alloc] init];
alertWindow.windowLevel = UIWindowLevelAlert + 1;
[alertWindow makeKeyAndVisible];
[alertWindow.rootViewController presentViewController:alert animated:YES completion:nil];
}else if ([info isEqualToString:@"no_user"]){
[Login showLoginInController:self.window.rootViewController WhenComplete:^{
AppDelegate * delegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
[delegate toSelectRoot:0];
[DXSave Save:@"showAd" with:@"yes"];
[SVProgressHUD showSuccessWithStatus:@"登录成功 请重新上传文件"];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if(![[DXSave Get:@"hideLoading"] isEqualToString:@"yes"]){
[SVProgressHUD showWithStatus:@"加载中"];
}
});
}];
}else{
[SVProgressHUD showErrorWithStatus:info];
[SVProgressHUD dismissWithDelay:2.0];
NSDictionary *dictionary = @{@"fileName":fileName, @"filePath":string};
[[NSNotificationCenter defaultCenter]postNotificationName:@"toMember" object:self userInfo:dictionary];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[SVProgressHUD showErrorWithStatus:@"网络繁忙, 请稍后再试"];
}];
}
}else{
[Login showLoginInController:self.window.rootViewController WhenComplete:^{
AppDelegate * delegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
[delegate toSelectRoot:0];
[DXSave Save:@"showAd" with:@"yes"];
[SVProgressHUD showSuccessWithStatus:@"登录成功 请重新上传文件"];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if(![[DXSave Get:@"hideLoading"] isEqualToString:@"yes"]){
[SVProgressHUD showWithStatus:@"加载中"];
}
});
}];
}
return YES;
}
}