为什么 WKWebView 不显示视频? - Swift 3

Why WKWebView doesn't display videos? - Swift 3

我在 viewController 中(在 viewDidLoad() 中)以编程方式成功添加了 WKWebView。当加载包含视频的 url 时,它看起来很好,但是当我尝试点击它(播放它)时,我看不到它,但音频工作正常。

奇怪的是,我创建了一个新项目只是为了确保它能正常工作,而且确实如此,我复制了与 webView 显示视频完全相同的代码。

在转换为 Swift 3.

之前工作正常

这是点击视频时的样子:

点击前:

点击后:

我也尝试了另一个网页:

点击前:

点击后(注意现在状态栏是隐藏的):

简单来说就是代码:

override func viewDidLoad() {
    super.viewDidLoad()

    let web = WKWebView(frame: view.frame)
    let urlRequest = URLRequest(url: URL(string: "http://www.w3schools.com/html/html5_video.asp")!)       
    web.frame = view.frame
    web.load(urlRequest)

    view.addSubview(web)
 }

我尝试检查了很多没有任何输出的情况。我错过了什么?

提前致谢。

Considering you are using the URL as absoluteStrings and not for the local files.

这是您的答案的解决方案

App Transport Security 在 iOS9 版本中进行了修订。现在,您的应用程序可以免受不安全连接的影响。 iOS 强制建立安全连接。这在你的情况下可能是冲突的。 - author

来自Apple documentation

If your app needs to make a request to an insecure domain, you have to specify this domain in your app's Info.plist file

查看文档here

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>testdomain.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <true/>
            <key>NSExceptionMinimumTLSVersion</key>
            <string>TLSv1.2</string>
            <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
            <false/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <true/>
            <key>NSThirdPartyExceptionMinimumTLSVersion</key>
            <string>TLSv1.2</string>
            <key>NSRequiresCertificateTransparency</key>
            <false/>
        </dict>
    </dict>
</dict>

如果您的应用有充分的理由这样做,您也可以使用一个密钥忽略所有应用传输安全限制:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

您可以查看有关连接到 http 站点时相关的安全问题的更多信息。

希望对您有所帮助

完整归属作者和 SO 线程以及此答案中提到的链接

来自 SO

我认为视频正在您的 WKWebview 后面播放,您能否调试视图层次结构并 post 它? Xcode->Menu->Debug->View Debugging->Capture View Hierarchy播放视频时。

我尝试了一个新的 swift3 项目和你的代码,没问题,这是视图层次结构:

你可以看到,从左侧面板,AVPlayerView 在另一个 UIWindow 中,与 WKWebView 不同,所以我猜你的项目中包含 WKWebView 的 UIWindow 具有更高的 windowLevel,所以它显示在包含 AVPlayer.

的 UIWindow

并且通过使默认的 UIWindow 具有更高的 WindowLevel(UIWindowLevelAlert),我重现了您在项目中看到的内容。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    window?.windowLevel = UIWindowLevelAlert
    return true
}

初始化webview时,需要传入两个配置属性。示例:

let webConfiguration = WKWebViewConfiguration()
    webConfiguration.allowsInlineMediaPlayback = true
    webConfiguration.mediaTypesRequiringUserActionForPlayback = []

    webView = WKWebView(frame: .zero, configuration: webConfiguration)

您还需要在 info.plist:

中允许任意加载
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoadsForMedia</key>
    <true/>
</dict>