How to show image while page is loading in swift? - ios

I'm beginner in swift and Xcode and want to know how to make an image appears while page finish loading in UIWebView,like when you enter a new website,image start appear on screen in webview

You can use any loader meanwhile the website loads, like MBProgressHUD or any other progress bar.
And if you are willing to show an custom image over a screen while loading then you can handle that in following delegate methods:
say you have take any custom image or View called loadingView
func webView(webView: UIWebView!, didFailLoadWithError error: NSError!) {
**loadingView**.viewWithTag(1)?.hidden = true
print("Webview fail with error \(error)");
}
**func webView(webView: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) -> Bool {**
return true;
}
func webViewDidStartLoad(webView: UIWebView!) {
**loadingView**.viewWithTag(1)?.hidden = false
print("Webview started Loading")
}
func webViewDidFinishLoad(webView: UIWebView!) {
**loadingView**.viewWithTag(1)?.hidden = true
print("Webview did finish load")
}
Hope this will help you.

Related

disable a specific link on ios uiwebview

How can I disable a specific link on iOS uiwebview ?
The page loaded in the webview can have thousand urls but i want to disable one url.
I am sensing i have to use this method for the work. But can't figure it out.
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
}
I'm working with objective-c. Sorry if my swift code is wrong. I hope you understand with the logic.
Try it.
func webView(_ webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let surl = request.url?.absoluteString
if surl == "http://disableurl.com" {
return false;
}
return true;
}

UIWebView offline error in Swift

I am loading a webpage into my app using UIWebView and following code:
let url = NSURL (string: "http://www.myurl.com");
let requestObj = NSURLRequest(URL: url!);
myWebView.loadRequest(requestObj);
I would like to hide webView if request failed for any reason, for example because of no access to internet. Is there a way to do it?
Use the provided delegate methods of UIWebView
func webViewDidFinishLoad(webView: UIWebView) {
print("webview did finish load!")
}
func webView(webView: UIWebView, didFailLoadWithError error: NSError) {
print("webview did fail load with error: \(error)")
}
Note: To use this method you need implement UIWebViewDelegate in your ViewController and set it with the webView outlet.
myWebView.delegate = self

Get URL of clicked link inside of UIWebView use Swift 2

I have UITextField and UIWebView like this:
enter image description here
But when I clicked a link inside webview, the textfield above did not change to clicked URL.
I try with :
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let scheme = request.URL!
print(scheme)
}
and this:
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == .LinkClicked {
print(request.URL.path)
}
return true
}
and this:
textfield.text = webview.request.URL.absoluteString
but it did not get request.URL.path and webview.request.URL.absoluteString.
How to make texfield.text change to clicked URL from webview?
If you are targeting iOS 8 and later, use WKWebView instead of UIWebView. From Apple:
In apps that run in iOS 8 and later, use the WKWebView class instead of using UIWebView. Additionally, consider setting the WKPreferences property javaScriptEnabled to false if you render files that are not supposed to run JavaScript.
The minor annoyance is that WKWebView can be dragged and dropped via Interface Builder so you have to add it to your view in code. Here's the navigation part:
override func viewDidLoad() {
super.viewDidLoad()
self.webView.navigationDelegate = self
}
func webView(webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
self.addressField.text = webView.URL!.absoluteString
}

swift void SendDelegateMessage.. webView:decidePolicyForNavigationActionfailed to return after waiting 10 seconds... kCFRunLoopDefaultMode

