UIWebView is not shown - ios

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.

Related

Delete cache of app

I want to delete the cache of my app.
I want to show a web page by using UIWebView.
MY current code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var webBrowser: UIWebView!
webBrowser:(UIWebView *)webBrowser shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string:"http://127.0.0.1:8000/admin/accounts/")
let request = NSURLRequest(url:url! as URL)
self.webBrowser.loadRequest(request as URLRequest)
// 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.
}
}
When I added this code
webBrowser:(UIWebView *)webBrowser shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
to ViewController,so "Expected declaration" error happen.
What is wrong in my code?
Should I connect my Storyboard and this code in some point?
How can i fix this?
To delete cache try below code
URLCache.shared.removeAllCachedResponses()
URLCache.shared.diskCapacity = 0
URLCache.shared.memoryCapacity = 0
Else you can also change the cache policy of the NSURLRequest
let url = URL(string: "http://127.0.0.1:8000/admin/accounts/")
let request = URLRequest(url: url!,
cachePolicy:NSURLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData,
timeoutInterval: 10.0)
self.webBrowser.loadRequest(day_url_request)
it's Objective C code.Update your code like below
class ViewController: UIViewController,UIWebViewDelegate {
#IBOutlet weak var webBrowser: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string:"http://127.0.0.1:8000/admin/accounts/")
let request = NSURLRequest(url:url! as URL)
self.webBrowser.delegate = self
self.webBrowser.loadRequest(request as URLRequest)
// Do any additional setup after loading the view, typically from a nib.
}
func webView(_ webView: UIWebView,
shouldStartLoadWith request: URLRequest,
navigationType: UIWebViewNavigationType) -> Bool
// do your stuffs
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
You are using Objective-C code shouldStartLoadWithRequest:
Here is the swift version code to load UIWebView
class ViewController: UIViewController,UIWebViewDelegate {
override func viewDidLoad() {
var webBrowser: UIWebView!
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
webBrowser = UIWebView(frame: UIScreen.main.bounds)
webBrowser.delegate = self
view.addSubview(webBrowser)
if let url = URL(string: "http://127.0.0.1:8000/admin/accounts/") {
let request = URLRequest(url: url)
webBrowser.loadRequest(request)
}
}
}

Web view: Returning nil at loadRequest while true in URLRequest

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

Search Bar & Web View

I'm trying to build a simple web browser. The web browser itself works pretty well but when I try to search a url in the search bar I can't return on the keyboard to open the website. How can I fix this?
#IBOutlet weak var searchBar: UISearchBar!
#IBOutlet weak var webView: UIWebView!
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder()
let text = searchBar.text
let urlSB = NSURL(string: text!)
let reqSB = NSURLRequest(url:urlSB! as URL)
self.webView!.loadRequest(reqSB as URLRequest)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let url = NSURL(string: "https://www.google.com")
let req = NSURLRequest(url:url! as URL)
self.webView!.loadRequest(req as URLRequest)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
I write a demo for you:
The result:
Step 1:
Step 2: Type in the url you want to access.
Step 3: The result, the webView load the url you typed in the searchBar:
The code is below:
import UIKit
class ViewController: UIViewController, UISearchBarDelegate {
#IBOutlet weak var searchBar: UISearchBar!
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "https://www.baidu.com")
let req = NSURLRequest(url:url! as URL)
self.webView!.loadRequest(req as URLRequest)
searchBar.delegate = self
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder()
let text = searchBar.text
let urlSB = NSURL(string: text!)
let reqSB = NSURLRequest(url:urlSB! as URL)
self.webView!.loadRequest(reqSB as URLRequest)
}
// MARK: - searchbar delegate
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
let urlStr = "https://" + searchBar.text!
let url = NSURL(string: urlStr)
let req = NSURLRequest(url:url! as URL)
self.webView!.loadRequest(req as URLRequest)
}
If your webView did not load the url, and report:
App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
You can go to info.plist to open the ATS like below, or search the SO to find the detail method:
You need to implement the delegate methods for UISearchBar and handle everything from there. Also don't forget to set the delegate to the viewcontroller.

App crashing after trying to load 10 webpages with UIWebView

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.

iOS Swift : can't Retrieve current url loaded in UIwebView

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)
}
}

Resources