Keeping video playing within the webView instead of presenting fullscreen? - swift

I know in iOS 10 you can pinch the video player to keep the video playing within the webview. Is there any way to set the webview so that the video player doesn't open in full screen on default?

webView.allowsInlineMediaPlayback = true
is what I believe you are looking for

The following is working for in WKWebView in swift 4.1
The main part of the WKWebView in WKwebviewConfiguration
wkwebView.navigationDelegate = self
wkwebView.allowsBackForwardNavigationGestures = true
self.wkwebView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
let config = WKWebViewConfiguration()
config.allowsInlineMediaPlayback = true
wkwebView = WKWebView(frame: wkwebView.frame, configuration: config)
self.view.addSubview(wkwebView)
self.wkwebView.load(NSURLRequest(url: URL(string: self.getUrl())!) as URLRequest)

Related

Scaling content in WKWebView

I recently started working on WKWebView and have minimum knowledge about it.
I want to know how to scale the content in the WKWebView so that it fits into the given view size without scrolling.
I have tried setting the height of the view to be screen size and load some large html content to the WKWebView and disable scrolling. Then only a part of the content is shown.
Then I searched for the approach to scale the content of the WKWebView so that the entire content is seen without need for scrolling.
Many people have posted questions regarding this but some questions were still unanswered and some other solutions did not work for me.
For the WKWebView try the following three options depending upon your requirement:
webview being the webview in your file
webView.contentMode = .scaleAspectFill
or
webView.contentMode = .scaleAspectFit
or
webView.contentMode = .scaleToFill
Given a webView object of type WKWebView you can do
webView.contentMode = .scaleToFill
This 'contentMode' will work after set webview frame correctly (need to fit to the device).
var webView: WKWebView!
func setupView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: CGRect(x: 0, y: 0, width: view.bounds.size.width, height: view.bounds.size.height), configuration: webConfiguration)
webView.uiDelegate = self
webView.contentMode = .scaleAspectFit
webView.sizeToFit()
webView.autoresizesSubviews = true
self.view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
setupView()
}

Resize WebView for ipad swift 3

I have a First View, where when I click on one button, it opens an UIWebView,
I want this webView to take only 3/4 of my view when I look at it from an Ipad, if it's an Iphone I want to get a full screen view.
Here is my code :
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
I try the method init with arbitrary values frame: .init(x: 20, y: 20, width: 20, height: 20)
But it's still taking full screen, for detect if it's an Ipad I use this condition
if (UIScreen.main.traitCollection.userInterfaceIdiom.rawValue == 1)
{
// if here I'm an Ipad I can resize my webview
webView.frame.size.height = view.frame.height - // some number
}
Isn't this exactly what adaptive user interfaces and size classes in Interface Builder is for? Have a look at this WWDC video from 2014.

WebView in SpriteKit

Working on MacOS and SpriteKit and looking to add a WKWebView. I would think that you could just add one like so.
webView = WKWebView(frame: NSRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
let url = URL(string: "http://www.llbean.com")
let request = URLRequest(url: url!)
webView.load(request)
view.addSubview(web)
Doing that in the top level ViewController doesn't do anything. Is there a better way to float a WebView above the SKView?
This question is at the center of my goal to convert my Game website to an iOS App.
So, I also need to insert my WKWebView into a new empty SKScene and that I do this creation within Xcode’s GameViewController routine loadView(). It is here I need to create my empty SKScene.
And this precisely what I do not know how to do.
It’s essential I do this because then I can use
myEmptyScene.sceneDidLoad() {
// the above URL code here
checkGamePadConnection() // my custom method
}
The reason I insert the above URL code into the SKScene is that others insist that each SKScene has its own WKWebView, not just the top ViewController.
Until I solve this, I am at a standstill

IOS Swift web view control, hiding the header and footer

When using the web view control in IOS 10, how do you hide the header and footer ( the grey boxes)? I am using the UIWebView that I drag to the viewcontrol. The web view has the grey boxes at the time it is placed in the storyboard. I have used the web view with previous versions and didn't see these boxes.
You can try setting the web view programmatically:
webView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height), configuration: null)
let url = URL(string: "http://www.google.com")
let urlRequest = URLRequest(url: url!)
webView.load(urlRequest)
self.view = webView

MPMoviePlayerController ignores MPMovieControlStyle.None in Swift

I'm trying to autoplay a video in my app. The video needs to play without the controls.
I've set up the video and the settings, including MPMovieControlStyle.None but the video controls appear for about 2 seconds before disappearing. I have no idea why.
I've used this code (exact code) for another project and it works well, but here for some reason it will not.
override func viewDidLoad() {
super.viewDidLoad()
generateVideo()
}
func generateVideo () {
let movieURL = NSBundle.mainBundle().pathForResource("video", ofType: "mp4")
let videoFilePath = NSURL(fileURLWithPath: movieURL!)
self.view.addSubview(MoviePlayerViewController.moviePlayer.view)
self.view.sendSubviewToBack(MoviePlayerViewController.moviePlayer.view)
MoviePlayerViewController.moviePlayer.contentURL = videoFilePath
MoviePlayerViewController.moviePlayer.shouldAutoplay = true
MoviePlayerViewController.moviePlayer.prepareToPlay()
MoviePlayerViewController.moviePlayer.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
MoviePlayerViewController.moviePlayer.fullscreen = true
MoviePlayerViewController.moviePlayer.controlStyle = MPMovieControlStyle.None
MoviePlayerViewController.moviePlayer.play()
MoviePlayerViewController.moviePlayer.repeatMode = MPMovieRepeatMode.One
MoviePlayerViewController.moviePlayer.scalingMode = MPMovieScalingMode.AspectFit
}
Any idea why this is happening?
After checking how the view is loaded I'm sure the problem is there.
I used a different layout with one Storyboard calling another storyboard and that was the source of the problem.