Xcode WKWebView (IOS) Wont Load Webpage, White Screen - ios

I have searched for a few hours now and tried many solutions and none seem to work.
I have disabled my AVG web shield, added this to my plist under App transport
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSAllowsArbitraryLoadsInWebContent</key>
<true/>
Code below
let link = URL(string: "https://stackoverflow.com")!
let req = URLRequest(url: link)
webView.navigationDelegate = self
webView.uiDelegate = self
self.view.addSubview(webView)
self.view.bringSubview(toFront: webView)
self.webView!.load(req)
I am extremely confused why any web page does not load
I have tried http and https
I have tired multiple websites, no luck
it just stays on a blank white screen

Your code works in my playground, so something is wrong with how you are creating the web view or the constraints. See example:
import UIKit
import PlaygroundSupport
import WebKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let webView = WKWebView()
view.addSubview(webView)
webView.translatesAutoresizingMaskIntoConstraints = false
webView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
webView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
webView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
webView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
let link = URL(string: "https://stackoverflow.com")!
let req = URLRequest(url: link)
webView.load(req)
view.setNeedsLayout()
}
}
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = ViewController()

I get this on OSX apps and works for that go to general app settings, sandbox mode turn on and off again. Works every time for me on OSX apps

Related

WKWebView is not loading perticular url in swift

Unable to load "https://dev-react.ndh01.com" this url only in WKWebView in swift. But it is loading in browser and Android Webview
you are not doing anything wrong and technically your webpage is loading properly, but this website is not optimized for all screen sizes, you can verify this by rotating your test device whether you using a simulator or physical device
import UIKit
import WebKit
class WebViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let preferences = WKPreferences()
preferences.javaScriptEnabled = true
preferences.javaScriptCanOpenWindowsAutomatically = true
let configuration = WKWebViewConfiguration()
configuration.preferences = preferences
let webview = WKWebView(frame: .zero, configuration: configuration)
view.addSubview(webview)
webview.load(URLRequest(url: URL(string: "https://dev-react.ndh01.com")!))
webview.translatesAutoresizingMaskIntoConstraints = false
webview.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
webview.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
webview.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
webview.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
}
if you are using a simulator try pressing Cmd + right/left arrow to rotate the device to landscape
you will find a screen showing your web page

Swift 5 Disable redirection to other apps inside WKWebView

First of all... My other question got marked as a duplicate even though the question suggested was the exact opposite, so:
Inside my webView I do not want to be redirected to other apps. WKWebView should never redirect to other Apps. Instead it should just open the url.
This is my webView:
lazy var webView: WKWebView = {
let webConfiguration = WKWebViewConfiguration()
let webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
webView.translatesAutoresizingMaskIntoConstraints = false
return webView
}()

Connecting iOS app with MobFox

I followed the documentation to add MobFox ads in my application. For some reason I am getting the error : Could not connect to the server.
func mobFoxAdDidFailToReceiveAdWithError(_ error:Error!) {
print("MobFoxAdDidFailToReceiveAdWithError: ", error.localizedDescription)
}
And it gives:
MobFoxAdDidFailToReceiveAdWithError: Could not connect to the server.
private var mobfoxAd: MobFoxAd!
override func viewDidLoad() {
super.viewDidLoad()
let rect = CGRect(origin: CGPoint(x: 0, y: 200), size: CGSize(width:320, height: 50))
mobfoxAd = MobFoxAd("fe96717d9875b9da4339ea5367eff1ec", withFrame: rect)
mobfoxAd.delegate = self
mobfoxAd.refresh = adRefresh as NSNumber?
self.view.addSubview(mobfoxAd)
}
In my Info.plist file, I have added.
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
I don't seem to find the reason.
Thanks.
Turns out the countries where these services are not provided are not mentioned.
If you face this problem, change to another country with VPN. Ads will appear.

How to use WKWebView in interface-builder? [duplicate]

It seems that the IB object templates in XCode 6 beta are still creating old-style objects (UIWebView for iOS and WebView for OSX). Hopefully Apple will update them for the modern WebKit, but until then, what is the best way to create WKWebViews in Interface Builder? Should I create a basic view (UIView or NSView) and assign its type to WKWebView? Most of the examples I find online add it to a container view programmatically; is that better for some reason?
You are correct - it doesn't seem to work. If you look in the headers, you'll see:
- (instancetype)initWithCoder:(NSCoder *)coder NS_UNAVAILABLE;
which implies that you can't instantiate one from a nib.
You'll have to do it by hand in viewDidLoad or loadView.
As pointed out by some, as of Xcode 6.4, WKWebView is still not available on Interface Builder. However, it is very easy to add them via code.
I'm just using this in my ViewController. Skipping Interface builder
import UIKit
import WebKit
class ViewController: UIViewController {
private var webView: WKWebView?
override func loadView() {
webView = WKWebView()
//If you want to implement the delegate
//webView?.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
if let url = URL(string: "https://google.com") {
let req = URLRequest(url: url)
webView?.load(req)
}
}
}
Info.plist
add in your Info.plist transport security setting
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Xcode 9.1+
Using interface builder
You can find WKWebView element in the Object library.
Add view programmatically with Swift 5
let webView = WKWebView(frame: .zero, configuration: WKWebViewConfiguration())
view.addSubview(webView)
webView.translatesAutoresizingMaskIntoConstraints = false
webView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
webView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor).isActive = true
webView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
webView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor).isActive = true
Add view programmatically with Swift 5 (full sample)
import UIKit
import WebKit
class ViewController: UIViewController {
private weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
initWebView()
webView.loadPage(address: "http://apple.com")
}
private func initWebView() {
let webView = WKWebView(frame: .zero, configuration: WKWebViewConfiguration())
view.addSubview(webView)
self.webView = webView
webView.translatesAutoresizingMaskIntoConstraints = false
webView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
webView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor).isActive = true
webView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
webView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor).isActive = true
}
}
extension WKWebView {
func loadPage(address url: URL) { load(URLRequest(url: url)) }
func loadPage(address urlString: String) {
guard let url = URL(string: urlString) else { return }
loadPage(address: url)
}
}
Here's a simple Swift 3 version based on crx_au's excellent answer.
import WebKit
class WKWebView_IBWrapper: WKWebView {
required convenience init?(coder: NSCoder) {
let config = WKWebViewConfiguration()
//config.suppressesIncrementalRendering = true //any custom config you want to add
self.init(frame: .zero, configuration: config)
self.translatesAutoresizingMaskIntoConstraints = false
}
}
Create a UIView in Interface Builder, assign your constraints, and assign it WKWebView_IBWrapper as a custom class, like so:
With Xcode 8 this is now possible, but the means of achieving it is a little hacky to say the least. But hey, a working solution is a working solution, right? Let me explain.
WKWebView's initWithCoder: is no longer annotated as "NS_UNAVAILABLE". It now looks as shown below.
- (nullable instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER;
Start by subclassing WKWebView and override initWithCoder. Instead of calling super initWithCoder, you'll need to use a different init method, such as initWithFrame:configuration:. Quick example below.
- (instancetype)initWithCoder:(NSCoder *)coder
{
// An initial frame for initialization must be set, but it will be overridden
// below by the autolayout constraints set in interface builder.
CGRect frame = [[UIScreen mainScreen] bounds];
WKWebViewConfiguration *myConfiguration = [WKWebViewConfiguration new];
// Set any configuration parameters here, e.g.
// myConfiguration.dataDetectorTypes = WKDataDetectorTypeAll;
self = [super initWithFrame:frame configuration:myConfiguration];
// Apply constraints from interface builder.
self.translatesAutoresizingMaskIntoConstraints = NO;
return self;
}
Over in your Storyboard, use a UIView and give it a custom class of your new subclass. The rest is business as usual (setting auto-layout constraints, linking the view to an outlet in a controller, etc).
Finally, WKWebView scales content differently to UIWebView. Many people are likely going to want to follow the simple advice in Suppress WKWebView from scaling content to render at same magnification as UIWebView does to make WKWebView more closely follow the UIWebView behaviour in this regard.
If you still face this issue in recent versions of Xcode, i.e. v9.2+, simply import Webkit to your ViewController:
#import <WebKit/WebKit.h>
Before the fix:
After the fix:
This is now apparently fixed in Xcode 9b4. The release notes say "WKWebView is available in the iOS object library."
I haven't looked deeper to see if it requires iOS 11 or is backward compatible yet.
You can instantiate and configure a WKWebView in IB since Xcode 9, no need to do it in code.
Note that your deployment target has to be higher than iOS 10 though or you will get a compile-time error.
In XCode Version 9.0.1 WKWebView is available on Interface Builder.
I've linked WebKit, now it's working!
Not sure if this helps but I solved the problem for me by including the WebKit framework for the target. Don't embed it just link the reference. I still use the WebView object in IB and drop it on the ViewController I'm embedding it in and I've never had a problem...
I've worked on 4 WKWebView MacOS projects now and the WebView has worked properly in each project.
This worked for me in Xcode 7.2...
First add the web view as a UIWebView outlet in the storyboard / IB. This will give you a property like this:
#property (weak, nonatomic) IBOutlet UIWebView *webView;
Then just edit your code to change it to a WKWebView.
#property (weak, nonatomic) IBOutlet WKWebView *webView;
You should also change the custom class to WKWebView in the Identity inspector.

WKWebView in Interface Builder

It seems that the IB object templates in XCode 6 beta are still creating old-style objects (UIWebView for iOS and WebView for OSX). Hopefully Apple will update them for the modern WebKit, but until then, what is the best way to create WKWebViews in Interface Builder? Should I create a basic view (UIView or NSView) and assign its type to WKWebView? Most of the examples I find online add it to a container view programmatically; is that better for some reason?
You are correct - it doesn't seem to work. If you look in the headers, you'll see:
- (instancetype)initWithCoder:(NSCoder *)coder NS_UNAVAILABLE;
which implies that you can't instantiate one from a nib.
You'll have to do it by hand in viewDidLoad or loadView.
As pointed out by some, as of Xcode 6.4, WKWebView is still not available on Interface Builder. However, it is very easy to add them via code.
I'm just using this in my ViewController. Skipping Interface builder
import UIKit
import WebKit
class ViewController: UIViewController {
private var webView: WKWebView?
override func loadView() {
webView = WKWebView()
//If you want to implement the delegate
//webView?.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
if let url = URL(string: "https://google.com") {
let req = URLRequest(url: url)
webView?.load(req)
}
}
}
Info.plist
add in your Info.plist transport security setting
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Xcode 9.1+
Using interface builder
You can find WKWebView element in the Object library.
Add view programmatically with Swift 5
let webView = WKWebView(frame: .zero, configuration: WKWebViewConfiguration())
view.addSubview(webView)
webView.translatesAutoresizingMaskIntoConstraints = false
webView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
webView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor).isActive = true
webView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
webView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor).isActive = true
Add view programmatically with Swift 5 (full sample)
import UIKit
import WebKit
class ViewController: UIViewController {
private weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
initWebView()
webView.loadPage(address: "http://apple.com")
}
private func initWebView() {
let webView = WKWebView(frame: .zero, configuration: WKWebViewConfiguration())
view.addSubview(webView)
self.webView = webView
webView.translatesAutoresizingMaskIntoConstraints = false
webView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
webView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor).isActive = true
webView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
webView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor).isActive = true
}
}
extension WKWebView {
func loadPage(address url: URL) { load(URLRequest(url: url)) }
func loadPage(address urlString: String) {
guard let url = URL(string: urlString) else { return }
loadPage(address: url)
}
}
Here's a simple Swift 3 version based on crx_au's excellent answer.
import WebKit
class WKWebView_IBWrapper: WKWebView {
required convenience init?(coder: NSCoder) {
let config = WKWebViewConfiguration()
//config.suppressesIncrementalRendering = true //any custom config you want to add
self.init(frame: .zero, configuration: config)
self.translatesAutoresizingMaskIntoConstraints = false
}
}
Create a UIView in Interface Builder, assign your constraints, and assign it WKWebView_IBWrapper as a custom class, like so:
With Xcode 8 this is now possible, but the means of achieving it is a little hacky to say the least. But hey, a working solution is a working solution, right? Let me explain.
WKWebView's initWithCoder: is no longer annotated as "NS_UNAVAILABLE". It now looks as shown below.
- (nullable instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER;
Start by subclassing WKWebView and override initWithCoder. Instead of calling super initWithCoder, you'll need to use a different init method, such as initWithFrame:configuration:. Quick example below.
- (instancetype)initWithCoder:(NSCoder *)coder
{
// An initial frame for initialization must be set, but it will be overridden
// below by the autolayout constraints set in interface builder.
CGRect frame = [[UIScreen mainScreen] bounds];
WKWebViewConfiguration *myConfiguration = [WKWebViewConfiguration new];
// Set any configuration parameters here, e.g.
// myConfiguration.dataDetectorTypes = WKDataDetectorTypeAll;
self = [super initWithFrame:frame configuration:myConfiguration];
// Apply constraints from interface builder.
self.translatesAutoresizingMaskIntoConstraints = NO;
return self;
}
Over in your Storyboard, use a UIView and give it a custom class of your new subclass. The rest is business as usual (setting auto-layout constraints, linking the view to an outlet in a controller, etc).
Finally, WKWebView scales content differently to UIWebView. Many people are likely going to want to follow the simple advice in Suppress WKWebView from scaling content to render at same magnification as UIWebView does to make WKWebView more closely follow the UIWebView behaviour in this regard.
If you still face this issue in recent versions of Xcode, i.e. v9.2+, simply import Webkit to your ViewController:
#import <WebKit/WebKit.h>
Before the fix:
After the fix:
This is now apparently fixed in Xcode 9b4. The release notes say "WKWebView is available in the iOS object library."
I haven't looked deeper to see if it requires iOS 11 or is backward compatible yet.
You can instantiate and configure a WKWebView in IB since Xcode 9, no need to do it in code.
Note that your deployment target has to be higher than iOS 10 though or you will get a compile-time error.
In XCode Version 9.0.1 WKWebView is available on Interface Builder.
I've linked WebKit, now it's working!
Not sure if this helps but I solved the problem for me by including the WebKit framework for the target. Don't embed it just link the reference. I still use the WebView object in IB and drop it on the ViewController I'm embedding it in and I've never had a problem...
I've worked on 4 WKWebView MacOS projects now and the WebView has worked properly in each project.
This worked for me in Xcode 7.2...
First add the web view as a UIWebView outlet in the storyboard / IB. This will give you a property like this:
#property (weak, nonatomic) IBOutlet UIWebView *webView;
Then just edit your code to change it to a WKWebView.
#property (weak, nonatomic) IBOutlet WKWebView *webView;
You should also change the custom class to WKWebView in the Identity inspector.

Resources