ios 怎么在第三方app uiactivitycontroller 出现自己app
2016-08-23 · 知道合伙人软件行家
关注
展开全部
1.自己的APP调用第三方打开文件
主要是使用 UIDocumentInteractionController 类 并实现 UIDocumentInteractionControllerDelegate 的代理方法
@interface HNDownFileViewController ()<UIDocumentInteractionControllerDelegate>
@property (nonatomic, strong) UIDocumentInteractionController *documentInteractionController;
@end
- (void)viewDidLoad {
[super viewDidLoad];
//url 为需要调用第三方打开的文件地址
NSURL *url = [NSURL fileURLWithPath:_dict[@"path"]];
_documentInteractionController = [UIDocumentInteractionController
interactionControllerWithURL:url];
[_documentInteractionController setDelegate:self];
[_documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
需要在真机上调试,例子中打开的是 doc文件,如果手机上装了WPS或者office套件,就能调用这些应用打开。
2.第三方APP调用自己的APP,打开文件
在info.plist中添加如下代码
1 <array>
2 <dict>
3 <key>CFBundleTypeName</key>
4 <string>com.myapp.common-data</string>
5 <key>LSItemContentTypes</key>
6 <array>
7 <string>com.microsoft.powerpoint.ppt</string>
8 <string>public.item</string>
9 <string>com.microsoft.word.doc</string>
10 <string>com.adobe.pdf</string>
11 <string>com.microsoft.excel.xls</string>
12 <string>public.image</string>
13 <string>public.content</string>
14 <string>public.composite-content</string>
15 <string>public.archive</string>
16 <string>public.audio</string>
17 <string>public.movie</string>
18 <string>public.text</string>
19 <string>public.data</string>
20 </array>
21 </dict>
22 </array>
这在系统中添加了参数,如果有以上类型的文件,第三方应用可以调用我们的APP进行操作。
在第三方调用我们的APP后,会调用如下方法
1 - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation
2 {
3 if (self.window) {
4 if (url) {
5 NSString *fileNameStr = [url lastPathComponent];
6 NSString *Doc = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/localFile"] stringByAppendingPathComponent:fileNameStr];
7 NSData *data = [NSData dataWithContentsOfURL:url];
8 [data writeToFile:Doc atomically:YES];
9 [XCHUDTool showOKHud:@"文件已存到本地文件夹内" delay:2.0f];
10 }
11 }
12 return YES;
13 }
主要是使用 UIDocumentInteractionController 类 并实现 UIDocumentInteractionControllerDelegate 的代理方法
@interface HNDownFileViewController ()<UIDocumentInteractionControllerDelegate>
@property (nonatomic, strong) UIDocumentInteractionController *documentInteractionController;
@end
- (void)viewDidLoad {
[super viewDidLoad];
//url 为需要调用第三方打开的文件地址
NSURL *url = [NSURL fileURLWithPath:_dict[@"path"]];
_documentInteractionController = [UIDocumentInteractionController
interactionControllerWithURL:url];
[_documentInteractionController setDelegate:self];
[_documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
需要在真机上调试,例子中打开的是 doc文件,如果手机上装了WPS或者office套件,就能调用这些应用打开。
2.第三方APP调用自己的APP,打开文件
在info.plist中添加如下代码
1 <array>
2 <dict>
3 <key>CFBundleTypeName</key>
4 <string>com.myapp.common-data</string>
5 <key>LSItemContentTypes</key>
6 <array>
7 <string>com.microsoft.powerpoint.ppt</string>
8 <string>public.item</string>
9 <string>com.microsoft.word.doc</string>
10 <string>com.adobe.pdf</string>
11 <string>com.microsoft.excel.xls</string>
12 <string>public.image</string>
13 <string>public.content</string>
14 <string>public.composite-content</string>
15 <string>public.archive</string>
16 <string>public.audio</string>
17 <string>public.movie</string>
18 <string>public.text</string>
19 <string>public.data</string>
20 </array>
21 </dict>
22 </array>
这在系统中添加了参数,如果有以上类型的文件,第三方应用可以调用我们的APP进行操作。
在第三方调用我们的APP后,会调用如下方法
1 - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation
2 {
3 if (self.window) {
4 if (url) {
5 NSString *fileNameStr = [url lastPathComponent];
6 NSString *Doc = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/localFile"] stringByAppendingPathComponent:fileNameStr];
7 NSData *data = [NSData dataWithContentsOfURL:url];
8 [data writeToFile:Doc atomically:YES];
9 [XCHUDTool showOKHud:@"文件已存到本地文件夹内" delay:2.0f];
10 }
11 }
12 return YES;
13 }
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询