WKWebView's (beta) takeSnapshot method, how to implement? - ios

I noticed the WKWebView documentation now lists a method called takeSnapshot that is supported by iOS 11 and macOS 10.13 and above (Xcode 9 beta).
Has anyone played around with yet or implemented? I'm trying to get it working in a playground but I'm not sure where to start? Is this a method on a WKWebView?
My code:
import UIKit
import PlaygroundSupport
import WebKit
let frame = CGRect(x: 0, y: 0, width: 800, height:600)
let web = WKWebView(frame: frame)
let rq = URLRequest(url: NSURL(string: "http://apple.com")! as URL)
web.load(rq)
PlaygroundPage.current.liveView = web
PlaygroundPage.current.needsIndefiniteExecution = true
//Take snapshot?

As far as I tested, the method takeSnapshot(with:completionHandler:) actually exists as an instance method and works as expected.
Just that it's a little hard to use it.
The method declares its first parameter as WKSnapshotConfiguration?, but the class WKSnapshotConfiguration is not imported with import WebKit. You can pass nil to the parameter, but to use the method, you need to import the type WKSnapshotConfiguration. And I could not have found any dependent submodules to import WKSnapshotConfiguration.
So, if you want to play with this new feature, you need to create an App project with bridging-header.
(If you know how to use bridging-header in the Playground, you may test this feature in it. But I do not know if you can or how.)
{ProjectName}-Bridging-Header.h:
#import CoreGraphics;
#import <WebKit/WKSnapshotConfiguration.h>
And an example of ViewController.swift:
import UIKit
import WebKit
class ViewController: UIViewController {
#IBOutlet weak var webView: WKWebView!
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let request = URLRequest(url: URL(string: "http://apple.com")!)
webView.load(request)
}
#IBAction func buttonPressed(_ sender: UIButton) {
webView.takeSnapshot(with: nil) {image, error in
if let image = image {
self.imageView.image = image
print("Got snapshot")
} else {
print("Failed taking snapshot: \(error?.localizedDescription ?? "--")")
}
}
}
}
(Place a WKWebView, a UIImageView and a UIButton on a View.)
One more, this seems to be a bug of WebKit framework, you should better send a bug report to Apple.

Related

The isExternalURL function from the IntuneMAMWebViewPolicyDelegate is not called

I have implemented the iOS Intune SDK in my app. The app applies the MAM policies and is working properly. I am trying to implement the Webview content restrictions using the IntuneMAMWebViewPolicyDelegate and isExternalURL as described here
The isExternalURL function is never called.
This is the view containing my WKWebView
import UIKit
import WebKit
import IntuneMAMSwift
class WebviewPage: UIViewController {
let webViewPolicyDelegage = WebViewPolicyClass.init();
#IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
IntuneMAMPolicyManager.instance().setWebViewPolicyDelegate(webViewPolicyDelegage, forWebViewer: webView!)
let webaddress = "https://www.google.com"
if let url = URL(string: webaddress) {
let urlRequest = URLRequest(url: url)
self.webView.load(urlRequest)
}
}
}
This is my delegate implementation
import IntuneMAMSwift
class WebViewPolicyClass: NSObject, IntuneMAMWebViewPolicyDelegate {
func isExternalURL(_ url: URL) -> Bool {
// TODO: Check if URL is external
return true
}
}
The app MAM policy is configured to require the managed Edge browser.
Any ideas why the delegate method is not called?

WKWebView is not getting intialized in swift 4 xcode 10

Could anyone help me in understanding why my webView is not getting initialized here. I am getting the following error because webView is nil.
Fatal error: Unexpectedly found nil while unwrapping an Optional value
What exactly I am missing here?
import UIKit
import WebKit
class MemoriesViewController: UIViewController, WKNavigationDelegate {
#IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear( animated )
let urlString:String = "https://www.apple.com"
let url:URL = URL(string: urlString)!
let urlRequest:URLRequest = URLRequest(url: url)
webView.load(urlRequest)
}
}
}
Screenshot for better clarity where the error is actually happening:
Add Webkit.Frameworks to Linked Framework and Libraries
You can then add the Outlet for WKWebView and use your code
import UIKit
import WebKit
class MyWebViewController: UIViewController {
#IBOutlet weak var myWV: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let urlString:String = "https://www.apple.com"
let url:URL = URL(string: urlString)!
let urlRequest:URLRequest = URLRequest(url: url)
myWV.load(urlRequest)
}
}
In Xcode 10, you can foll bellow the step:
Click the project
Go to build phase
Go to Link Binary with Libraries
Click add plus sing under Link Binary with Libraries
open a windows & search Webkit.framework
add & enjoy it
Show bellow image
The problem was my output was not connected to the storyboard. That's why #IBOutlet was not getting initialized.

Enable zooming in WKWebView in Swift 4, Xcode 10, for iOS 12

I am trying to enable zooming/magnification (by pinching) in a very simple WebView in a simple app. (I am pretty new to this).
The Swift code that loads the web page is this and it works fine, I got from here, I think.
import UIKit
import WebKit
class SecondViewController: UIViewController {
#IBOutlet weak var WebViewSchedule: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// load the page
let url = URL(string: "https://mywebsite.com")
let request = URLRequest(url: url!)
WebViewSchedule.load(request)
//how do i make it zoomable ????
}
}
I've looked at a number of answers about this, and they all refer to a property "allowsMagnification". I have tried various ways of using this and got nowhere.
So could some kind person give me a beginners' guide to how to make my iOS Web page zoomable?

How to Enable Local Storage in Web Application Swift View

I am developing a small app in Swift3 for iOS, I previously developed it for Android where I had a problem that is replicated here. It happens that I have a webview where you have to upload some videos, youtube videos are reproduced without problems (both Android and iOS), the problem arises when you want to play a video for example with JWPlayer, the problem I had in Android Was that the property was not enabled to write to disk and the solution was as follows:
final WebView myBrowser;
myBrowser.getSettings().setDomStorageEnabled(true);
What would be the solution for Swift3?
I found this solution:
var prefs: WebPreferences? = webView.prefereces
prefs?._setLocalStorageDatabasePath("~/Library/Application Support/MyApp")
prefs?.localStorageEnabled = true
But I get an error:
Use of undeclared type WebPreferences
I pay attention to your comments, greetings and many thanks in advance.
I update my question.
import UIKit
import WebKit
class ViewController: UIViewController {
#IBOutlet var webView: WebView!
override func viewDidLoad() {
super.viewDidLoad()
webView.loadUrl( string: "www.google.com")
}
class WebView: WKWebView{
required init?(coder: NSCoder) {
if let _view = UIView(coder: coder){
super.init(frame: _view.frame, configuration: WKWebViewConfiguration())
autoresizingMask = _view.autoresizingMask
}else{
return nil
}
}
func loadUrl(string: String){
if let url = URL(string: string ) {
load(URLRequest(url: url))
}
}
}
}

Getting "Thread 1:EXC_BAD_ACCESS" error?

I am creating a simple web viewer using WKWebView and the Swift language. Here is my code.
import Cocoa
import WebKit
#NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate {
#IBOutlet var containerView : NSView! = nil
#IBOutlet weak var window: NSWindow!
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
var webView: WKWebView
webView = WKWebView() //Thread 1:EXC_BAD_ACCESS (code=1,address=0x20)
var url = NSURL(string:"http://www.google.com/")
var req = NSURLRequest(URL: url!)
webView.loadRequest(req)
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
I'm now getting a "Thread 1:EXC_BAD_ACCESS" error on the webView = WKWebView() line when I try to run the application. How can I fix this?
If that's it's all your code, than I'd say you're missing to instantiate the WKWebView. Put a breakpoint on the self.webView!.loadRequest(req) line and check whether it's nil or not.
I've never worked with WKWebView, but I think that you need a view controller. Maybe this blog post can help you: Getting started with WKWebView using Swift in iOS 8
You forgot to initialise your WKWebView
var webView = WKWebView(frame: CGRectMake(0, 0, 320, 480))
var url = NSURL(string:"http://www.google.com/")
var req = NSURLRequest(URL: url)
webView.loadRequest(req)
I get this error whenever I try to use a brand new minimal class as a delegate. For example:
class MyDelegate: NSObject, UIWebViewDelegate { /* ... */ }
This does not happen when I try to use the default ViewController as a delegate. For example:
class ViewController: UIViewController, UIWebViewDelegate { /* ... */ }
I would suggest using the ViewController as a delegate until we get to the bottom of this.

Resources