Skip to content

Instantly share code, notes, and snippets.

@rodi01
Created August 18, 2011 23:59
Show Gist options
  • Save rodi01/1155590 to your computer and use it in GitHub Desktop.
Save rodi01/1155590 to your computer and use it in GitHub Desktop.
#import "WebServerBaseViewController.h"
#import "GogobotApi.h"
#import "NXOAuth2Client.h"
#import "UIHelpers.h"
#import "GogobotAppDelegate.h"
#import "NSDictionary+QueryString.h"
@interface WebServerBaseViewController (Private)
- (void)loadWebView;
@end
@implementation WebServerBaseViewController
@synthesize webView = _webView;
//@synthesize activityIndicator = _activityIndicator;
- (NSString *)pageUrl { [self doesNotRecognizeSelector:_cmd]; @throw @""; }
- (BOOL)requireUser { [self doesNotRecognizeSelector:_cmd]; @throw @""; }
- (BOOL)showNavigationBarInWebView { return YES; }
- (BOOL)nibbable { return YES; }
- (NSString *)nibName { return @"WebServerBaseViewController"; }
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backToInitialUrlAndRefresh) name:kLoginSuccessfulNotificationKey object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backToInitialUrlAndRefresh) name:kLogoutSuccessfulNotificationKey object:nil];
// disables bounce of the webview on scroll. bounce should be implemented in the html page
for (id subview in _webView.subviews) {
if ([[subview class] isSubclassOfClass: [UIScrollView class]]) {
((UIScrollView *)subview).bounces = NO;
}
}
}
- (void)backToInitialUrlAndRefresh {
[self loadWebView];
}
- (void)loadWebView {
NSString *accessToken = [[GogobotApi sharedInstance] oAuthClient].accessToken.accessToken;
if ([self requireUser] && !accessToken) return;
NSMutableString *url = [NSMutableString stringWithFormat:@"mobile/%@", [self pageUrl]];
[url appendString:[url rangeOfString:@"?"].location == NSNotFound ? @"?" : @"&"];
if (![self showNavigationBarInWebView]) [url appendString:@"show_nav_bar=0"];
if ([self requireUser]) {
NSMutableDictionary *qs = [NSMutableDictionary dictionaryWithObjectsAndKeys:
accessToken, @"oauth_access_token",
url, @"return_url",
nil];
url = [NSMutableString stringWithFormat:@"mobile/users/authenticate?%@", [qs urlEncodedString]];
}
url = [NSMutableString stringWithFormat:@"%@%@", GOGOBOT_MOBILE_SITE_URL, url];
GOGO_LOG(@"WebView (%@) URL = %@", [self class], url);
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
[_webView loadRequest:request];
[request release];
_webViewRequestSent = YES;
}
- (void)updateBackButton {
if ([self.webView canGoBack]) {
if (!self.navigationItem.leftBarButtonItem) {
[self.navigationItem setHidesBackButton:YES animated:YES];
UIBarButtonItem *backItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(backWasClicked:)] autorelease];
[self.navigationItem setLeftBarButtonItem:backItem animated:YES];
}
}
else {
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
[self.navigationItem setHidesBackButton:NO animated:YES];
}
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
// [_activityIndicator startAnimating];
[self updateBackButton];
[UIHelpers showActivityLabelInView:self.view];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// [_activityIndicator stopAnimating];
[self updateBackButton];
[UIHelpers hideActivityLabelInView:self.view];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
// [_activityIndicator stopAnimating];
[UIHelpers hideActivityLabelInView:self.view];
GOGO_LOG(@"Error loading webview = %@", error);
}
- (void)backWasClicked:(id)sender {
if ([self.webView canGoBack]) {
[self.webView goBack];
}
}
- (void)viewWillAppear:(BOOL)animated {
if (
[self requireUser] &&
![[UIHelpers appDelegate] requireUserOnViewController:self]){
return;
}
[super viewWillAppear:animated];
if ([self showNavigationBarInWebView]) {
[[self navigationController] setNavigationBarHidden:YES animated:NO];
}
if (!_webViewRequestSent) [self loadWebView];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:request.URL];
return false;
}
return true;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
if (self.isViewLoaded && self.view.window) return;
// make it load deafult about:blank
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
_webViewRequestSent = NO;
}
- (void)viewDidUnload {
self.webView = nil;
// self.activityIndicator = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self name:kLoginSuccessfulNotificationKey object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:kLogoutSuccessfulNotificationKey object:nil];
[super viewDidUnload];
}
- (void)dealloc {
[_webView release];
// [_activityIndicator release];
[super dealloc];
}
//- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
// navigationType:(UIWebViewNavigationType)navigationType {
// NSString *url = [[request URL] description];
//
// // Check for protocol type and determine routing
// if (navigationType == UIWebViewNavigationTypeOther) {
// return YES;
// } else if ([url hasPrefix:@"miso://ajax"]) {
//// [_ajaxController fireAjaxRequest:url];
// return NO;
// } else {
// // RoutesController delegates native app actions given a miso://<controller>/<action>?<params>
//// [[RoutesController instance] processRoute:url viewController:_vc webview:self];
// return NO;
// }
//}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment