Text bleeding out of the webview - ios

I am having an issue with a simple webview app in Swift. I have set up most of the app, however when I test it there are two pages of the website (out about 40) which bleed out of the webview and mess up all the formatting in the app.
Normal View:
Normal webview
One of the problem pages:
problem webview
I have look at a number of articles on here dealing with similar issues but none of those solutions seem to work for me.
Right now i'm using this code:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let aurl = NSURL(string: "http://georgetown.neotalogic.com/a/jobdefender")
let request = NSURLRequest(url: aurl! as URL)
Webview.loadRequest(request as URLRequest)
Webview.scalesPageToFit = true

Just tried it in a quickly created demo app with
webview.delegate = self
webview.scalesPageToFit = true
webview.loadRequest(request as URLRequest)
And couldn't reproduce the issue. Maybe the order matters or maybe you have to set the delegate to self even without implementing any delegate functions. Idk :/

Related

WKWebViews (multiple) - sharing cookies, localStorage in Swift

I'm having such an irritating issue. I have a UITabBar application with 5 tab bar buttons. Each hooked up to a viewController: FirstViewController, SecondViewController etc.
In each of the 5 ViewControllers I have a WKWebView. The 5 WKWebViews display sections of an eCommerce website. For example, tab 1 shows home, tab 2 shows cart etc.
In order for the app to work, I need all 5 WKWebViews to share all cookies, localStorage, IndexDB.
Right now, I'm doing this:
FirstViewController
class FirstViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
let uniqueProcessPool = WKProcessPool()
let websiteDataStoreA = WKWebsiteDataStore.nonPersistent()
..
class YourModelObject {
static let sharedInstance = YourModelObject()
let configA = WKWebViewConfiguration()
}
override func loadView() {
let model = YourModelObject.sharedInstance
//so I believe I'm doing the right thing here setting this
model.configA.processPool = uniqueProcessPool
model.configA.websiteDataStore = websiteDataStoreA
webView = WKWebView(frame: .zero, configuration: model.configA)
webView.navigationDelegate = self
self.webView.scrollView.delegate = self
view = webView
}
Now in SecondViewController, I simply do this:
override func loadView() {
//am I doing something wrong here - like creating another instance of configA, instead of re-using - and if so, how do I re-use what was created in FirstViewController?
let model = FirstViewController.YourModelObject.sharedInstance
webView = WKWebView(frame: .zero, configuration: model.configA)
webView.navigationDelegate = self
self.webView.scrollView.delegate = self
view = webView
}
When I run the project, the cookies, localStorage, IndexDB information from WKWebView in FirstViewController is not shared with SecondViewController - even though according to me, I'm re-using the same configA for both.
I think it is best if you use SFSafariViewController for your needs.
As the documentation states:
In iOS 9 and 10, it shares cookies and other website data with Safari.
It means, that it is going to use the same cookies and data from the Safari browser, which is even better. If I am not mistaken, the user can be logged in through Safari, and when he comes to your app, he will not have to log in again.
Here is the full documentation for it:
SFSafariViewController
Update:
If you still want to do what you already started, according to this answer here in Objective-C, this is the solution in Swift:
You need a place where you would store the persistent 'process pool'. In your case, it is YourModelObject singleton
class YourModelObject {
static let sharedInstance = YourModelObject()
let processPool = WKProcessPool()
}
Use the shared processPool before initializing the webView. This is the initialization function which you would call in the loadView() for every viewController:
override func loadView() {
super.loadView() //don't forget this line
setupWebView()
}
private func setupWebView() {
let config = WKWebViewConfiguration()
config.processPool = YourModelObject.sharedInstance.processPool
self.webView = WKWebView(frame: .zero, configuration: config)
self.webView.navigationDelegate = self
self.webView.scrollView.delegate = self
self.view = self.webView
}

WKWebView App on ios 13 is showing a blank page

I'm developing a ios app to embed my website inside a webview.
After setup the project and run the code, i'm getting a blank page on the ios simulator.
I already added the NSAllowsArbitraryLoadsInWebContent and NSAllowsLocalNetworking to the info.plist file. When i inspect with the simulator with safari i only saw a _aboutBlank.
This is the code that i use to render the website into a webview.
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://www.globo.com/")!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
}
}
Note: if i change the url to apple.com it works.
Any ideas in how to solve this issue?
After trying some options i found two ways to fix the problem.
Test on the real device
Create an app in ReacNative and use the Webview

How to make transparent background WKWebView in Swift

