应用间通信 - 文档导入导出实现
变更记录
序号 | 录入时间 | 录入人 | 备注 |
---|---|---|---|
1 | 2015-04-25 | Alfred Jiang | - |
2 | 2015-12-23 | Alfred Jiang | - |
方案名称
应用间通信 - 文档导入导出实现
关键字
应用间通信 \ 文档 \ 文档导入 \ 文档导出 \ 其他应用共享打开
需求场景
- 需要将自己应用内文档分享到其他应用打开时
- 需要自己的应用打开其他应用中的文档时
参考链接
详细内容
1. 导出自己应用内文档到其他应用打开
ViewController.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16//
// ViewController.h
// test
//
// Created by Alfred Jiang on 4/25/15.
// Copyright (c) 2015 Alfred Jiang. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIDocumentInteractionControllerDelegate>
- (IBAction)btnDisplayFiles:(id)sender;
- (void)openDocumentIn;
@endViewController.m
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//
// ViewController.m
// test
//
// Created by Alfred Jiang on 4/25/15.
// Copyright (c) 2015 Alfred Jiang. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong) UIDocumentInteractionController *documentController;
@end
@implementation ViewController
@synthesize documentController;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)btnDisplayFiles:(id)sender
{
[self openDocumentIn];
}
-(void)openDocumentIn {
NSString * filePath = [[NSBundle mainBundle] pathForResource:@"ee" ofType:@"pdf"];
documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
documentController.delegate = self;
documentController.UTI = @"com.adobe.pdf";
[documentController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
-(void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application {
NSLog(@"documentInteractionController : willBeginSendingToApplication");
}
-(void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application {
NSLog(@"documentInteractionController : didEndSendingToApplication");
}
-(void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller {
NSLog(@"documentInteractionControllerDidDismissOpenInMenu");
}
@end
2. 通过 iTunes 传输文档到手机并打开
在 info.plist 中增加 Application supports iTunes file sharing 为 YES (亦可设置 UIFileSharingEnabled 为 YES);
链接 iPhone 至 iTunes ,可在 iPhone -> Apps -> File Sharing 中看到自己应用;
在 iTunes 选中自己应用,点击 Add… 按钮可添加文档至自己应用中;
在自己应用中打开通过 iTunes 传输到应用中的文档
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//
// ViewController.h
// test
//
// Created by Alfred Jiang on 4/25/15.
// Copyright (c) 2015 Alfred Jiang. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIWebView *webView;
- (IBAction)btnDisplayFiles:(id)sender;
-(void)handleDocumentOpenURL:(NSURL *)url;
-(void)displayAlert:(NSString *) str;
-(void)loadFileFromDocumentsFolder:(NSString *) filename;
-(void)listFilesFromDocumentsFolder;
@end
//
// ViewController.m
// test
//
// Created by Alfred Jiang on 4/25/15.
// Copyright (c) 2015 Alfred Jiang. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)btnDisplayFiles:(id)sender {
[self listFilesFromDocumentsFolder];
}
- (void)handleDocumentOpenURL:(NSURL *)url {
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView setUserInteractionEnabled:YES];
[_webView loadRequest:requestObj];
}
-(void)loadFileFromDocumentsFolder:(NSString *) filename {
//---get the path of the Documents folder---
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory
stringByAppendingPathComponent:filename];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
[self handleDocumentOpenURL:fileUrl];
}
-(void)listFilesFromDocumentsFolder {
//---get the path of the Documents folder---
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList =
[manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSMutableString *filesStr =
[NSMutableString stringWithString:@"Files in Documents folder \n"];
for (NSString *s in fileList){
[filesStr appendFormat:@"%@ \n", s];
}
[self loadFileFromDocumentsFolder:@"ee.pdf"];
}
@end
3. 在其他应用中调用自己的应用打开系统支持的默认文档
- 在 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>PDF Document</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>LSItemContentTypes</key>
<array>
<string>com.adobe.pdf</string>
</array>
</dict>
</array>
ViewController 代码实现如下
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//
// ViewController.h
// test
//
// Created by Alfred Jiang on 4/25/15.
// Copyright (c) 2015 Alfred Jiang. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIWebView *webView;
-(void)handleDocumentOpenURL:(NSURL *)url;
@end
//
// ViewController.m
// test
//
// Created by Alfred Jiang on 4/25/15.
// Copyright (c) 2015 Alfred Jiang. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize webView;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)handleDocumentOpenURL:(NSURL *)url {
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView setUserInteractionEnabled:YES];
[self.webView loadRequest:requestObj];
}
@end在 Appdelegate.m 中增加如下代码
1
2
3
4
5
6-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
if (url != nil && [url isFileURL]) {
[(ViewController *)self.window.rootViewController handleDocumentOpenURL:url];
}
return YES;
}在支持 “用其他应用打开” 选项的应用中就可以看到自己的应用了
4. 在其他应用中调用自己的应用打开自定义文档
- 在 info.plist 中增加如下 字段
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<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>Sudoku Game Document</string>
<key>LSHandlerRank</key>
<string>Owner</string>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>LSItemContentTypes</key>
<array>
<string>net.learn2develop.offlinereader.sdk</string>
</array>
</dict>
</array>
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>public.data</string>
</array>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>testextension</string>
<key>public.mime-type</key>
<string>application/test</string>
</dict>
<key>UTTypeIdentifier</key>
<string>net.learn2develop.offlinereader.sdk</string>
<key>UTTypeDescription</key>
<string>Sudoku Game Document</string>
</dict>
</array>
- 在其他应用中选择 “用其他应用打开” 选项的应用中就可以看到自己的应用了
效果图
(无)
备注
(无)
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 宁静至远,博雅多通!