I create app with ios 7 and ios 8 compatible but UIWebview on ios 7 delegete never called and i get
void SendDelegateMessage(NSInvocation *): delegate
(webView:decidePolicyForNavigationAction:request:frame:decisionListener:)
failed to return after waiting 10 seconds. main run loop mode:
kCFRunLoopDefaultMode
I search on internet but no sollution
4 day trying with no lucky..
class AWTncViewController: UIViewController, UIWebViewDelegate{
#IBOutlet weak var wv: UIWebView!
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
wv.delegate = nil
}
override func viewDidLoad() {
super.viewDidLoad()
super.viewDidLoad()
self.wv.delegate = self;
let myHTMLString:String! = "<h1>Hello word!</h1>"
self.wv.loadHTMLString(myHTMLString, baseURL: nil)
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
/* Test 2 still not load
let myHTMLString:String! = "<h1>Hello word!</h1>"
self.wv.loadHTMLString(myHTMLString, baseURL: nil)
*/
}
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
//this never called
println("shouldStartLoadWithRequest execute")
return true
}
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool
{
//this never called
println("shouldStartLoadWithRequest execute")
return true
}
func webViewDidStartLoad(webView: UIWebView)
{
//this never called
println("Start load")
}
func webViewDidFinishLoad(webView: UIWebView)
{
//this never called
println("FinishLoad")
}
func webView(webView: UIWebView, didFailLoadWithError error: NSError)
{
//this never called
println("didFailLoadWithError: \(error.description)")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
I read this :iOS 7 UIWebView not rendering
But i'm not use Crittercism
I ran into this issue too. It seems the problem is caused when certain 3rd party libraries are linked, but I am not even sure exactly which one was responsible in my case.
What fixed it for me was the suggestion I found here on Apple's Dev Forum to instantiate a UIWebView early.
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// In a severe case of WTF: some 3rd party libs (exact culprit unknown) can cause webviews to stop
// showing anything on iOS 7, and instead have console warnings every 10 seconds that look like:
// void SendDelegateMessage(NSInvocation *): delegate (webView:decidePolicyForNavigationAction:request:frame:decisionListener:) failed to return after waiting 10 seconds. main run loop mode: kCFRunLoopDefaultMode
// Just instantiating an UIWebView before any of the 3rd party libs kick in is enough to fix it.
// Don't know why, but it works.
if (SYSTEM_VERSION_LESS_THAN(#"8.0")) {
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
webView.delegate = nil; // Do something with webView to silence warning
}
return YES;
}

UIWebView.loading returns false

With swift using the following code:
super.viewDidLoad()
let webView = UIWebView(frame: self.view.bounds)
view.addSubview(webView)
let URL = NSURL(string: "http://www.google.com")
webView.loadRequest(NSURLRequest(URL: URL!))
println(webView.loading)
It prints false and the screen is blank. How is this resolved?
This is perfectly normal behaviour. The UIWebView does not actually start loading content until the UI event, in this case viewDidLoad, has finished executing. So checking it immediately returns false because it has not started just yet.
If you want want to track the success or failure of the UIWebView you should implement the UIWebViewDelegate in your view controller. That way you get callbacks when the request has finished loading or if it has failed.
I'm uncertain exactly on the internals of how UIWebView loads its data or what exactly happens when you call loadRequest, but contrary to my expectations, nothing seems to actually happen until the method which called loadRequest has returned.
Consider the following code:
#IBAction func buttonPress(sender: UIButton) {
let webview = UIWebView(frame: self.view.bounds)
view.addSubview(webview)
let url = NSURL(string: "http://www.google.com")
webview.delegate = self
webview.loadRequest(NSURLRequest(URL: url!))
println("buttonPress: webview.loading? \(webview.loading)")
}
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
println("webview asking for permission to start loading")
return true
}
func webViewDidStartLoad(webView: UIWebView) {
println("webview did start loading")
}
func webView(webView: UIWebView, didFailLoadWithError error: NSError) {
println("webview did fail load with error: \(error)")
}
func webViewDidFinishLoad(webView: UIWebView) {
println("webview did finish load!")
}
Here, the println in the buttonPress method always executes before any of the other methods. In fact, seemingly no matter what sort of code we put after the loadRequest call, it all executes before the web view even asks shouldStartLoadingWithRequest:.
webview.loadRequest(NSURLRequest(URL: url!))
for _ in 1...10000 {
println("buttonPress: webview.loading? \(webview.loading)")
}
Ten thousand iterations, and yet nothing starts for the webview until after buttonPress method returns.
Meanwhile, webview.loading will only return true between webViewDidStartLoad and one of the two ways the load can stop (failure/success) (webView(webView:didFailLoadWithError:) or webViewDidFinishLoad()).
IF you implement the UIWebViewDelegate protocol and set the web view's delegate, you can implement these methods to keep track of the loading process. If, for whatever reason, your web view isn't loading the URL, implementing webView(webView:didFailLoadWithError:) is the only way to get any sort of diagnostic informatin to determine what failed with the load.

Resources