I'm trying to make an IOS app to show a web site in a webview.
That's ok it's working. Now I'm trying to detect the end of the page load.
That's my code :
class ViewController: UIViewController, UIWebViewDelegate{
#IBOutlet weak var my_web_view: UIWebView!
#IBOutlet weak var my_loading_view: UIView!
override func viewDidLoad() {
super.viewDidLoad()
NSLog("----------------> START");
my_web_view.scrollView.bounces = false;
my_web_view.scrollView.isScrollEnabled = true;
let url = URL(string: "https://www.sortirauhavre.com/");
let request = URLRequest(url: url!);
my_web_view.delegate = self;
my_web_view.loadRequest(request);
}
func webViewDidFinishLoad(webView: UIWebView!){
NSLog("----------------> STOP");
}
}
In my console I can read "START" but never "STOP". Xcode don't say me I made an error, do you know what am I doing wrong please ?
yes , the problem you are not connected the delegate of your webview to current class
and call the delegate as
func webViewDidFinishLoad(_ webView: UIWebView){
NSLog("----------------> STOP");
}
you get the output as
Related
WebView in Mainstoryboard is not shown anything. I wanna show yahoo page in my app.When I run my app in an emulator(that is not my real iPhone), it is like only black screen.
Any error does not happen in that time, but
warning
In this part, I got a warning
"signal SIGABRT"
My code is like
import Foundation
import Alamofire
import UIKit
class webViewController: UIViewController {
#IBOutlet var webBrowser: UIWebView! = nil
var targetURL = "https://www.yahoo.co.jp"
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.
}
func loadAddressURL() {
let requestURL = NSURL(string: targetURL)
let req = NSURLRequest(url: requestURL as! URL)
webBrowser.loadRequest(req as URLRequest)
}
}
I think a connection of WebView & WebViewController is ok. I tried to do Add Exception Breakpoint,and find a point of this error.
The result is
result
My app dropped in
"Thread1:breakpoint4.1".
What is wrong with my app?
You cannot assign nil value to #IBOutlet var webBrowser: UIWebView!.
Leave:
#IBOutlet weak var webBrowser: UIWebView!
BTW, I'm assuming that you create correctly #IBOutlet by holding ctr and dragging from storyboard to you view controller. But I'm afraid that because of lack weak next to your #IBOutlet you created it manually in code so you need to clip it to the view in storyboard
CTRL + DRAG to create IBOutlet for UIWebView like mentioned in below image and it should be:
#IBOutlet weak var webBrowser: UIWebView!
Initialize your targetURL like :
let targetURL = "https://www.yahoo.co.jp".addingPercentEncoding( withAllowedCharacters: .urlQueryAllowed)
Then in your func loadAddressURL must be
func loadAddressURL() {
if NSURL(string: targetURL!) != nil {
let url = URL(string: targetURL!)!
webBrowser.loadRequest(URLRequest(url: url))
}
}
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var webBrowser: UIWebView!
var targetURL = "https://www.yahoo.co.jp"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.loadAddressURL()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadAddressURL() {
let requestURL = NSURL(string: targetURL)
let req = NSURLRequest(url: requestURL! as URL)
webBrowser.loadRequest(req as URLRequest)
}
}
Use this.
Web view: Returning nil at loadRequest while true in URLRequest
I don't know where is wrong.
import UIKit
class DetailViewController: UIViewController, UIWebViewDelegate {
#IBOutlet weak var designatedWebView: UIWebView!
var webview: UIWebView!
var testString: String!
override func viewDidLoad() {
super.viewDidLoad()
designatedWebView = webview
testString = "http://www.apple.com"
let baseURL = URL(string: testString)
let baseURLRequest = URLRequest(url: baseURL!) //this prints true
designatedWebView.loadRequest(baseURLRequest) //while this prints nil
}
}
Replace your code with this. This is the normal and easiest way to load a website in UIWebView.
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let url = URL(string: "http://www.apple.com")
let request = URLRequest(url: url!)
webView.loadRequest(request)
webView.scalesPageToFit = true
}
}
If you are not connecting the webView from your Main.storyboard, then add this line of code with the above code in your viewDidLoad() function.
webView.delegate = self
So I am trying to make an app in Xcode 8 basically I have a webview and a side bar with buttons I want to make those buttons change the webview but for some reason when I press them the app crashes.
This is the code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var menuView: UIView!
#IBOutlet weak var leadingConstraint: NSLayoutConstraint!
var menuShowing = false
#IBOutlet weak var Webview: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
menuView.layer.shadowOpacity = 1
menuView.layer.shadowRadius = 6
let url = URL(string: "http://www.peulibrary.com/application/")
Webview.loadRequest(URLRequest(url: url!))
}
#IBAction func loadSecond(_ sender: Any) {
let url = URL(string: "http://www.peulibrary.com/application/category/פעולות-גן-ג/")
Webview.loadRequest(URLRequest(url: url!))
}
when I run the app in the simulator I get an error at the second
Webview.loadRequest(URLRequest(url: url!))
that says "Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0)"
any help appreciated, thanks in advance! :)
Your URL contains special characters so encode your URL string then use it with webView.
let stringURL = "http://www.peulibrary.com/application/category/פעולות-גן-ג/"
if let encodeURL = stringURL.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
let url = URL(string: encodeURL) {
Webview.loadRequest(URLRequest(url: url))
}
I want to login many accounts of same site in different webView. For example i have Tab Bar Controller that contains three view controllers and each view controllers contain webView. And for example i embed stackoverflow url for webView in every class. How user can login to different accounts at the same time using these three webView? I've tried this but i can just login one user at a time.
I have found that i need to create separate cookie for every UIWebView, but mostly answers are in objective-c and not the proper answer i want. For example (First Second Third)
Can any one please tell me how i can do it?
class FirstViewController: UIViewController , UIWebViewDelegate{
#IBOutlet weak var webView: UIWebView!
#IBOutlet weak var activityIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
webView.delegate = self
let requestURL = NSURL(string: "http://stackoverflow.com")
let request = NSURLRequest(URL: requestURL!)
activityIndicator.hidesWhenStopped = true
activityIndicator.startAnimating()
webView.loadRequest(request)
}
func webViewDidFinishLoad(webView: UIWebView) {
activityIndicator.stopAnimating()
}
}
class SecondViewController: UIViewController, UIWebViewDelegate{
#IBOutlet weak var webView: UIWebView!
#IBOutlet weak var activityIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
webView.delegate = self
let requestURL = NSURL(string: "http://stackoverflow.com")
let request = NSURLRequest(URL: requestURL!)
activityIndicator.hidesWhenStopped = true
activityIndicator.startAnimating()
webView.loadRequest(request)
}
func webViewDidFinishLoad(webView: UIWebView) {
activityIndicator.stopAnimating()
}
}
Thanks
You can do this with WKWebView by using different instances of WKWebSiteDataStore:
let configuration1 = WKWebViewConfiguration()
configuration1.websiteDataStore = WKWebsiteDataStore.nonPersistent()
self.webView1 = WKWebView(frame: CGRect.zero, configuration: configuration1)
self.view.addSubview(self.webView1)
let configuration2 = WKWebViewConfiguration()
configuration2.websiteDataStore = WKWebsiteDataStore.nonPersistent()
self.webView2 = WKWebView(frame: CGRect.zero, configuration: configuration2)
Unfortunately, you will loose webView data (such as cookies, cache, etc) after app restart, because non-persistent WKWebsiteDataStore can't be saved to disk (you could notice that WKWebsiteDataStore implements NSCoding, but it doesn't work for non-persistent stores).
i am working in webview application iOS swift :
problem i am facing is i want to get each url which webview displays :
upon urls i have to perform some if else checks . but i could not get the urls which webview loads.
webview is displaying fine results and loading urls upon clicking . i want to fetch all urls which webview navigates on..
import Foundation
import UIKit
class seconVC: UIViewController {
var toPass:String!
var touser:String!
var toPassing:String!
#IBOutlet weak var webV: UIWebView!
#IBOutlet weak var L_back: UIBarButtonItem!
#IBOutlet weak var R_frwrd: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var domurl = toPass;
var username = touser;
var passwing = toPassing;
var sendurl = stringA+username+"/"+passwing;
//exchange login with auth/username/password
println(sendurl);
let url = NSURL (string: sendurl);
let requestObj = NSURLRequest(URL: url!);
webV.userInteractionEnabled = true
webV.loadRequest(requestObj);
}
#IBAction func L_back(sender: UIBarButtonItem) {
if (webV.canGoBack){
webV.goBack()
}
}
#IBAction func R_frwrd(sender: UIBarButtonItem) {
if (webV.canGoForward){
webV.goForward()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
all i want is to get string of current url and navigation buttons for webview :please help ? i referred all the internet no solution is working in my case .
There are couple of UIWebViewDelegate such as shouldStartLoadWithRequest or webViewDidStartLoad or webViewDidFinishLoad or didFailLoadWithError that help you to accomplish your goal. If you want to perform operation after view did finished then implement this delegate method
Swift 3
if let currentURL = webView.request?.url?.absoluteString{
print(currentURL)
}
as it mentioned here.
option-1
let currentURL = webV.request.URL.absoluteString
option-2
get URL in delegate method
func webViewDidFinishLoad(webView: UIWebView){
print(WebView.request?.mainDocumentURL)
}
option-3
func webViewDidFinishLoad(webView: UIWebView) {
let currentURL = webView.request().URL()
print("\(currentURL.description)")
}
or use like
func webViewDidFinishLoad(webView: UIWebView){
let currentURL = webView.request?.URL
}
option-4
func webViewDidFinishLoad(webView: UIWebView) {
let currentURL: = webView.stringByEvaluatingJavaScriptFromString("window.location.href")!
print("\(currentURL)")
}
option-5
func webView(webview: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let currentURL = request.URL.absoluteString
print("\(currentURL)")
}
option-6
func webViewDidFinishLoad(WebVie: UIWebView) {
let currentURL = WebVie.request.mainDocumentURL
print("\(currentURL)")
}
First you have to define the delegate for WebView class. "WebView.delegate = self". Add this code beside one of the function above:
override func viewDidLoad() {
super.viewDidLoad()
myWebView.delegate = self }
and for exmaple:
func webViewDidFinishLoad(_ webView: UIWebView) {
if let currentURL = WebView.request?.url?.absoluteString{
print(currentURL)
}
}