WKWebView is not loading web page on iOS 13 - ios

I am trying to load a webpage with WKWebView on iOS 13 with Swift. It works fine in iOS 12. The problem is the WKWebView shows a white screen on iOS 13. The same url used for both (iOS 12/iOS 13) so I am 100% sure that there is no problem in the URL. Here is my UIViewController where I load the webpage:
import UIKit
import WebKit
class WebViewController: UIViewController , WKNavigationDelegate{
var param : String!
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://google.com")!
webView.load(URLRequest(url: url))
}
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
}
safari inspector result :
for iOS 13 similator is about:blank
for real device iOS 12.4 is www.google.com

One way to ensure you will always have a valid URL before making the request is by using an 'if' instead of force unwrapping.
override func viewDidLoad() {
super.viewDidLoad()
if let url = URL(string: MyData.url){
webView.load(URLRequest(url: url))
}
}
You can also add an else statement on there or use an early return to print something out if you don't go inside.
Other than doing this to check the url is valid you can debug and check the webview to see if that is nil. If this is the case then your solution is to add your code to viewWillAppear instead of viewDidLoad.

I tried it also with iOS 12.2 version on simulator and gives the same problem , so I see that the simulators does not support WKWebView , on the real device it works fine

Related

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?

Swift webview PROXY

With the code below I create a webview navigating to a URL. It works perfectly. But I need this navigation to be through an HTTP proxy.
How do I put proxy in a webview?
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView = WKWebView()
webView.navigationDelegate = self
view = webView
let url = URL(string: "https://www.iphub.info")!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
}
}
Unfortunately, there's no simple way to achieve that.
You may intercept deprecated UIWebView with custom NSURLProtocol subclass, which utilizes NSURLSession inside. You'd easily set HTTP proxy configuration to the connectionProxyDictionary property of the NSURLSessionConfigurtion. Here's the example. That would work, but you may see slightly worse performance than with WKWebView.
Alternatively, you may consider using NetworkExtension.framework, if the proxy server supports tunneling. Here's a sample project which utilizes Network Extension.

Swift Error : Classes implemented in two places but files I don't touch?

I am testing out swifts UIWebView, I have placed a simple UIWebview on my storyboard and this is my viewController code
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let url = URL(string: "https://mywebsitehere.com")
webView.loadRequest(URLRequest(url: url!))
} }
Unfortunately I am getting this Error below, I have added App transport security settings to my app like other answers to a similar question have said to do but it still isn't working...
Can anyone help?
objc[946]: Class PLBuildVersion is implemented in both
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/AssetsLibraryServices.framework/AssetsLibraryServices
(0x124feecc0) and
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/PhotoLibraryServices.framework/PhotoLibraryServices
(0x124e056f0). One of the two will be used. Which one is undefined.
This is bug with xCode while simulating the real iOS device, some time it through such kind of error.
Please check whether your url is active or not.
let url = URL(string: "https://google.com")
webView.loadRequest(URLRequest(url: url!))
I used the same code with google URL and it is working.

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

WKNavigationDelegate Crashing App on Assignment

I'm working on a simple web wrapper application for iOS, and I'm having some issues with WKWebView and WKNavigationDelegate. I want to use the didFinishNavigation function from WKNavigationDelegate, so I can grab information from the URL query on navigation (namely a session GUID). My program launches correctly and loads my webpage when I comment out the "webView.navigationDelegate = self" line, but when I include that, my app crashes immediately with the following errors:
" -[UIWebView setNavigationDelegate:]: unrecognized selector sent to instance
0x7f8251e058f0"
"*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[UIWebView setNavigationDelegate:]: unrecognized selector sent to
instance 0x7f8251e058f0'"
I noticed that both of these error messages include "UIWebView," when I'm trying to use WKWebView, so I tried to use the "custom class" field on the webview from the identity inspector part of the storyboard, but when I try to run after that, I get "(lldb)." Any help/insight would be appreciated, I'm including my code below:
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
#IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
print("view did load")
webView.navigationDelegate = self
loadURL("http://mydomain/login.html")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
private func loadURL(targetURL: String){
if let url = NSURL(string: targetURL) {
let request = NSURLRequest(URL: url)
webView.loadRequest(request)
}
}
private func checkDomainGetSessionGUID(){
print ("we are here")
if let query = webView.URL?.query {
let queryArr = query.componentsSeparatedByString("&")
var parsedQuery : [String: String] = [:]
for field in queryArr {
let parts = field.componentsSeparatedByString("=")
parsedQuery.updateValue(parts[1], forKey: parts[0])
print ("key = \(parts[0]) and value = \(parts[1])")
}
}
else {
print ("didn't enter the if let")
}
}
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
print ("delegate")
checkDomainGetSessionGUID()
}
}
PROCEDURE 1:
Step-1: Check UIElements that you are using in Storyboard design. if you had used Web view instead of using **WebKit View this error might come.Check your IBOutlet connection.
Step-2: Check your IOS deployment target, It must be IOS 11 and above because of UIWebKit View was released in IOS 8 but it contains a bug that was fixed in IOS 11 only. So set your deployment target 11 and above.
Step-3: Check your info.plist property. The following property should add in the listApp Transport Security Settings -> Allow Arbitrary Loads -> YES
PROCEDURE 2:
If in case you want deployment target as IOS 10 or below IOS 11 means you can implement like this
Step-1: Create a new UIViewcontroller with its swift ViewController file. And add below-given code to run your URL:
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string:"https://www.apple.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}}
I hope this might be helpful to you...
I was getting this error inside (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
I removed the line [super webView:webView didFinishNavigation:navigation]; and everything worked as I expected, I'm not sure if it's hacky though.

Resources