5

I successfully added a WKWebView programmatically In a viewController (in viewDidLoad()). When loading a url that contains a video it appears fine, but when I try to tap on it (playing it), I cannot see it, but the audio works fine.

The weird thing is I created a new project just to make sure it should works fine and it did, I copied the same exact code the webView displayed the video as it should.

It was working fine before converting to Swift 3.

This is how it looks when tapping on the video:

Before tapping:

enter image description here

After Tapping:

enter image description here

I also tried another web page:

Before tapping:

enter image description here

After Tapping (Note that the status bar is hidden now):

enter image description here

Simply, this is the code:

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)
 }

I tried to check many cases without any output. What am I missing?

Thanks in advance.

Ekta Padaliya
  • 5,743
  • 3
  • 39
  • 51
A.Munzer
  • 1,890
  • 1
  • 16
  • 27
  • Can you init your `WKWebView` [with configuration](https://developer.apple.com/reference/webkit/wkwebview/1414998-init), with [allowsInlineMediaPlayback](https://developer.apple.com/reference/webkit/wkwebviewconfiguration/1614793-allowsinlinemediaplayback) set to true? It's just wild guess, it may not work. – user28434'mstep Oct 18 '16 at 11:27
  • I tried it, it's not working :/ – A.Munzer Oct 18 '16 at 11:39

3 Answers3

6

When initialising the webview, you need to pass in two configuration properties. Example:

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

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

You also need to allow arbitrary loads in info.plist:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoadsForMedia</key>
    <true/>
</dict>
Per Quested Aronsson
  • 11,380
  • 8
  • 54
  • 76
  • I had an issue where a pre-roll ad was being played fine but then the actual video wouldn't play after. This fixed my issue, thanks! – FSUWX2011 Feb 12 '19 at 17:50
  • @PerQuestedAronsson when you enable `NSAllowsArbitraryLoadsForMedia`, aren't you required to justify why it to App Review? – Lance Samaria Apr 13 '22 at 14:41
5

I think the video is playing behind your WKWebview, could you please debug the view hierarchy and post it? Xcode->Menu->Debug->View Debugging->Capture View Hierarchy when playing the video.

I've tried with a new swift3 project and with your code, there is no problem, here is the view hierarchy:

enter image description here

You can see that from left pannel, the AVPlayerView is in another UIWindow, different from WKWebView,So I guess the UIWindow which contains the WKWebView in your project has a higher windowLevel so it shows above the UIWindow which contains AVPlayer.

And by making the default UIWindow a higher WindowLevel(UIWindowLevelAlert),I reproduced what you've seen in your project.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    window?.windowLevel = UIWindowLevelAlert
    return true
}
xfdai
  • 2,735
  • 1
  • 19
  • 18
  • Thanks alot! the problem is not exactly the same that I face, but it is related to the window level, it sound like there is a third party library changes the window level. I think that you will get the bounty :) – A.Munzer Oct 27 '16 at 06:36
  • 1
    @AbdallaAlHajHasan Glad it helped, in case you still not figured out which library is changing the window level, try to debug the view hierarchy, you may get some clues. – xfdai Oct 27 '16 at 07:11
  • 1
    @AbdallaAlHajHasan could you please also mark it as answer if it answered your question :) – xfdai Oct 27 '16 at 09:10
  • In my case Crashlytics library was displaying alert about new version of app available. Instead of displaying it as regular modal it was creating new UIWindow which then was marked as "key" the whole time. – Roval Nov 08 '18 at 15:08
2

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

Here is a solution to your answer

App Transport Security revised in iOS9 release. Now onwards your application is safe from un secure connection. And iOS forces to make secure connection. This can be conflict in your case. - author SO Thread Reference

From 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

See the documentation 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>

You can also ignore all app transport security restrictions with a single key, if your app has a good reason to do so:

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

You can check more about the Security issues which are associated while connecting to the http sites.

Hope it may help you a bit

Full attribution goes to the authors and the SO threads and the links mentioned in this answer

From the SO thread

Community
  • 1
  • 1
Pritish Vaidya
  • 21,561
  • 3
  • 58
  • 76