How to show a loading message while UIWebView loads it's content? - iphone

I want to put a big spinner along with a "Loading message..." or a gif image, when UIWebView loads its contents so it won't just show a blank view. How should I do it?

implement UIWebview's delegate method put this code in it
- (void)webViewDidStartLoad:(UIWebView *)webView {
[activityIndicator startAnimating];
myLabel.hidden = FALSE;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[activityIndicator stopAnimating];
myLabel.hidden = TRUE;
}
set ActivityIndicater's Hidden when stop property to TRUE

Implement the UIWebView Delegate as outlined in Mihir's answer above but don't forget to assign the delegate otherwise the delegate methods will not be triggered
For example In ViewDidLoad you should add:
self.myWebView.delegate = self;

Related

How to run code when a WebView has finished loading.

I have 2 WebViews in my application. one is loading a youtube video and the other is loading an events calendar. I want to show a custom 'Loading' image until the data from the web views are done loading.
I am trying
while([youtubeView isLoading]){
NSLog(#"youtubeVideo is loading");
[loadingImage setHidden:NO];
}
[loadingImage setHidden:YES];
In viewDidLoad but it doesn't work.
Whats the best way to accomplish what I am trying to achieve?
Cheers, Beers on me.
Use below method of UIWebViewDelegate. It get called once webView finish loading.
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
if(youtubeView isEqual:webView)
{
loadingImage.hidden = YES;
}
}
Also note that your class need to conform to UIWebViewDelegate and both your webviews should set delegate equal to object of that class.
#interface YourViewController : UIViewController <UIWebViewDelegate>
{
UIWebView *youtubeView;
}
#end
inside method where you create UIWebView youtubeView
youtubeView.delegate = self;
Beer ?

iOS: Webview causes flicking while navigating to another controller

I use webview in my UIVIewController and load the local HTML file in it using following method.
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:filePath]]];
If I put this method in ViewDidLoad, I can see white flicking while navigating to my controller that doesn't look good.
I tried putting this method in ViewWillAppear like below. I am using webViewLoaded flag to make sure that webview has been loaded then only show the current view else it waits but it is going in infinite loop of waiting!
- (void)viewWillAppear:(BOOL)animated {
webViewLoaded = NO;
webView.scalesPageToFit = allowZoom;
webView.dataDetectorTypes = UIDataDetectorTypeNone;
webView.delegate = self;
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:filePath]]];
int count = 0;
while(webViewLoaded == NO)
{
NSLog(#"Waiting...");
[NSThread sleepForTimeInterval:0.1];
if(count++ > 10) break;
}
}
- (void)webViewDidFinishLoad:(UIWebView *)localwebView {
webViewLoaded = YES;
}
I have also tried the same thing in ViewDidLoad but still its going in infinite loop.
Please note that webViewLoaded is "volatile BOOL" but the webview delegate method is not getting called. Not sure what's going on!
Could anyone please help me to fix this. Thanks.
First : You're blocking your main thread and not giving WebView any chance to finish loading. Never block your main thread.
Second : There are two UIWebView delegate methods : webViewDidFinishLoad and webView:didFailLoadWithError: - make sure to listen to both.
If you're trying to wait until WebView completes loading and only show your controller afterwards - use delegation or blocks. Please note that this is not a proper way of doing things - i'm just modifying your example to make it work :
child controller (with WebView) :
-(void)startLoadingWithParent:(id)_parent {
self.parent = _parent;
NSURL * url = [[NSBundle mainBundle] URLForResource:#"resource" withExtension:#"html"];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView {
[parent showMe:self];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
[parent showError:error];
}
master controller :
-(void)doSomething {
SecondController * ctrl; /// secondController should be created AND has its view loaded at this point
/// ctrl = [SecondController new];
/// ctrl.view;
[ctrl startLoadingWithParent:self];
/// show user that we're doing something - display activity indicator or something
}
-(void)showMe:(UIViewController*)me {
[self.navigationController pushViewControllerAnimated:me];
}
Try using [NSURL fileURLWithPath:filePath]
use Following code for
[self performSelectorInBackground:#selector(webViewLoaded) withObject:nil];
so that it will not affect your UI

UIWebView cannot click link

I am pretty sure that I understand how to catch a click on a UIWebView using the webView:shouldStartLoadWithRequest:navigationType: method, but my webView does not even allow me to click the link. I am using:
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(12, top, boundsSize.width - 40.0, 400.0f)];
webView.delegate = self;
webView.backgroundColor = [UIColor clearColor];
webView.opaque = NO;
webView.allowsInlineMediaPlayback = YES;
webView.dataDetectorTypes = UIDataDetectorTypeAll;
// add to subview
I am loading in an HTML string rather than loading a URL:
[webView loadHTMLString:bodyHTML baseURL:nil];
Delegate method:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"Loaded");
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
NSLog(urlString);
}
return YES;
}//end
Everytime my webview loads I DO get the "Loaded" in my logs, so I know that the delegate method is getting called. But I can never click on a link within my UIWebView and have anything happen. It does not even look like it is allowing a pressed state on the link. The link is highlighted like a link, just won't allow clicking.
Ideas?
The reason for the UIWebView not responding to touch events is most probably this line:
webView.opaque = NO;
Try setting opaque to YES.
My understanding is that for a view to respond to touch events, that view has to be returned by the call to hitTest:withEvent: during the view hierarchy traversal performed by the event delivery mechanism.
From the hitTest:withEvent: documentation:
This method ignores view objects that are hidden, that have disabled user interaction, or have an alpha level less than 0.01.
Update
Based on new info: if the UIWebView is embedded in a UIScrollView, quote from the Apple docs:
Important: You should not embed UIWebView or UITableView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.
Please try with following code
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if(navigationType==UIWebViewNavigationTypeLinkClicked)
{
[[UIApplication sharedApplication]openURL:request.URL];
return NO;
}
return YES;
}

how to add UIWebview inside a view by coding

In my app I dragged a View from the library of the interface builder and linked it to the AppDelegate.
my question is "can I add UIWebView inside the View ?"
and if make a special class (UIView class) for the view which method behaves like viewDidload ?
Yes you can add a UIWebView inside the View.
[UIWebView *aWebView =[[UIWebView alloc] initWithFrame:CGRectMake(x,y,width,height)]; aWebView.delegate=self;
[self.view addSubview:aWebView];
[aWebView release];
In the above code you set the delegate of the WebView as self. So you can use the delegate methods of UIWebView like
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
- (void)webViewDidFinishLoad:(UIWebView *)webView
Refer this link for more details.
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIWebViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UIWebViewDelegate
Yes you can simply use -
In your h file add
#interface YourView:UIView<UIWebViewDelegate>{
in your m file add
CGRect frame = CGRectMake(0,0,200,200);
UIWebView *webView =[[UIWebView alloc] initWithFrame:frame)];
webView.delegate = self;
[self addSubview:webView];
CGRect rect=CGRectMake(x,y,width,height) ;
UIWebView *webView1 =[[UIWebView alloc] initWithFrame:rect)];
webView1.delegate=self;
[self.view addSubview:webView1];
[webView1 release];
If you view's outlet is named myView:
.h file
#interface myViewController:UIViewController <UIWebViewDelegate> {
IBOutlet UIView *myView;
}
.m file
UIWebView *,myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,myView.frame.size.width,myView.frame.size.height)];
myWebView.delegate = self;
[myView addSubView:myWebView];
It's not necissary to create another subclass to listen for deleagte methods of your view, i would now treat your UIView as a UIWebView and thus use UIWebView's delegate methods, like so:
- (void)webViewDidStartLoad:(UIWebView *)webView {
NSLog(#"started loading");
}
more delegate methods can be found here in the documentation. You could also use NSNotificationCenter to listen for your own events application wide if you wish to fire something in a different class when the UIWebView is loaded.

UIwebview loader on iphone

this code works fine , its show activityIndicator loading and hide after loading
but lets say if its on loading stage and then i go back then my apps quit , so how to avoid this
if i go back quickly withiut site fully loaded then it quits ( i think activityIndicator is culprit)
**
- (void)webViewDidStartLoad:(UIWebView *)webView {
[activityIndicator startAnimating];
// myLabel.hidden = FALSE;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[activityIndicator stopAnimating];
activityIndicator.hidesWhenStopped=TRUE;
//myLabel.hidden = TRUE;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
self.title=#"KKH Website";
[super viewDidLoad];
NSURL *requestUrl = [NSURL URLWithString:#"http://www./index.html"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:requestUrl];
[aWw loadRequest:requestObj];
}
**
updates
if i link uiwebview delegate to fileowner then its creating isuee , but i need delegate also since i want to show loading
i forgot to release my webview :(
[aWw release];
now its fixed