我有一个非常标准的 UIActivityViewController 实现。当我使用 Twitter 或 Facebook 时, View Controller 被关闭,应用程序继续工作。但是,当我通过电子邮件或文本发送相同的内容时, View Controller 被关闭,但应用程序卡住(而不是崩溃)。一切仍在屏幕上,但卡住 - 没有输入等。
也许邮件或消息应用程序尚未将控制权释放回我的应用程序?有没有办法使用 Instruments 来分析发生了什么?
谢谢!
作为违规者,我从 NSArray 的这部分泄漏
- (void)postToFacebookUITapGestureRecognizer *)sender
{
NSString *postText = @"Testing";
UIImage *imageToPost = [self captureTheScreenImage];
NSArray *postItems = @[postText, imageToPost];
UIActivityViewController *activityPostVC = [[UIActivityViewController alloc]initWithActivityItems:postItems applicationActivities:nil];
NSArray *excludedItems = @[UIActivityTypePostToWeibo,UIActivityTypePrint,UIActivityTypeCopyToPasteboard,UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll, UIActivityTypeMail, UIActivityTypeMessage];
[activityPostVC setExcludedActivityTypes:excludedItems];
[self presentViewController:activityPostVC animated:YES completion:nil];
}
同时使用多个 UIWindow
对象时,我也遇到了这个问题。
UIActivityViewController
关闭后,显示的窗口内容不会正确恢复。具体来说,窗口的第一个 subview (UILayoutContainerView
) 缺少对父 View 的约束。这会导致窗口 UILayoutContainerView
的宽度为零,导致窗口显示为透明并显示其下方的窗口但不允许用户交互。
解决方法是在当前窗口的顶部放置一个空的透明窗口,并从与该新窗口关联的空 View Controller 中显示 UIActivityViewController
。当 UIActivityViewController
被解除时,我们可以处理它出现的空窗口。
import Foundation
private var previousWindow: UIWindow?
private var activityViewControllerWindow: UIWindow?
extension UIViewController {
fileprivate var isActivityViewControllerWindowPresented: Bool {
return activityViewControllerWindow?.isKeyWindow ?? false
}
func presentActivityViewController(_ activityViewController: UIActivityViewController, animated: Bool = true, completion: (() -> Void)? = nil) {
if isActivityViewControllerWindowPresented {
return
}
let window = UIWindow(frame: view.window!.frame)
previousWindow = UIApplication.shared.keyWindow
activityViewControllerWindow = window
window.rootViewController = UIViewController()
window.makeKeyAndVisible()
let activityCompletionClosure = activityViewController.completionWithItemsHandler
activityViewController.completionWithItemsHandler = { [weak self] (activityType, completed, returnedItems, activityError) in
self?.cleanUpActivityViewControllerWindow()
activityCompletionClosure?(activityType, completed, returnedItems, activityError)
}
window.rootViewController?.present(activityViewController, animated: animated, completion: completion)
}
fileprivate func cleanUpActivityViewControllerWindow() {
previousWindow?.makeKeyAndVisible()
activityViewControllerWindow?.rootViewController = nil
activityViewControllerWindow = nil
}
}
关于ios - UIActivityViewController 在电子邮件或文本而不是 fb 或 Twitter 时卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16909402/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |