How do you start activity indicator? - iphone

I want to start an activity indicator whenever the user presses any link on a webview.

First assign the ViewController which the UIWebView appears in as its delegate: Do this either by control-dragging from the UIWebView to the File's Owner object in InterfaceBuilder or in code:
[myWebView setDelegate:myViewController];
Then in your ViewController.m file you can now use the delegate methods to detect when pages are being loaded. These delegate methods will be triggered every time a link or new page is loaded in the UIWebView.
- (void)webViewDidStartLoad:(UIWebView *)webView {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}

If you wanna show network activity indicator just put
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
and then
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
when you wanna hide it
while if you needs UIActivityIndicator
you have to define it in your controller then Outlet it in XIB and then
show
[activityIndicator startAnimating];
hide
[activityIndicator stopAnimating];

Take a look at UIWebViewDelegate Protocol.
It will allow your delegate to be notified whenever the webview is starting to load new content, which, presumably, is the case whenever a link has been pressed. The delegate method you want is webViewDidStartLoad: - in its implementation you could then easily set up or start your activity indicator.

Use the method webViewDidStartLoad: in UIWebViewDelegate.

Related

remoteControlReceivedWithEvent not called in multiple view controllers app

I have a App works well on iOS 4.3.3. But I found there are some problems on iOS 5. This App plays mp3 files by avaudioplayer and the remote control does not work well on iOS 5.
There are four view controllers in this App. I add following codes on each view controller in order to realize the function of remote control. The problem is, when the view controller is first time to open. The remote control button works well, even the app run in background or lock the screen. But when I click anther view controller and go back the previous on, the remote controller does not work anymore. But canBecomeFirstResponder function was called every time.
I even try to put
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
in delegate function
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
It not work.
I don't understand why this is happening. It tortured me for few days.
Is there better way to realize remote control function in multiple view controllers?
BTW, I added the UIBackgroundModes audio key to the info.plist. And this App works well well in background.
Any help will be appreciated.
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
- (void)viewWillAppear:(BOOL)animated {
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
}
- (BOOL) canBecomeFirstResponder {
NSLog(#"Can be first responder!");
return YES;
}
- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause) {
NSLog(#"UIEventSubtypeRemoteControlTogglePlayPause");
[self playAndpause:nil];
}
}

Stop Activity Indicator in Status Bar?

I'm near completing my application, but I'm running into an issue. Obviously, my web view needs an activity indicator for the app to be accepted. I have the following already:
- (void)viewDidLoad
{
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#""]]]; // I removed the website for privacy's sake
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
Problem is that it won't stop spinning when I need it to (i.e. even after the page is loaded, it doesn't stop)
Any suggestions? Any help is appreciated!
Implement the UIWebViewDelegate in your class file that holds the UIWebView, and insert the following two methods:
- (void)webViewDidStartLoad:(UIWebView *)webView
- (void)webViewDidFinishLoad:(UIWebView *)webView
In the start method, place your:
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
... and in the finish, place:
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
Hope this helps!
In the web views delegate you can do:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
you need to call
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
to get it to stop

How to stop the NetworkActivity on a UIWebView?

I load some content on my UIWebview ,and when the UIWebView load finish the content of a URL such as "http://www.google.com", but the network NetworkActivityIndicator didn't stop,any suggestion?
I found somebody use [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES],but I think this is just to hidden the NetworkActivityIndicator ,in fact it does't stop,is that right??
Now what I want is really to stop the NetworkActivityIndicator ,how to accomplish it??
In your UIWebView delegate:
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
Remove any other calls to -setNetworkActivityIndicatorVisible in this delegate.'
If you haven't already assigned a delegate for this UIWebView, then do so as follows:
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(x,y,width,height)];
[webView setDelegate:self];
[self.view addSubview:webView];
This of course assumes you are inside a UIViewController and that self.view exists, so change as needed.
#McGrady:Try this..
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"webViewDidfinishLoad");
[activity stopAnimating];
}
Here activity is your activityindicator name
Hope this help you..
Regards
Renuga

How to launch links in UIWebView with the Safari app?

I have an small embedded UIWebView for my about-section of the app. When the user taps a link in there, the link opens in that small embedded UIWebView which of course has no navigation controls and isn't full-screen. I don't like that. Can I force it to open links with Safari instead?
You can implement the shouldStartLoadWithRequest method of the UIWebViewDelegate protocol to intercept link clicks. Inside that method you can use UIApplication's openURL method to cause the url to be opened in the appropriate application (ie. Safari for HTTP/HTTPS).
- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
if ([url isEqual:YOUR_HOME_URL_CONTSTANT])
return YES;
[[UIApplication sharedApplication] openURL:url];
return NO;
}
You should implement shouldStartLoadWithRequest of the UIWebViewDelegate. In here you can see when the UIWebView will start loading a URL. Then you can handle which should be loaded in this webView and which should be loaded by Safari.
My code looks like:
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType
{
if (inType != UIWebViewNavigationTypeLinkClicked)
{
//This was not a clicked link, so is probably the initial load
//and should be loaded in the UIWebView
return YES;
}
//This was a clicked link, so load using Safari
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
Here I am using UIWebViewNavigationType to determine whether it was a link click, and responding appropriately.

UIWebView in UIView need links to open up in browser

So i have an app that is not so much a webapp but more uses UIViewControllers and UIViews for most screens. On one particular controller i have a UIWebView control that only occupies a small portion of the UIViewController screen real estate that i load html from a web service as a string.
[self.webView loadHTMLString:marketingBlurb baseURL:nil];
what i'm wondering is if the html links in this string can open up on the browser and not in the UIWebView in my app???? How can i do this?? Is this even possible ?
Set the delegate of the webview to self, and intercept all links using webView:shouldStartLoadWithRequest:navigationType:
You can then call [UIApplication openURL] to open Safari.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}
I didn't test this code, but it will get you started.