关于UIDocumentInteractionController使用问题

1、调用问题

在使用UIDocumentInteractionController的时候不能使用局部变量形式调用。
比如

        UIDocumentInteractionController * documentInteraction = [UIDocumentInteractionController interactionControllerWithURL:[NSURL URLWithString:fileLocalPath]];
        [documentInteraction presentOpenInMenuFromRect:self.view.bounds inView:self.view animated:YES];

这样调用后UIDocumentInteractionController 释放了界面就没了,你始终是调用不了。
解决方法:
你可以用成员变量形式来解决,代码如下

#pragma mark - Lazy
- (UIDocumentInteractionController *)documentInteraction{
    
    
    if (!_documentInteraction) {
    
    
        _documentInteraction = [[UIDocumentInteractionController alloc] init];
        _documentInteraction.delegate = self;
    }
    return _documentInteraction;
}

//调用
self.documentInteraction.URL = [NSURL fileURLWithPath:filesLocalPath];
[self.documentInteraction presentOpenInMenuFromRect:self.view.bounds inView:self.view animated:YES];

2、点击其他App分享问题

2.1 点击后界面弹出个空界面。

不要在代理函数中如以下函数中调用释放UIDocumentInteractionController

- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller

- (void)documentInteractionControllerDidDismissOptionsMenu:(UIDocumentInteractionController *)controller;

释放代码

self.documentInteraction = nil;

因为有一些第三方App(word、QQ浏览器)要跳入其他App打开文件的,跳过去的时候他们就帮你把其关闭了。
会调用以上代码中的函数, 那UIDocumentInteractionController 释放了,第三方就打不开了。
千万不要写释放,利用ARC让其释放。

2.2 先点击一个分享后,回到App再点击分享后没有反应。

这种情况只发生在点击分享后,这个分享调用第三方App,并且UIDocumentInteractionController 没有关闭(有些分享会关有些不会关)。例如使用word App分享文件就不会关闭UIDocumentInteractionController。然后你回到自己的App的时候再点击分享就没有用了。(目前发现微信App 也存在这个问题)
解决方法:

- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(nullable NSString *)application{
    
    
    [self.documentInteraction dismissMenuAnimated:YES];
}

在点击分享跳入第三方App 的时候统一关闭UIDocumentInteractionController。

猜你喜欢

转载自blog.csdn.net/sky_long_fly/article/details/120336651