I want a transparent web page in swift, so I have tried the below code according to this answer. Still, I am not getting a transparent web page. nothing changes in webview colour.. may I know why??
where am I going wrong? please help me in below code.
Total code:
import UIKit
import WebKit
class WebviewViewController: UIViewController {
#IBOutlet weak var testWebView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
guard let url = URL(string: "https://developer.apple.com/swift/") else { return }
let request = URLRequest(url: url)
testWebView.load(request)
// Do any additional setup after loading the view.
self.testWebView = WKWebView()
self.testWebView!.isOpaque = false
self.testWebView!.backgroundColor = UIColor.clear
self.testWebView!.scrollView.backgroundColor = UIColor.clear
}
}
Please help me with the code.
the code to make transparent background is as follow what you already added.
self.testWebView!.backgroundColor = UIColor.clear
now question is you added right code already then why you are not getting reliable output ..?
Also , if you try
self.testWebView!.alpha with any value, it will affect all of WebPages as WkWebView is a single view and changing it's alpha will also affect the components within...
it happened because the page you load in WebViewController has some HTML and CSS code, you make your WebViewController transparent but because of that HTML &CSS you can't see it's transparency as each webpage has it's own background color settings (which is merely impossible to change for each webpage)
I hope you will understand and it will help you ...:)
as I see, you use storyboard (#IBOutlet) and you can use Storyboard for setting your WKWebView:
And about code. This is enough for the result. You shouldn't set again self.testWebView = WKWebView(), because you use storyboard and you can set isOpaque with storyboard. As result:
class ViewController: UIViewController, WKNavigationDelegate {
#IBOutlet var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView.navigationDelegate = self
let url = URL(string: "https://developer.apple.com/swift/")!
webView.load(URLRequest(url: url))
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
let js = "(function() { document.body.style.background='transparent'; })();"
webView.evaluateJavaScript(js) { (_, error) in
print(error)
}
}
}
and evaluateJavaScript helped to add transparency for background:

WKWebView stays blank on simulator (allowing arbitrary loads)

I'm trying to load a WKWebView but my simulator (iPhone 6 - iOS 10.2) keeps displaying a blank screen. I can only see horizontal and vertical scrollers as if the webview was quite wide but empty.
App Transport Security Settings > Allow Arbitrary Loads is set to YES in the info.plist file.
Here is my code
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.google.co.uk")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
}
I tried several pieces of code found online or even downloaded working simple WKWebView xcodeproj, but I always have the same problem.
I can load the website (https://www.google.co.uk) in my browser. Also tried with general .com extension, or local .co.uk extension.
EDIT: works perfectly fine on the device but not on the simulator
I know it's an old post but it might be helpful for others.
So it most have something to do with override loadView() This method loads or creates a view and assigns it to the view property, but somehow in Simulator it doesn't do what it should!
Try removing loadView and Add the method body inside viewDidLoad() and it should be fine.
Check if you have a proxy running. I've had Charles Proxy running in the background and that caused this issue.

UIWebView - white background before loading content

I have a question. I have a menu, when I click on it, it will load content remotely from URL via UIWebView.
When I try on simulator, before loading the content, there's a white background and then loads my HTML Page (which has dark background)
Demo:
You can see that white flash, when I click on "KALENDER"
How can I fix this issue?
My UIWebView looks like this:
let myWebView:UIWebView = UIWebView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
myWebView.loadRequest(NSURLRequest(URL: NSURL(string: "http://website.com/test.html")!))
self.view.addSubview(myWebView)
Thank you!
So, what I did, is, added:
myWebView.opaque = false
myWebView.backgroundColor = UIColor.clearColor()
and at controller, I unchecked this from "View"
You cannot set background color to your webView.
You should try following steps:
1. write following in your viewDidLoad - Setting delegate and hiding webView.
webView.delegate = self
webView.hidden = YES
2. Write delegate - shouldStartLoadWithRequest - hiding webView when loading starts.
func webView(webView: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) -> Bool {
webView.hidden = YES
return true;
}
3. Write delegate - webViewDidFinishLoad - Show webView when loading Ends.
func webViewDidFinishLoad(webView: UIWebView!) {
webView.hidden = NO
print("Webview did finish load")
}
Hope this will help you....
You could load your own html document, which uses the color you need.
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let htmlString = "<html><body bgcolor=\"#ABCDEF\"></body></html>"
webView.loadHTMLString(htmlString, baseURL: nil)
}

Resources