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).
Related
I want to load privacy policy (simple string) from server say, https://*****.com/privacy-policy.html to my app.
What I have tried till now is given below:
class TermsAndConditionsDetailViewController: UIViewController, WKNavigationDelegate {
#IBOutlet weak var contentView: UIView!
var webView: WKWebView!
func webView(_ webView: WKWebView,
didFinish navigation: WKNavigation!) {
print("loaded")
}
override func viewDidLoad() {
super.viewDidLoad()
webView = WKWebView()
contentView.addSubview(webView)
let myURL = URL(string: "https://***.com/privacy-policy.html")
let myRequest = URLRequest(url: myURL!)
webView.navigationDelegate = self
//I have tried these both one by one
webView.loadHTMLString("", baseURL: myURL)
webView.load(myRequest)
}
}
When i run this code "loaded" gets printed after 2 - 3 seconds but it doesn't show anything on the screen. I have been searching this issue for some time now and have tried to play around with the code. I have cross checked the constraints of all view specially contentView, but no luck.
You forget to add frame for your WKWebView
just add
webView = WKWebView(frame: self.contentView.frame)
after
webView = WKWebView()
and it will work fine.
I am trying to load a URL using webView in swift 4. I tried https://www.google.co.in and it works fine. and the mobile site for the specific URL works fine with android.
But when I tried this in the simulator, it is loading forever.
my code is below:
import UIKit
class WebViewController: UIViewController, UIWebViewDelegate {
#IBOutlet weak var webView: UIWebView!
let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
if let url = URL(string: "https://www.myurl.com") {
let request = URLRequest(url: url)
webView.loadRequest(request)
}
}
public func webViewDidStartLoad(_ webView: UIWebView)
{
activityIndicator.center = CGPoint(x: self.view.bounds.size.width/2, y: self.view.bounds.size.height/2)
self.view.addSubview(activityIndicator)
activityIndicator.color = UIColor.red
self.activityIndicator.startAnimating()
}
public func webViewDidFinishLoad(_ webView: UIWebView)
{
self.activityIndicator.stopAnimating()
}
}
I don't think that my code is wrong. but I was wondering if I can do anything to make it work on simulator.
Thanks
I just made a small project with your code and no problem in the simulator, https://www.google.co.in loaded just fine.
I noticed you mention you are using Swift 4, I think you should consider using WebKit View (WKWebView) since UIWebView is deprecated. In Apple's documentation you can see how to implement it, it's pretty straight forward.
If your certificate is not trusted you must add to Info.plist App Transport Security Settings, Allow Arbitrary Loads to YES. (This is not recommended).
The code is really simple, give it a try:
import UIKit
import WebKit
class WebViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
#IBOutlet weak var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let myURL = URL(string: "https://www.myurl.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: #escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
if let serverTrust = challenge.protectionSpace.serverTrust {
completionHandler(.useCredential, URLCredential(trust: serverTrust))
}
}
}
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
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
I'm making an app where you can order food. I made it to where the checkout is in a UIWebView page on paypal. The problem is their are 9 or 10 diffrent webpages being loaded at once. I was wondering if their is a way to make it only load when needed.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var webView: UIWebView!
#IBOutlet weak var webView1: UIWebView!
#IBOutlet weak var webView2: UIWebView!
#IBOutlet weak var webView3: UIWebView!
#IBOutlet weak var webView4: UIWebView!
#IBOutlet weak var webView5: UIWebView!
#IBOutlet weak var webView6: UIWebView!
#IBOutlet weak var webView7: UIWebView!
#IBOutlet weak var webView8: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let url = NSURL(string: "https://mms-lunch-order.000webhostapp.com/12count/pay.html")
let request = NSURLRequest(URL: url!)
webView.loadRequest(request)
let url1 = NSURL(string: "https://mms-lunch-order.000webhostapp.com/spicychicken6th/pay2.html")
let request1 = NSURLRequest(URL: url1!)
webView1.loadRequest(request1)
let url2 = NSURL(string: "https://mms-lunch-order.000webhostapp.com/chickensandwhich6th/pay3.html")
let request2 = NSURLRequest(URL: url2!)
webView2.loadRequest(request2)
let url3 = NSURL(string: "https://mms-lunch-order.000webhostapp.com/12count7/pay4.html")
let request3 = NSURLRequest(URL: url3!)
webView3.loadRequest(request3)
let url4 = NSURL(string: "https://mms-lunch-order.000webhostapp.com/spicychicken7/pay9.html")
let request4 = NSURLRequest(URL: url4!)
webView4.loadRequest(request4)
let url5 = NSURL(string: "https://mms-lunch-order.000webhostapp.com/chickensandwhich7/pay7.html")
let request5 = NSURLRequest(URL: url5!)
webView5.loadRequest(request5)
let url6 = NSURL(string: "https://mms-lunch-order.000webhostapp.com/12count8/pay5.html")
let request6 = NSURLRequest(URL: url6!)
webView6.loadRequest(request6)
let url7 = NSURL(string: "https://mms-lunch-order.000webhostapp.com/spicychicken8/pay10.html")
let request7 = NSURLRequest(URL: url7!)
webView7.loadRequest(request7)
let url8 = NSURL(string: "https://mms-lunch-order.000webhostapp.com/chickensandwhich8/pay8.html")
let request8 = NSURLRequest(URL: url8!)
webView8.loadRequest(request8)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
This is comment because I don't have enough reputation.
I think may be #IBOutlet is not connected properly,can you please check that
I'm assuming you are navigating to this web view from a previous page, and you only intend to check out for the item you chose. If this is not the case, then let me know and I will adjust this answer.
In that case, remove all but one webview. I would set a variable on this viewcontroller called foodUrlString.
class ViewController: UIViewController {
#IBOutlet weak var webView: UIWebView!
var foodUrlString = String()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if let url = NSURL(string: foodUrlString) {
let request = NSURLRequest(URL: url)
webView.loadRequest(request)
} else {
//if URL does not work for some reason, do something here like show error loading view or something
}
}
When you are about to navigate to this viewcontroller and you know what you want to buy, set the URL String for that foodUrlString property.
For instance, if you are presenting this viewcontroller, set it right before you present it from previous viewcontroller
viewController.foodUrlString = "http://food.com"
As far as your web view being nil, leave only one web view. In storyboard, control click on the top most view in the hierarchy and delete any unlinked outlets.