菜鸟教程小白 发表于 2022-12-11 20:02:58

回到之前的 UIViewController 时 iOS App 崩溃


                                            <p><p>我在这个问题 (<a href="https://stackoverflow.com/questions/25747163/change-interface-orientation-when-back-button-is-pressed" rel="noreferrer noopener nofollow">link</a>) 中描述了同样的问题,但在我的情况下,我使用的是 <code>AVPlayerViewController</code> 而不是 <code>MPMoviePlayerController</code>,所以我尝试应用相同的答案的逻辑,但 <code>AVPlayer</code> 只有 unknow、readyToPlay 和 failed 状态,我不知道 Controller 何时关闭。我怎么知道 <code>AVPlayerViewController</code> 何时关闭或如何解决由方向引起的相同崩溃?</p>

<p>我的 ViewController 定位方法:</p>

<pre><code>override var shouldAutorotate : Bool {
    return false
}

override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.portrait
}
</code></pre>

<p>这是打开<code>AVPlayerViewController</code>的方法:</p>

<pre><code>func playVideo(at videoUrl: URL) {
    let controller = AVPlayerViewController()
    controller.player = AVPlayer(playerItem: AVPlayerItem(url: videoUrl))
    self.present(controller, animated: true, completion: {
      controller.player?.play()
    })
}
</code></pre>

<p>更新:</p>

<p>崩溃日志:</p>

<pre><code>Fatal Exception: UIApplicationInvalidInterfaceOrientation
0CoreFoundation               0x183dc2fe0 __exceptionPreprocess
1libobjc.A.dylib                0x182824538 objc_exception_throw
2CoreFoundation               0x183dc2f28 -
3UIKit                        0x189fe7f78 -
4UIKit                        0x18a8c5b68 -
5UIKit                        0x18a2421a4 -
6UIKit                        0x18a2689a4 -
7UIKit                        0x18a01dc04 -
8UIKit                        0x189fc5a98 -
9UIKit                        0x18a267c4c -
10 UIKit                        0x189fc55ec -
11 AVKit                        0x191667f28 -
12 AVKit                        0x19168d614 -
13 UIKit                        0x189f29010 -
14 UIKit                        0x189f28f90 -
15 UIKit                        0x189f13504 -
16 UIKit                        0x189f28874 -
17 UIKit                        0x189f28390 -
18 UIKit                        0x189f23728 -
19 UIKit                        0x189ef433c -
20 UIKit                        0x18a6ee014 __dispatchPreprocessedEventFromEventQueue
21 UIKit                        0x18a6e8770 __handleEventQueue
22 UIKit                        0x18a6e8b9c __handleHIDEventFetcherDrain
23 CoreFoundation               0x183d7142c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
24 CoreFoundation               0x183d70d9c __CFRunLoopDoSources0
25 CoreFoundation               0x183d6e9a8 __CFRunLoopRun
26 CoreFoundation               0x183c9eda4 CFRunLoopRunSpecific
27 GraphicsServices               0x185708074 GSEventRunModal
28 UIKit                        0x189f59058 UIApplicationMain
29 selftv                         0x10006c124 main (AppDelegate.swift:15)
30 libdyld.dylib                  0x182cad59c start
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>最佳解决方案是创建新的 <code>UIWindow</code> 并将 <code>AVPlayerViewController</code> 设置为 <code>rootViewController</code> 并为新窗口配置方向。
<br/>当应用程序仅用于纵向时,我将此解决方案用于具有横向和纵向方向的图片库。<br/>
示例:<br/></p>

<pre><code>UIWindow *modalWindow = [ initWithFrame:.bounds];
modalWindow.tag = kWindowTag;
modalWindow.rootViewController = &lt;your player vc&gt;;
;
</code></pre>

<p>但您需要在关闭 <code>AVPlayerViewController</code> 后设置 <code>modalWindow.hidden = yes</code> 并为上一个窗口调用 <code>makeKeyAndVisible</code>。</p>

<p>我使用 <code>#import <objc/runtime.h></code> 来覆盖 <code>presentViewController:animated:completion:</code> 和 <code>dismissViewControllerAnimated:completion:</code> 并在我的方法实现了显示/关闭新窗口的逻辑。</p>

<p>如果您需要更多示例或解释,请告诉我。
谢谢</p></p>
                                   
                                                <p style="font-size: 20px;">关于回到之前的 UIViewController 时 iOS App 崩溃,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/44923825/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/44923825/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: 回到之前的 UIViewController 时 iOS App 崩溃