I have the following HTML content to be rendered in a WKWebView:
<html>
<body>
<div>Here goes plain text google.com type of content</div>
</body>
</html>
If I load it in Safari, it's rendered as expected:
But if I load it in my iOS app in a WKWebView using webview.loadHTMLString method, it automatically highlights the url and makes it clickable:
webview.loadHTMLString("<html><body><div>Here goes plain text google.com type of content</div></body></html>", baseURL: nil)
How can I prevent this behavior?
You either create your wkwebview from your own empty configuration like below
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()
webView.loadHTMLString("<html><body><div>Here goes plain text google.com type of content</div></body></html>", baseURL: nil)
}
}
Or you if you are using storyboard, please disable link detector in xcode attributes inspector
Related
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
}
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
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:
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.
I am using WKWebView in my iOS (swift) app and i am using loadHTMLString(string, baseURL: ). Now the problem is that I want the HTML to be vertically centered in the frame. The text is variable in length. How can I do that?
This is the screenshot of the HTML from Xcode:
This is image of the output. The top HTML is on the upper side, I want it to be centrally aligned in its view (the view is 300 points from the top):
Try using this HTML with inline CSS, you can replace the stuff in between the divs that you want centered:
<div style="display:flex;align-items:center;height:100%;justify-content:center;">
<p>You content here</p>
</div>
This code works for me using Swift 3.0 on an iPhone 6 simulator
import UIKit
import WebKit
class ViewController: UIViewController {
var webView:WKWebView?
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
webView = WKWebView(frame: self.view.frame)
self.view.addSubview(webView!);
addHTML()
}
func addHTML() {
let html = "<html><body><div style='height:100%;background-color:black;display:flex;align-items:center;justify-content:center;'><p style='color:white;font-size:100px;'>Asleepace</p></div></body></html>";
webView!.loadHTMLString(html, baseURL: nil)
}
}
Example Output