My webview is not showing up. (Just a blank white page)
I have a TabBarController and two ViewControllers.
One of them containing the webview.
My code:
import Foundation
import UIKit
class HomeViewController: UIViewController, UIWebViewDelegate {
#IBOutlet var webView: UIWebView!
var urlpath: String = "http://www.google.de"
func loadAddressURL(){
let requesturl = NSURL(string: urlpath)
let request = NSURLRequest(URL: requesturl!)
webView.loadRequest(request)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
loadAddressURL()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Whats wrong here?
First double check the view is laying out correctly. Run the app and nav to the screen. Do Debug > View Debugging > Capture View Hierarchy. Right click the screen and print out the view to see if the frame is correct. If this is ok then implement the delegate methods of UIWebView, specifically webViewDidFinishLoad. This should let you know if there was an issue loading the webpage.
Do you have constraints set up in IB? You might try setting them programmatically to see if that helps.
if let wv = myWebView {
wv.translatesAutoresizingMaskIntoConstraints = false
view.addConstraints(
NSLayoutConstraint.constraintsWithVisualFormat(
"H:|[webView]|", options: [], metrics: nil, views: ["webView":wv]))
view.addConstraints(
NSLayoutConstraint.constraintsWithVisualFormat(
"V:|[webView]|", options: [], metrics: nil, views: ["webView":wv]))
}
You need to tell the webView who is its delegate, just binding the outlet won't be enough.
In viewDidLoad add this:
self.webView.delegate = self
Then you need to conform to UIWebViewDelegate (which you are already doing, but I suggest you extend your view controller instead):
extension HomeViewController: UIWebViewDelegate {
func webViewDidStartLoad(_ webView: UIWebView) {
print("Loading")
}
func webViewDidFinishLoad(_ webView: UIWebView) {
print("Finished")
}
}
And last but not least, you need to allow your application to load external URL's by adding a transport security, I suggest you check this post.
Please note I'm using Swift 3, you may need to make little changes.
Related
im trying to use the UI WebKit View.
I created a single view app, placed a UI WebKit View on the main View and put the constraints "centering horizontally and vertically in container, fixed height and equal width to superview" to it.
then i changed my ViewController.swift to
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
#IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let myUrl = URL(string: "www.apple.com")
let myRequest = URLRequest(url: myUrl!)
webView.load(myRequest)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
But when i start the app i get only a blank screen. What am i doing wrong?
Your url is not valid so change this
let myUrl = URL(string: "www.apple.com")
to
let myUrl = URL(string: "https://www.apple.com")
I've scoured the internet for 3 hours now, and have just given up. I figured since no one else on this entire planet has had this problem before, maybe I should ask the stackoverflow community, and see what happens.
I'm familiar with xCode, and objective C, but am learning swift. For my first app I figured I'd build a super simple webview app that loads a page. Easy right? I can code this in about 15 seconds in objective c, and I did, it works fine. Maybe there's something I'm missing here.
When I attempt to connect the object to the UIWebView it won't connect.
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
#IBOutlet var webView: WKWebView!
#IBOutlet var textBox: UITextField!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
let url = NSURL(string: "https://www.google.com")!
webView.loadRequest(NSURLRequest(URL: url))
webView.allowsBackForwardNavigationGestures = true
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Image of it not connecting. It's not connecting like it should
Change type from WKWebView to UIWebView.You have added UIWebView from object library which your are trying to connect to a WKWebView object so it will not connect. WKWebview has to be added programmatically as it is not present in object library.
These two classes have similar functionalities but they are not connected to each other in anyway.
You can use the following code to build a webView app that loads a page.
import UIKit
class ADKWebViewDemoViewController: UIViewController {
#IBOutlet weak var webview: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
webview.loadRequest(NSURLRequest(URL: NSURL(string: "https://www.google.com")!))
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I am trying to get a webpage to appear in my swift application. I have added the WebView and wrote the following code in the controller class but when I run my app, there is only a white screen. Am I missing something?
class WebViewController: UIViewController {
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let WebView = UIWebView(frame: view.bounds)
let url = NSURL(string: "http://www.google.com")
let request = NSURLRequest(URL:url!)
WebView.loadRequest(request)
view.addSubview(WebView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Run your code in viewDidLayoutSubviews. You are setting the WebView's frame to the view's bounds. The view's frame has not fully loaded yet.
Never mind, I'm an idiot and forgot the s in http thanks all.
You need to tell the webView who is its delegate, just binding the outlet won't be enough.
In viewDidLoad add this:
self.webView.delegate = self
Then you need to conform to UIWebViewDelegate (which you are already doing, but I suggest you extend your view controller instead):
extension WebViewController: UIWebViewDelegate {
func webViewDidStartLoad(_ webView: UIWebView) {
print("Loading")
}
func webViewDidFinishLoad(_ webView: UIWebView) {
print("Finished")
}
}
And last but not least, you need to allow your application to load external URL's by adding a transport security, I suggest you check this post.
Please note I'm using Swift 3, you may need to make little changes.
I am new to iOS development, i am trying to use a UIWebView using the following code
import UIKit
class SecondViewController: UIViewController {
#IBOutlet var myWebView: UIWebView!
#IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let url = NSURL (string: "http://www.chrisharrisracing.com/twitterapp.php");
let requestObj = NSURLRequest(URL: url!);
myWebView.loadRequest(requestObj);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
However when I build the the web page is not displayed, although when I go to the web address in a browser the page works fine, any help would be appreciated
I just created a sample project to check your issue and its working for me.
class ViewController: UIViewController {
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL (string: "http://www.chrisharrisracing.com/twitterapp.php");
let requestObj = NSURLRequest(URL: url!);
webView.loadRequest(requestObj);
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
If you want you can download the sample project from the following link
https://www.dropbox.com/home/SAMPLE%20GIT%20PROJECTS%20%5BSHARED%5D/WEBVIEW?preview=webViewSample.zip
You should add NSTransportSecurity to info.plist
HOPE THIS HELPS.....
I made a sample project with your code and it works just fine. The only difference is that I didn't use the second outlet (#IBOutlet var webView: UIWebView!) you have in this code. My guess is you haven't added any constraints for your webview and that's why you can't see it. So go to your storyboard choose your webview and then go to the triangle icon at the bottom right and choose Add missing constraints.
Of course make sure your internet connection is ok and that you have added the allow arbitrary loads key in your info.plist
Maybe you haven't added your constraints yet, which is why myWebView might not be visible. Or maybe, this is probably a longshot, but I've noticed that you have another unused UIWebView called webView. Maybe, just maybe, webView is covering up myWebView, thus you aren't seeing myWebView loading the page.
Im crating a simple wrapper, however no matter what device I test a website with loading it into a webview its always a bit to wide and has horizontal scrolling. I can turn that off by changing the content size but then i just cut off part of the view on the right hand side.
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet var webView: UIWebView?
let endpointURL : String = "http://www.google.com/"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
webView?.delegate = self
var url : NSURL = NSURL(string: endpointURL)!
var request : NSURLRequest = NSURLRequest(URL: url)
webView?.loadRequest(request)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webViewDidStartLoad(webView: UIWebView) {
}
func webViewDidFinishLoad(webView: UIWebView) {
//webView.scrollView.contentSize = CGSizeMake(webView.frame.size.width, webView.scrollView.contentSize.height)
}
}
Use webView.scalesPageToFit = true
This should size the web page to fit the size of the UIWebView.
I suspect your problem might be related to the frame of the WebView.Try resizing your webview to your screen bounds.
webView.frame = UIScreen.mainScreen().bounds