Change to Different View Controller Immediately after authenticating in a Web View - ios

I have a UIWebView that opens up a webpage in my app where a user can log in to their account. What I want the app to do is to change view controllers immediately after the user has logged into their account without seeing the next webpage in the webview. This is the code in my
viewDidLoad function:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *webpage = #"myfakewebpage.com";
self.defaults = [NSUserDefaults standardUserDefaults];
[self.activityIndicator startAnimating];
[self.myWebView setHidden:YES];
self.myWebView.delegate = self;
NSURL *myURL = [NSURL URLWithString:triggerPage];
self.myRequest = [NSURLRequest requestWithURL:myURL];
[self.myWebView loadRequest:self.myRequest];
}
And this is what my webViewDidFinishLoad function looks like:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
if ([self.activityIndicator isAnimating] == YES) {
[self.activityIndicator stopAnimating];
[self.activityIndicator setHidden:YES];
[self.myWebView setHidden:NO];
}
[self loadViewControllerAfterAuthentication];
}
[self loadViewControllerAfterAuthentication]; calls a function I built myself that will change the view controller through a poor workaround based on the response of the webpage after the user has authenticated. As of right now, the user can authenticate, but the next webpage will load for about half a second before my code kicks in and a new view controller is displayed. If anyone has an idea on how I can make the transition described above any smoother, that would be greatly appreciated.

Related

if I clicked IBAction login button in login form,go to web view in ios

