NetworkActivity indicator in webview - objective-c

ive got a view based application containg a button clicking the button takes you to a web view..wat i want is the netwokActivityindicator to swirl..after finish loading of the web page i want the networkActivity indicator to disappear ...below is the code
-(IBAction)Button4
{
WebViewGive *newEnterNameController4 = [[WebViewGive alloc] initWithNibName:#"WebViewGive"
bundle:[NSBundle mainBundle]];
[[self navigationController] pushViewController:newEnterNameController4 animated:YES];
[newEnterNameController4 release];
}
- (void)viewDidLoad {
[super viewDidLoad];
[UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
self.urlAddress1 = #"http://www.youtube.com/user/stevenandkeirabanks";
self.url1 = [NSURL URLWithString:urlAddress1];
self.requestObj1 = [NSURLRequest requestWithURL:url1];
[webViewAnnouncements loadRequest:requestObj1];
}
if i m to use the network activity indicator below the
[webViewAnnouncements loadRequest:requestObj1];
I m jst getting a flickr and not the swrling of the activity indicator

The simplest way to do this is to add this line after you instantiate your UIWebView.
[webView setDelegate:self];
Now you will call webViewDidStartLoad: and the entire method should look like this.
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
You can stop the network activity indicator as follows -
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}

Related

Objective C - When tapping the tab bar item again, how to reload web view again?

I really need help please. Initially, by tapping on a tab bar item, I will load a webview on Gym VC user will then scroll down the web view
What I am trying to accomplish is when user tap on the same tab bar item again, the webView will reload again. I managed to call the method handleRefresh from MyTabBarController But the method doesn't do anything at all. Any guidance is much appreciated.
Currently I have the following code
MyTabBarController.m
#import "GymNavigationController.h"
#import "Gym.h"
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if([viewController isKindOfClass:[GymNavigationController class]]){
Gym *myGym = [[Gym alloc] init];
[myGym handleRefresh:nil];
}
}
Gym.m
#import "Gym.h"
#import "AppDelegate.h"
#import "GymDet.h"
#import <QuartzCore/QuartzCore.h>
#interface Gym () {
AppDelegate *appDelegate;
NSString *sURL, *sRefresh;
}
#end
#implementation Gym
#synthesize webView;
- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
sURL = appDelegate.gURL;
sURL = [sURL stringByAppendingString:#"/apps/gym.asp?"];
NSLog(#" The sURL to be load for the current page : %# ", sURL);
sRefresh = sURL;
[defaults setObject:sRefresh forKey:#"txtRefresh"];
[defaults synchronize];
NSURL *url = [NSURL URLWithString:sURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView setDelegate:(id<UIWebViewDelegate>)self];
[webView loadRequest:urlRequest];
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
[webView.scrollView addSubview:refreshControl];
}
- (void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//[self handleRefresh:nil];
[[self navigationController] setNavigationBarHidden:YES animated:YES];
}
-(void)viewDidDisappear:(BOOL)animated{
[[self navigationController] setNavigationBarHidden:NO animated:YES];
}
- (void)handleRefresh:(UIRefreshControl *)refresh {
//--- Call the stored txtMemCode, something like session ---
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
sRefresh = [defaults objectForKey:#"txtRefresh"];
NSURL *url = [NSURL URLWithString:sRefresh];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
[refresh endRefreshing];
}
First i would like to suggest why you're initiating a new Gym object every time tabbar is tapped you should take a reference of the Gym object and initialise it if its nil then process ahead with the reference, but it maybe your requirement and if it is i would like to suggest you to create a BOOL variable lets say isUpdateRequire and when you're instantiating your viewcontroller assign isUpdateRequire = true. Then in the end of view did load or view will appear (according to your need) check
if isUpdateRequire {
[self handleRefresh:nil]
}
Alternatively you can create protocols in your webviewcontroller and assign its delegate to your tabbarcontroller and fire the delegate method when ever required.
and if you don't want the method to call when you come back to vc simply put this condition in viewWillAppear
if self.isMovingToParentViewController {
[self handleRefresh:nil]
}

Load initial webview every time tabbar is pressed

Edited
My initial webview is loaded when a tabbar is pressed, then when clicked on the webview, I called
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
to detect the URLString clicked
When the string matches my destination string, I will call the segue [self performSegueWithIdentifier:#"NewsDet" sender:self]; and also pass the URLString and load the detail page on another ViewController
Now, when I click the another tabbar and then click back to this tabbar. The detail page remains. I want to refresh back to initial page. How do I do so?
Is it the I need to create a new class with a subclass of tabviewcontroller? Please help.
Gym.m
- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
sURL = appDelegate.gURL;
sURL = [sURL stringByAppendingString:#"/apps/gym.asp"];
NSURL *url = [NSURL URLWithString:sURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView setDelegate:(id<UIWebViewDelegate>)self];
[webView loadRequest:urlRequest];
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
[webView.scrollView addSubview:refreshControl];
}
- (void)handleRefresh:(UIRefreshControl *)refresh {
NSURL *url = [NSURL URLWithString:sURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
[refresh endRefreshing];
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
sURL = appDelegate.gURL;
sURL = [sURL stringByAppendingString:#"/apps/gym.asp"];
NSURL *url = [NSURL URLWithString:sURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView setDelegate:(id<UIWebViewDelegate>)self];
[webView loadRequest:urlRequest];
}
GymDet.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
sURL = self.urlString;
NSURL *url = [NSURL URLWithString:sURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView setDelegate:(id<UIWebViewDelegate>)self];
[webView loadRequest:urlRequest];
}
Basically I have 5 tab controllers. Each Tab has a Navigation Controller. When I click on say Tab-A it will load initial webview in VC1 then when I click on webview URL, it will navigate to a new VC say VC2 and load the detailed page.
Now, if I click on say Tab-B and back to Tab-A. I want it to load the initial webview or VC1. Now, my program always stay at VC2.
After clarification on what you're trying to do...
Your issue has nothing to do with webviews or loading URLs. The problem is that you are navigating to a new VC in your NavigationController, then switching tabs, then switching back and you are still at the "pushed" VC.
A comment not related to the problem:
I am on Tab-A, viewing VC-A1... Via "normal" Navigation Controller
action, VC-A2 slides-in from the right. Now I select Tab-B, and I see
VC-B1. Now, I want to switch back to Tab-A, and I expect to see what
was there: VC-A2... but instead, you want to send me back to VC-A1. That really doesn't follow Apple's interface guidelines, and can be very confusing to your users.
However, to accomplish that, you want to subclass UITabBarController and implement didSelectViewController:
#import "MyTabBarViewController.h"
#interface MyTabBarViewController () <UITabBarControllerDelegate>
#end
#implementation MyTabBarViewController
- (void) viewDidLoad {
[super viewDidLoad];
self.delegate = self;
}
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if ([viewController isKindOfClass:[UINavigationController class]]) {
[(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
}
}
#end
That will work, but it will "reset" any Navigation Controller in every tab. You'd need to add some other logic to handle only the first tab.
(original answer)
viewDidLoad is only called once - when the view loads. Switching between tabs does not "re-load" the view.
You probably want to reload your original URL in viewWillAppear as that will be called each time you switch back to that tab.
Although, you probably also want to check if the original URL is the current one displayed - you likely only want to reload it if it's not.
And... you never want to call [self viewWillAppear:YES]; -- that is a notification provided by the system.

Html form in UIwebview send back to ios?

i am trying to pass information between an HTML form and ios i tryed javascrip but it isn't working?
// Init Web View
UIWebView *webView;
webView = [[UIWebView alloc] initWithFrame:boundsRect];
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
webView.scalesPageToFit = YES;
webView.contentMode = UIViewContentModeCenter;
webView.multipleTouchEnabled = NO;
webView.userInteractionEnabled = YES;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
[self addSubview:webView];
NSString *testArrayString = [webView stringByEvaluatingJavaScriptFromString:#"testString"];
NSArray *testArray = [testArrayString componentsSeparatedByString:#"#"];
NSLog(#"%#", testArray);
[webView release];
return self;
any ideas?
----EDIT----
Ok guys if u only want to make data from a form pass to a ios app you only do this to your webview
[webView setDelegate:self];
Then you can implement this methods in your program marks
like:
-(void)webViewDidFinishLoad:(UIWebView *) webView {
//implement code to do here
}
if you want to capture a for just do a webview shouldStartWithRequest like this:
-(BOOL)webView:(UIwebView *) webView shouStartWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigation{
//implement code to do afeter the user click the form don't forget to return YES or NO because it is a BOOL
}
that is how i solve it any question please do tell me i try my best to help
To pass data from UIWebView and back to your Objective-C runtime, you need to implement UIWebViewDelegates webView:shouldStartLoadingWithRequest: and pass your form-data as parameters in the URL. If you craft all your URL's with a scheme like f.ex. my-app://?parameter1=value1&parameter2=value2, you can easily filter out the URLs the web view should not load, and return NO.
EDIT:
Added example-code.
- (void)initWebView
{
UIWebView *webView;
webView = [[UIWebView alloc] initWithFrame:boundsRect];
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
webView.scalesPageToFit = YES;
webView.contentMode = UIViewContentModeCenter;
webView.multipleTouchEnabled = NO;
webView.userInteractionEnabled = YES;
webView.delegate = self;
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = request.URL;
if ([url.scheme isEqualToString:#"http://"]) {
return YES;
}
else if ([url.scheme isEqualToString:#"my-app://"]){
// Process data from form
return NO;
}
return YES;
}
EDIT2: You are releasing the web view in your initialisation-code. This prevents the delegate-method from being called when you are loading the form-url
EDIT3: See this question for more code-examples: Invoke method in objective c code from HTML code using UIWebView

UIButton not working in second time (objective-C)

I have a button in my first screen for loading a small game here is the code:
- (IBAction)playButtonPressed:(id)sender
{
[self startGameWithConfig:[RootViewController singleplayerGameConfig]];
NSLog(#"play again");
}
and inside of my game page I have a button to back to the first screen when I used this button I can back but my playButtonPressed not working any more, it print the NSLog but
the button it's not loading the game any more
here is my return button:
- (IBAction)return:(id)sender {
RootViewController *b = [[RootViewController alloc] init];
[self presentModalViewController:b animated:YES];
}
would you please help me to this implementation
Thanks in advance!
and here is my startGamingWithConfig
- (void) startGameWithConfig:(GameConfig *)config
{
// fade in the loading screen to hide load time
[_loadingView setHidden:NO];
[_loadingView setAlpha:0.0f];
[UIView animateWithDuration:0.2f
delay:0.0f
options:UIViewAnimationOptionCurveLinear
animations:^{
[_loadingView setAlpha:0.0f];
}
completion:^(BOOL finished){
CardsViewCtrl* newController = [[CardsViewCtrl alloc] init];
newController.gameConfig = config;
AppDelegate *delegate = (AppDelegate *)[[UIApplication
sharedApplication] delegate];
[delegate.navController pushFadeInViewController:newController
animated:YES];
}];
}
It is a bit hard for me to understand where this code is since there are no headings but it looks like when you are attempting to go back to the RootViewController you are creating a new one and pushing that on the stack. You want to make sure that your topViewController is removed, not adding more views.
So for a starter try to replace return method:
- (IBAction)return:(id)sender {
RootViewController *b = [[RootViewController alloc] init];
[self presentModalViewController:b animated:YES];
}
with this:
- (IBAction)goBack:(id)sender {
[self dismissModalViewController];
}
I am not sure how you have your view hierarchy setup, but if you have a working navigationController than try using [self.navigationController popNavigationController:YES];

Status Bar doesnot display when adding UIImagePickerController

I want to show a Camera view in between Navigation bar & Tab bar, so I added the UIImagePickerController object as follows.
picker = [[Camera3DViewController alloc] init];
picker.allowsImageEditing = NO;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.showsCameraControls = NO;
picker.view.transform = CGAffineTransformScale(picker.view.transform, 1, 1);
[self.view addSubview:picker.view];
[picker viewWillAppear:YES];
[picker viewDidAppear:YES];
Note that Camera3DViewController is a subclass of UIImagePickerController Class.
Camera get display but status bar does not shown, so I use,
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];
but still it does not show the status bar.
Please guide me to solve the above problem.
I subclassed UIImagePictureController and added:
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:NO animated:animated];
}
It is important to show status after [super viewDidAppear:animated]; call, else it didn't appear. Also it is important to call superclass method.