From 8c81bd47bbe3ed65a55a2c5fd376bfd726bd6777 Mon Sep 17 00:00:00 2001 From: Struan Donald Date: Wed, 10 Jul 2013 15:17:41 +0100 Subject: upgrade iOS to phonegap 2.9 --- iPhone/CordovaLib/Classes/CDVViewController.m | 172 ++++++++++++++++++++------ 1 file changed, 137 insertions(+), 35 deletions(-) (limited to 'iPhone/CordovaLib/Classes/CDVViewController.m') diff --git a/iPhone/CordovaLib/Classes/CDVViewController.m b/iPhone/CordovaLib/Classes/CDVViewController.m index bec716d..94f4552 100755 --- a/iPhone/CordovaLib/Classes/CDVViewController.m +++ b/iPhone/CordovaLib/Classes/CDVViewController.m @@ -19,7 +19,6 @@ #import #import "CDV.h" -#import "CDVCommandQueue.h" #import "CDVCommandDelegateImpl.h" #import "CDVConfigParser.h" #import "CDVUserAgentUtil.h" @@ -103,6 +102,48 @@ return self; } +- (void)viewWillAppear:(BOOL)animated +{ + [super viewWillAppear:animated]; + + NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; + [nc addObserver:self + selector:@selector(keyboardWillShowOrHide:) + name:UIKeyboardWillShowNotification + object:nil]; + [nc addObserver:self + selector:@selector(keyboardWillShowOrHide:) + name:UIKeyboardWillHideNotification + object:nil]; +} + +- (void)viewWillDisappear:(BOOL)animated +{ + [super viewWillDisappear:animated]; + + NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; + [nc removeObserver:self name:UIKeyboardWillShowNotification object:nil]; + [nc removeObserver:self name:UIKeyboardWillHideNotification object:nil]; +} + +- (void)keyboardWillShowOrHide:(NSNotification*)notif +{ + if (![@"true" isEqualToString : self.settings[@"KeyboardShrinksView"]]) { + return; + } + BOOL showEvent = [notif.name isEqualToString:UIKeyboardWillShowNotification]; + + CGRect keyboardFrame = [notif.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; + keyboardFrame = [self.view convertRect:keyboardFrame fromView:nil]; + + CGRect newFrame = self.view.bounds; + if (showEvent) { + newFrame.size.height -= keyboardFrame.size.height; + } + self.webView.frame = newFrame; + self.webView.scrollView.contentInset = UIEdgeInsetsMake(0, 0, -keyboardFrame.size.height, 0); +} + - (void)printDeprecationNotice { if (!IsAtLeastiOSVersion(@"5.0")) { @@ -208,9 +249,6 @@ id backupWebStorage = self.settings[@"BackupWebStorage"]; if ([backupWebStorage isKindOfClass:[NSString class]]) { backupWebStorageType = backupWebStorage; - } else if ([backupWebStorage isKindOfClass:[NSNumber class]]) { - NSLog(@"Deprecated: BackupWebStorage boolean property is a string property now (none, local, cloud). A boolean value of 'true' will be mapped to 'cloud'. Consult the docs: http://docs.cordova.io/en/edge/guide_project-settings_ios_index.md.html#Project%%20Settings%%20for%%20iOS"); - backupWebStorageType = [(NSNumber*) backupWebStorage boolValue] ? @"cloud" : @"none"; } self.settings[@"BackupWebStorage"] = backupWebStorageType; @@ -231,6 +269,10 @@ if ([self.settings objectForKey:@"MediaPlaybackRequiresUserAction"]) { mediaPlaybackRequiresUserAction = [(NSNumber*)[settings objectForKey:@"MediaPlaybackRequiresUserAction"] boolValue]; } + BOOL hideKeyboardFormAccessoryBar = NO; // default value + if ([self.settings objectForKey:@"HideKeyboardFormAccessoryBar"]) { + hideKeyboardFormAccessoryBar = [(NSNumber*)[settings objectForKey:@"HideKeyboardFormAccessoryBar"] boolValue]; + } self.webView.scalesPageToFit = [enableViewportScale boolValue]; @@ -239,14 +281,26 @@ */ if ([enableLocation boolValue]) { + NSLog(@"Deprecated: The 'EnableLocation' boolean property is deprecated in 2.5.0, and will be removed in 3.0.0. Use the 'onload' boolean attribute (of the CDVLocation plugin."); [[self.commandDelegate getCommandInstance:@"Geolocation"] getLocation:[CDVInvokedUrlCommand new]]; } + if (hideKeyboardFormAccessoryBar) { + __weak CDVViewController* weakSelf = self; + [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification + object:nil + queue:[NSOperationQueue mainQueue] + usingBlock:^(NSNotification* notification) { + // we can't hide it here because the accessory bar hasn't been created yet, so we delay on the queue + [weakSelf performSelector:@selector(hideKeyboardFormAccessoryBar) withObject:nil afterDelay:0]; + }]; + } + /* * Fire up CDVLocalStorage to work-around WebKit storage limitations: on all iOS 5.1+ versions for local-only backups, but only needed on iOS 5.1 for cloud backup. */ if (IsAtLeastiOSVersion(@"5.1") && (([backupWebStorageType isEqualToString:@"local"]) || - ([backupWebStorageType isEqualToString:@"cloud"] && !IsAtLeastiOSVersion(@"6.0")))) { + ([backupWebStorageType isEqualToString:@"cloud"] && !IsAtLeastiOSVersion(@"6.0")))) { [self registerPlugin:[[CDVLocalStorage alloc] initWithWebView:self.webView] withClassName:NSStringFromClass([CDVLocalStorage class])]; } @@ -260,12 +314,19 @@ self.webView.mediaPlaybackRequiresUserAction = NO; } - // UIWebViewBounce property - defaults to true - NSNumber* bouncePreference = [self.settings objectForKey:@"UIWebViewBounce"]; - BOOL bounceAllowed = (bouncePreference == nil || [bouncePreference boolValue]); + // By default, overscroll bouncing is allowed. + // UIWebViewBounce has been renamed to DisallowOverscroll, but both are checked. + BOOL bounceAllowed = YES; + NSNumber* disallowOverscroll = [self.settings objectForKey:@"DisallowOverscroll"]; + if (disallowOverscroll == nil) { + NSNumber* bouncePreference = [self.settings objectForKey:@"UIWebViewBounce"]; + bounceAllowed = (bouncePreference == nil || [bouncePreference boolValue]); + } else { + bounceAllowed = ![disallowOverscroll boolValue]; + } // prevent webView from bouncing - // based on UIWebViewBounce key in config.xml + // based on the DisallowOverscroll/UIWebViewBounce key in config.xml if (!bounceAllowed) { if ([self.webView respondsToSelector:@selector(scrollView)]) { ((UIScrollView*)[self.webView scrollView]).bounces = NO; @@ -307,8 +368,16 @@ } } - for (NSString* pluginName in self.startupPluginNames) { - [self getCommandInstance:pluginName]; + if ([self.startupPluginNames count] > 0) { + [CDVTimer start:@"TotalPluginStartup"]; + + for (NSString* pluginName in self.startupPluginNames) { + [CDVTimer start:pluginName]; + [self getCommandInstance:pluginName]; + [CDVTimer stop:pluginName]; + } + + [CDVTimer stop:@"TotalPluginStartup"]; } // TODO: Remove this explicit instantiation once we move to cordova-CLI. @@ -318,16 +387,44 @@ // ///////////////// [CDVUserAgentUtil acquireLock:^(NSInteger lockToken) { - _userAgentLockToken = lockToken; - [CDVUserAgentUtil setUserAgent:self.userAgent lockToken:lockToken]; - if (!loadErr) { - NSURLRequest* appReq = [NSURLRequest requestWithURL:appURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0]; - [self.webView loadRequest:appReq]; - } else { - NSString* html = [NSString stringWithFormat:@" %@ ", loadErr]; - [self.webView loadHTMLString:html baseURL:nil]; + _userAgentLockToken = lockToken; + [CDVUserAgentUtil setUserAgent:self.userAgent lockToken:lockToken]; + if (!loadErr) { + NSURLRequest* appReq = [NSURLRequest requestWithURL:appURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0]; + [self.webView loadRequest:appReq]; + } else { + NSString* html = [NSString stringWithFormat:@" %@ ", loadErr]; + [self.webView loadHTMLString:html baseURL:nil]; + } + }]; +} + +- (void)hideKeyboardFormAccessoryBar +{ + NSArray* windows = [[UIApplication sharedApplication] windows]; + + for (UIWindow* window in windows) { + for (UIView* view in window.subviews) { + if ([[view description] hasPrefix:@"