UIWebView displaying a network loading


/ Published in: Objective C
Save to your folder(s)

This works fine on my app.


Copy this code and paste it in your HTML
  1. #import <UIKit/UIKit.h>
  2.  
  3.  
  4. @interface className : UIViewController <UIWebViewDelegate>{
  5. UIWebView *myWebView;
  6. }
  7.  
  8. @end
  9.  
  10.  
  11. #import "className.h"
  12.  
  13. @implementation className
  14.  
  15. // Implement loadView to create a view hierarchy programmatically, without using a nib.
  16. - (void)loadView {
  17. UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
  18. self.view = contentView;
  19.  
  20. CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
  21. webFrame.origin.y = 0.0;
  22. myWebView = [[UIWebView alloc] initWithFrame:webFrame];
  23. myWebView.backgroundColor = [UIColor whiteColor];
  24. myWebView.scalesPageToFit = YES;
  25. myWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
  26. myWebView.delegate = self;
  27. [self.view addSubview: myWebView];
  28. [myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/"]]];
  29. }
  30.  
  31. #pragma mark UIWebView delegate methods
  32.  
  33. - (void)webViewDidStartLoad:(UIWebView *)webView
  34. {
  35. // starting the load, show the activity indicator in the status bar
  36. [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
  37. }
  38.  
  39. - (void)webViewDidFinishLoad:(UIWebView *)webView
  40. {
  41. // finished loading, hide the activity indicator in the status bar
  42. [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
  43. }
  44.  
  45. - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
  46. {
  47. // load error, hide the activity indicator in the status bar
  48. [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
  49.  
  50. // report the error inside the webview
  51. NSString* errorString = [NSString stringWithFormat:
  52. @"<html><center><font size=+5 color='red'>An error occurred:<br>%@</font></center></html>",
  53. error.localizedDescription];
  54. [myWebView loadHTMLString:errorString baseURL:nil];
  55. }
  56.  
  57. - (void)didReceiveMemoryWarning {
  58. [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
  59. // Release anything that's not essential, such as cached data
  60. }
  61.  
  62.  
  63. - (void)dealloc {
  64. [super dealloc];
  65. }
  66.  
  67.  
  68. @end

URL: http://www.iphonedevsdk.com/forum/iphone-sdk-development-advanced-discussion/15077-uiwebview-not-calling-webviewdidfinishload.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.