ios - UIActivityViewController 在电子邮件或文本而不是 fb 或 Twitter 时卡住
<p><p>我有一个非常标准的 UIActivityViewController 实现。当我使用 Twitter 或 Facebook 时, ViewController 被关闭,应用程序继续工作。但是,当我通过电子邮件或文本发送相同的内容时, ViewController 被关闭,但应用程序卡住(而不是崩溃)。一切仍在屏幕上,但卡住 - 没有输入等。</p>
<p>也许邮件或消息应用程序尚未将控制权释放回我的应用程序?有没有办法使用 Instruments 来分析发生了什么?</p>
<p>谢谢!</p>
<p>作为违规者,我从 NSArray 的这部分泄漏</p>
<pre><code>- (void)postToFacebook:(UITapGestureRecognizer *)sender
{
NSString *postText = @"Testing";
UIImage *imageToPost = ;
NSArray *postItems = @;
UIActivityViewController *activityPostVC = [initWithActivityItems:postItems applicationActivities:nil];
NSArray *excludedItems = @;
;
;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>同时使用多个 <code>UIWindow</code> 对象时,我也遇到了这个问题。</p>
<p><code>UIActivityViewController</code> 关闭后,显示的窗口内容不会正确恢复。具体来说,窗口的第一个 subview(<code>UILayoutContainerView</code>) 缺少对父 View 的约束。这会导致窗口 <code>UILayoutContainerView</code> 的宽度为零,导致窗口显示为透明并显示其下方的窗口但不允许用户交互。</p>
<p>解决方法是在当前窗口的顶部放置一个空的透明窗口,并从与该新窗口关联的空 ViewController 中显示 <code>UIActivityViewController</code>。当 <code>UIActivityViewController</code> 被解除时,我们可以处理它出现的空窗口。</p>
<pre><code>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 = { (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
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - UIActivityViewController 在电子邮件或文本而不是 fb 或 Twitter 时卡住,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/16909402/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/16909402/
</a>
</p>
页:
[1]