If I pressed login action button
- (IBAction)login button:(id)sender
I would like to go webview without using triggered segues.
Because I have two web views.If I pressed login button,I would like to go login webview, else I would like to go home webview. It depends on one button click.
I used delegate protocol and create two instance method and then,I would like to call method by using [self method name].delegate is okay but login button action didn't go to webview.
How can I do that?
piece of code in viewcontroller.m
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)loginCompleted{
NSString *fullURL = #"http://www.example.com/login";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[homeWebView loadRequest:requestObj];
}
-(void)homeCompleted{
NSString *fullURL = #"http://www.example.com/home";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[homeWebView loadRequest:requestObj];
}
- (IBAction)loginEvent:(id)sender {
if ([sender isEqual:loginbtn]) {
[self loginCompleted];
NSLog(#"Button pressed");
} else {
NSLog(#"Button pressed2"); }
}
Where is your loginbtn? How you set it?
set it's tag
loginbtn.tag = 1
In loginEvent compare the tag like this
if (sender.tag == 1) {
[self loginCompleted];
NSLog(#"Button pressed");
} else {
NSLog(#"Button pressed2");
}
You can select and deselect your button depending on what action you want to trigger: loginbtn.isSelected=true. And then:
- (IBAction)loginEvent:(id)sender {
if ([sender isSelected]) {
[self loginCompleted];
NSLog(#"Button pressed");
} else {
NSLog(#"Button pressed2");
}
}
(I write in Swift so it might be written a bit differently)
What you say 'If I pressed login button,I would like to go login web view,else I would like to go home web view.It depends on one button click.' causes me confused.What are the 'else cases' ?
I guest you are hoping to load the home page first,if the user press login button,it goes to the login page.
If it's true,you should add requestObj to the view controller's property,cancel the request if needed when the user press login button.
You say you have two webViews,but you load login request to the homeWebView see loginCompleted method

UIActivityIndicator does not show/hide based on the loading of the UIWebView

I want to load a URL on a UIViewController with an UIWebView and UIActivityIndicatorView, but UIActivityIndicator never appears and UIWebView never loads the URL.
This is my code:
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.title = #"Web";
[self displayURL:[NSURL URLWithString:#"(Website)"]];
}
-(void) webViewDidFinishLoad:(UIWebView *)webView {
[self.loadView stopAnimating];
self.loadView.hidden = YES;
}
-(void) displayURL:(NSURL *) aURL {
self.web.delegate = self;
self.loadView.hidden = NO;
[self.loadView startAnimating];
[self.web loadRequest:[NSURLRequest requestWithURL:aURL]];
}
If you add your webView by code, you need add
[self.view addSubview: web];
after you created webView, and of course you need to add UIActivityIndicatorView as a subView of webView or superView, so you need to add [web addSubview: loadView] or [self.view insertSubview: loadView aboveSubview: web];.
And about the URL, if your website URL is like www.google.com, you need to change #"(Website)" to the real URL. If your website is a file copied in your project, you need to change [NSURL URLWithString: ]; to [NSURL fileURLWithPath:[NSBundle mainBundle][pathForResource: ofType:]];.
Really poorly formulated question.
UIWebViewController does not work
UIWebViewController does not exists, you are talking about a UIWebView in a UIViewController
Code is very incomplete

How do I enable my back button only when a link is clicked in my UIWebView? (Code won't execute)

I'm trying to make it so that the back button will only be enabled when the user presses a link and/or if canGoBack = TRUE. I tried using an if statement in the body but that didn't work. I then tried using what I could find online and it still didn't work. My code is below. What am I doing wrong? Thanks!
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest: (NSURLRequest *)request navigationType: (UIWebViewNavigationType)navigationType{
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
NSString *url = webView.request.URL.absoluteString;
NSURL *nsurl = [NSURL URLWithString:url];
NSURLRequest *nsrequest = [NSURLRequest requestWithURL:nsurl];
[webView loadRequest:nsrequest];
backButton.enabled = TRUE;
}
return TRUE;
}
shouldStartLoadWithRequest() won't get called if you haven't set the web view's delegate.
In order to use UIWebView its typically set as an outlet property of a view controller, so you should have something like this:
IBOutlet UIWebView* webView;
The webView has a delegate property which needs setting to your view controller, you can set delegates in a storyboard, but its more explicit to set it in code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.webView.delegate = self;
99% of the time you just set delegates, but UIWebView's delegate is funny in that it needs setting to nil when finished with. So add this:
- (void)viewDidUnload
{
[self.webView stopLoading];
self.webView.delegate = nil;
UIWebView and WKWebView both have a canGoBack and canGoForward property on them. You can use KVO to see when these are enabled, and enable/disable your back and forward buttons accordingly.
http://nshipster.com/key-value-observing/

UIWebView back button implementation issue in iPad

I have implemented a browser in my application by using UIWebView, by default I'm loading google page in my browser.
When I search something in the google page ,the UIWebViewDelegate's webView:shouldStartLoadWithRequest:navigationType: method is called.
The problem is when I tap on the back button from this search page no delegates are getting called, so I am having a problem disabling my back button.
This problem happens only in an iPad application not in an iPhone application.
This code may help u...
A UIWebView is a UIView that can load a web page while remaining in the user's application.
Navigation to other webpages is allowed through the use of imbedded links in a web page itself. Forward and backward navigation through history can be set up with instance methods goForward and goBack, but the programmer must supply the buttons.
The following example uses a UIWebView, and
1) adds forward and backward buttons. The buttons are enabled and highlighted using UIWebViewDelegate optional methods webViewDidStartLoad: and webViewDidFinishLoad:
2) adds a UIActivityIndicatorView which displays while the web page is loading
In the .h file for the WebViewController :
Declare the UIWebView, Optionally : add buttons to control moving forward and backward through browsing history and IBActions for pressing the buttons, Optionally again : add a UIActivityIndicatorView.
#interface WebViewController : UIViewController <UIWebViewDelegate>
{
UIWebView *webView;
UIButton *back;
UIButton *forward;
UIActivityIndicatorView *activityIndicator;
}
#property(nonatomic,retain)IBOutlet UIWebView *webView;
#property(nonatomic,retain)IBOutlet UIButton *back;
#property(nonatomic,retain)IBOutlet UIButton *forward;
#property(nonatomic,retain)IBOutlet UIActivityIndicatorView *activityIndicator;
-(IBAction)backButtonPressed: (id)sender;
-(IBAction)forwardButtonPressed: (id)sender;
#end
//In the .m file for the WebViewController
#implementation WebViewController
#synthesize webView;
#synthesize back;
#synthesize forward;
#synthesize activityIndicator;
//method for going backwards in the webpage history
-(IBAction)backButtonPressed:(id)sender {
[webView goBack];
}
//method for going forward in the webpage history
-(IBAction)forwardButtonPressed:(id)sender
{
[webView goForward];
}
//programmer defined method to load the webpage
-(void)startWebViewLoad
{
//NSString *urlAddress = #"http://www.google.com";
NSString *urlAddress = #"http://cagt.bu.edu/page/IPhone-summer2010-wiki_problemsandsolutions";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
}
// acivityIndicator is set up here
- (void)viewDidLoad
{
//start an animator symbol for the webpage loading to follow
UIActivityIndicatorView *progressWheel = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
//makes activity indicator disappear when it is stopped
progressWheel.hidesWhenStopped = YES;
//used to locate position of activity indicator
progressWheel.center = CGPointMake(160, 160);
self.activityIndicator = progressWheel;
[self.view addSubview: self.activityIndicator];
[self.activityIndicator startAnimating];
[progressWheel release];
[super viewDidLoad];
//call another method to do the webpage loading
[self performSelector:#selector(startWebViewLoad) withObject:nil afterDelay:0];
}
- (void)dealloc
{
[webView release];
[back release];
[forward release];
[activityIndicator release];
[super dealloc];
}
#pragma mark UIWebViewDelegate methods
//only used here to enable or disable the back and forward buttons
- (void)webViewDidStartLoad:(UIWebView *)thisWebView
{
back.enabled = NO;
forward.enabled = NO;
}
- (void)webViewDidFinishLoad:(UIWebView *)thisWebView
{
//stop the activity indicator when done loading
[self.activityIndicator stopAnimating];
//canGoBack and canGoForward are properties which indicate if there is
//any forward or backward history
if(thisWebView.canGoBack == YES)
{
back.enabled = YES;
back.highlighted = YES;
}
if(thisWebView.canGoForward == YES)
{
forward.enabled = YES;
forward.highlighted = YES;
}
}
#end
/*****************************/
//In viewDidLoad for the class which adds the WebViewController:
WebViewController *ourWebVC = [[WebViewController alloc] initWithNibName:#"WebViewController" bundle:nil];
ourWebVC.title = #"WebView";
[self.view addSubview:ourWebVC];
//release ourWebVC somewhere else
In your case ,You have to ignore/avoid "caching data". Following lines of code may help.
NSURLRequest *requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"] cachePolicy: NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0];
[webView loadRequest:requestObj];

Another (!) Activity Indicator Issue

Well, after reading a bunch of SO posts on this issue, I still can't fix problems with my activity indicator. This indicator is in a view in a tab under the control of its own view controller. It has a view with a UIWebView which loads a local html page just fine. The view is loaded with initWithNibName and then awakeFromNib. Here's the part I think is relevant:
#implementation HelpViewController
#synthesize webView = _webView;
#synthesize back = _back;
#synthesize forward = _forward;
#synthesize aI = _aI;
- (void) viewDidLoad {
_aI = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[_aI stopAnimating];
// Now add the web view
NSString *filePath = [[NSBundle mainBundle]
pathForResource:#"Help"
ofType:#"html"];
[self.view addSubview:_webView];
_webView.delegate = self;
NSURL* fileURL = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
[_webView loadRequest:request];
[super viewDidLoad];
}
-(void) webViewDidStartLoad:(UIWebView *)webView {
[_aI startAnimating];
_back.enabled = NO;
_forward.enabled = NO;
}
-(void) webViewDidFinishLoad:(UIWebView *)webView {
[_aI stopAnimating];
if (webView.canGoBack) {
_back.enabled = YES;
_back.highlighted = YES;
}
if (webView.canGoForward) {
_forward.enabled = YES;
_forward.highlighted = YES;
}
}
The navigation buttons work fine. The activity indicator was placed in the nib, but in the main view, not on/over/under the webView. In the attributes, I have Hides When Stopped checked. If I check Animating the indicator is always visible and animated, no matter how I navigate through the UIWebView.. If I uncheck Animating, Hidden is automatically checked for me. In this case, the indicator never shows up. So it's either always on or always off.
I've read quite a bit about cases where you need to have the indicator on a different thread. I'm not sure, but I don't think that applies here (I load a local html page but allow users to navigate off and then back to the local page). But, I seem to have some disconnect; perhaps it is the fact that the indicator is in the main view but the pages are in the webView? Or I'm not calling things in the right methods. Or who knows... Thanks for any help!
You overwrite _aI in your viewDidLoad and never place it in your view hierarchy, so the object you are sending the messages to is never visible and the activity indicator placed in interface build will never change its state, so thats why its either always animating or hidden.

Resources