CB-5595 Add toolbarposition=top option.

The position of the toolbar is now configurable, it can be specified on
the caller's side. The possible values are top and bottom and the key
for the setting is toolbarposition. Also extended the logging of the web
view's load errors, it now logs the error code too, not just the
localized description of the error that occured. Added the
browserOptions to the init method's parameters in order to make it
conveniently accessible from the ViewController.


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-inappbrowser/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/cordova-plugin-inappbrowser/commit/20611efe
Tree: 
http://git-wip-us.apache.org/repos/asf/cordova-plugin-inappbrowser/tree/20611efe
Diff: 
http://git-wip-us.apache.org/repos/asf/cordova-plugin-inappbrowser/diff/20611efe

Branch: refs/heads/master
Commit: 20611efe67ae0ed78db21d4224069d54f00c6fda
Parents: e819041
Author: Peter Somogyvari <[email protected]>
Authored: Mon Nov 18 15:00:25 2013 +0000
Committer: Andrew Grieve <[email protected]>
Committed: Fri Dec 13 09:16:04 2013 -0500

----------------------------------------------------------------------
 src/ios/CDVInAppBrowser.h | 48 ++++++++++++-----------
 src/ios/CDVInAppBrowser.m | 86 ++++++++++++++++++++++++++----------------
 2 files changed, 79 insertions(+), 55 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-inappbrowser/blob/20611efe/src/ios/CDVInAppBrowser.h
----------------------------------------------------------------------
diff --git a/src/ios/CDVInAppBrowser.h b/src/ios/CDVInAppBrowser.h
index 059edb0..b506b1f 100644
--- a/src/ios/CDVInAppBrowser.h
+++ b/src/ios/CDVInAppBrowser.h
@@ -38,11 +38,33 @@
 
 @end
 
+@interface CDVInAppBrowserOptions : NSObject {}
+
+@property (nonatomic, assign) BOOL location;
+@property (nonatomic, assign) BOOL toolbar;
+@property (nonatomic, copy) NSString* closebuttoncaption;
+@property (nonatomic, copy) NSString* toolbarbarposition;
+
+@property (nonatomic, copy) NSString* presentationstyle;
+@property (nonatomic, copy) NSString* transitionstyle;
+
+@property (nonatomic, assign) BOOL enableviewportscale;
+@property (nonatomic, assign) BOOL mediaplaybackrequiresuseraction;
+@property (nonatomic, assign) BOOL allowinlinemediaplayback;
+@property (nonatomic, assign) BOOL keyboarddisplayrequiresuseraction;
+@property (nonatomic, assign) BOOL suppressesincrementalrendering;
+@property (nonatomic, assign) BOOL hidden;
+
++ (CDVInAppBrowserOptions*)parseOptions:(NSString*)options;
+
+@end
+
 @interface CDVInAppBrowserViewController : UIViewController 
<UIWebViewDelegate>{
     @private
     NSString* _userAgent;
     NSString* _prevUserAgent;
     NSInteger _userAgentLockToken;
+    CDVInAppBrowserOptions *_browserOptions;
     CDVWebViewDelegate* _webViewDelegate;
 }
 
@@ -61,29 +83,9 @@
 - (void)close;
 - (void)navigateTo:(NSURL*)url;
 - (void)showLocationBar:(BOOL)show;
-- (void)showToolBar:(BOOL)show;
+- (void)showToolBar:(BOOL)show : (NSString *) toolbarPosition;
 - (void)setCloseButtonTitle:(NSString*)title;
 
-- (id)initWithUserAgent:(NSString*)userAgent 
prevUserAgent:(NSString*)prevUserAgent;
+- (id)initWithUserAgent:(NSString*)userAgent 
prevUserAgent:(NSString*)prevUserAgent browserOptions: 
(CDVInAppBrowserOptions*) browserOptions;
 
-@end
-
-@interface CDVInAppBrowserOptions : NSObject {}
-
-@property (nonatomic, assign) BOOL location;
-@property (nonatomic, assign) BOOL toolbar;
-@property (nonatomic, copy) NSString* closebuttoncaption;
-
-@property (nonatomic, copy) NSString* presentationstyle;
-@property (nonatomic, copy) NSString* transitionstyle;
-
-@property (nonatomic, assign) BOOL enableviewportscale;
-@property (nonatomic, assign) BOOL mediaplaybackrequiresuseraction;
-@property (nonatomic, assign) BOOL allowinlinemediaplayback;
-@property (nonatomic, assign) BOOL keyboarddisplayrequiresuseraction;
-@property (nonatomic, assign) BOOL suppressesincrementalrendering;
-@property (nonatomic, assign) BOOL hidden;
-
-+ (CDVInAppBrowserOptions*)parseOptions:(NSString*)options;
-
-@end
+@end
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cordova-plugin-inappbrowser/blob/20611efe/src/ios/CDVInAppBrowser.m
----------------------------------------------------------------------
diff --git a/src/ios/CDVInAppBrowser.m b/src/ios/CDVInAppBrowser.m
index 9839ccc..8b5e14d 100644
--- a/src/ios/CDVInAppBrowser.m
+++ b/src/ios/CDVInAppBrowser.m
@@ -26,6 +26,9 @@
 #define    kInAppBrowserTargetSystem @"_system"
 #define    kInAppBrowserTargetBlank @"_blank"
 
+#define    kInAppBrowserToolbarBarPositionBottom @"bottom"
+#define    kInAppBrowserToolbarBarPositionTop @"top"
+
 #define    TOOLBAR_HEIGHT 44.0
 #define    LOCATIONBAR_HEIGHT 21.0
 #define    FOOTER_HEIGHT ((TOOLBAR_HEIGHT) + (LOCATIONBAR_HEIGHT))
@@ -106,9 +109,10 @@
 
 - (void)openInInAppBrowser:(NSURL*)url withOptions:(NSString*)options
 {
+    CDVInAppBrowserOptions* browserOptions = [CDVInAppBrowserOptions 
parseOptions:options];
     if (self.inAppBrowserViewController == nil) {
         NSString* originalUA = [CDVUserAgentUtil originalUserAgent];
-        self.inAppBrowserViewController = [[CDVInAppBrowserViewController 
alloc] initWithUserAgent:originalUA prevUserAgent:[self.commandDelegate 
userAgent]];
+        self.inAppBrowserViewController = [[CDVInAppBrowserViewController 
alloc] initWithUserAgent:originalUA prevUserAgent:[self.commandDelegate 
userAgent] browserOptions: browserOptions];
         self.inAppBrowserViewController.navigationDelegate = self;
 
         if ([self.viewController 
conformsToProtocol:@protocol(CDVScreenOrientationDelegate)]) {
@@ -118,9 +122,8 @@
 
     _previousStatusBarStyle = [UIApplication sharedApplication].statusBarStyle;
 
-    CDVInAppBrowserOptions* browserOptions = [CDVInAppBrowserOptions 
parseOptions:options];
     [self.inAppBrowserViewController showLocationBar:browserOptions.location];
-    [self.inAppBrowserViewController showToolBar:browserOptions.toolbar];
+    [self.inAppBrowserViewController showToolBar:browserOptions.toolbar 
:browserOptions.toolbarbarposition];
     if (browserOptions.closebuttoncaption != nil) {
         [self.inAppBrowserViewController 
setCloseButtonTitle:browserOptions.closebuttoncaption];
     }
@@ -390,12 +393,13 @@
 
 @synthesize currentURL;
 
-- (id)initWithUserAgent:(NSString*)userAgent 
prevUserAgent:(NSString*)prevUserAgent
+- (id)initWithUserAgent:(NSString*)userAgent 
prevUserAgent:(NSString*)prevUserAgent browserOptions: 
(CDVInAppBrowserOptions*) browserOptions
 {
     self = [super init];
     if (self != nil) {
         _userAgent = userAgent;
         _prevUserAgent = prevUserAgent;
+        _browserOptions = browserOptions;
         _webViewDelegate = [[CDVWebViewDelegate alloc] initWithDelegate:self];
         [self createViews];
     }
@@ -408,10 +412,10 @@
     // We create the views in code for primarily for ease of upgrades and not 
requiring an external .xib to be included
 
     CGRect webViewBounds = self.view.bounds;
-
-    webViewBounds.size.height -= FOOTER_HEIGHT;
-
+    BOOL toolbarIsAtBottom = ![_browserOptions.toolbarbarposition 
isEqualToString:kInAppBrowserToolbarBarPositionTop];
+    webViewBounds.size.height -= _browserOptions.location ? FOOTER_HEIGHT : 
TOOLBAR_HEIGHT;
     self.webView = [[UIWebView alloc] initWithFrame:webViewBounds];
+    
     self.webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | 
UIViewAutoresizingFlexibleHeight);
 
     [self.view addSubview:self.webView];
@@ -453,7 +457,10 @@
     UIBarButtonItem* fixedSpaceButton = [[UIBarButtonItem alloc] 
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil 
action:nil];
     fixedSpaceButton.width = 20;
 
-    self.toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, 
(self.view.bounds.size.height - TOOLBAR_HEIGHT), self.view.bounds.size.width, 
TOOLBAR_HEIGHT)];
+    float toolbarY = toolbarIsAtBottom ? self.view.bounds.size.height - 
TOOLBAR_HEIGHT : 0.0;
+    CGRect toolbarFrame = CGRectMake(0.0, toolbarY, 
self.view.bounds.size.width, TOOLBAR_HEIGHT);
+    
+    self.toolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];
     self.toolbar.alpha = 1.000;
     self.toolbar.autoresizesSubviews = YES;
     self.toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth | 
UIViewAutoresizingFlexibleTopMargin;
@@ -468,7 +475,9 @@
     self.toolbar.userInteractionEnabled = YES;
 
     CGFloat labelInset = 5.0;
-    self.addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(labelInset, 
(self.view.bounds.size.height - FOOTER_HEIGHT), self.view.bounds.size.width - 
labelInset, LOCATIONBAR_HEIGHT)];
+    float locationBarY = toolbarIsAtBottom ? self.view.bounds.size.height - 
FOOTER_HEIGHT : self.view.bounds.size.height - LOCATIONBAR_HEIGHT;
+    
+    self.addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(labelInset, 
locationBarY, self.view.bounds.size.width - labelInset, LOCATIONBAR_HEIGHT)];
     self.addressLabel.adjustsFontSizeToFitWidth = NO;
     self.addressLabel.alpha = 1.000;
     self.addressLabel.autoresizesSubviews = YES;
@@ -510,6 +519,11 @@
     [self.view addSubview:self.spinner];
 }
 
+- (void) setWebViewFrame : (CGRect) frame {
+    NSLog(@"Setting the WebView's frame to %@", NSStringFromCGRect(frame));
+    [self.webView setFrame:frame];
+}
+
 - (void)setCloseButtonTitle:(NSString*)title
 {
     // the advantage of using UIBarButtonSystemItemDone is the system will 
localize it for you automatically
@@ -544,7 +558,7 @@
 
             CGRect webViewBounds = self.view.bounds;
             webViewBounds.size.height -= FOOTER_HEIGHT;
-            self.webView.frame = webViewBounds;
+            [self setWebViewFrame:webViewBounds];
 
             locationbarFrame.origin.y = webViewBounds.size.height;
             self.addressLabel.frame = locationbarFrame;
@@ -553,7 +567,7 @@
 
             CGRect webViewBounds = self.view.bounds;
             webViewBounds.size.height -= LOCATIONBAR_HEIGHT;
-            self.webView.frame = webViewBounds;
+            [self setWebViewFrame:webViewBounds];
 
             locationbarFrame.origin.y = webViewBounds.size.height;
             self.addressLabel.frame = locationbarFrame;
@@ -567,17 +581,15 @@
             // webView take up whole height less toolBar height
             CGRect webViewBounds = self.view.bounds;
             webViewBounds.size.height -= TOOLBAR_HEIGHT;
-            self.webView.frame = webViewBounds;
+            [self setWebViewFrame:webViewBounds];
         } else {
             // no toolBar, expand webView to screen dimensions
-
-            CGRect webViewBounds = self.view.bounds;
-            self.webView.frame = webViewBounds;
+            [self setWebViewFrame:self.view.bounds];
         }
     }
 }
 
-- (void)showToolBar:(BOOL)show
+- (void)showToolBar:(BOOL)show : (NSString *) toolbarPosition
 {
     CGRect toolbarFrame = self.toolbar.frame;
     CGRect locationbarFrame = self.addressLabel.frame;
@@ -591,30 +603,31 @@
 
     if (show) {
         self.toolbar.hidden = NO;
-
+        CGRect webViewBounds = self.view.bounds;
+        
         if (locationbarVisible) {
             // locationBar at the bottom, move locationBar up
             // put toolBar at the bottom
-
-            CGRect webViewBounds = self.view.bounds;
             webViewBounds.size.height -= FOOTER_HEIGHT;
-            self.webView.frame = webViewBounds;
-
             locationbarFrame.origin.y = webViewBounds.size.height;
             self.addressLabel.frame = locationbarFrame;
-
-            toolbarFrame.origin.y = (webViewBounds.size.height + 
LOCATIONBAR_HEIGHT);
             self.toolbar.frame = toolbarFrame;
         } else {
             // no locationBar, so put toolBar at the bottom
-
             CGRect webViewBounds = self.view.bounds;
             webViewBounds.size.height -= TOOLBAR_HEIGHT;
-            self.webView.frame = webViewBounds;
-
-            toolbarFrame.origin.y = webViewBounds.size.height;
             self.toolbar.frame = toolbarFrame;
         }
+        
+        if ([toolbarPosition 
isEqualToString:kInAppBrowserToolbarBarPositionTop]) {
+            toolbarFrame.origin.y = 0;
+            webViewBounds.origin.y += toolbarFrame.size.height;
+            [self setWebViewFrame:webViewBounds];
+        } else {
+            toolbarFrame.origin.y = (webViewBounds.size.height + 
LOCATIONBAR_HEIGHT);
+        }
+        [self setWebViewFrame:webViewBounds];
+        
     } else {
         self.toolbar.hidden = YES;
 
@@ -625,16 +638,14 @@
             // webView take up whole height less locationBar height
             CGRect webViewBounds = self.view.bounds;
             webViewBounds.size.height -= LOCATIONBAR_HEIGHT;
-            self.webView.frame = webViewBounds;
+            [self setWebViewFrame:webViewBounds];
 
             // move locationBar down
             locationbarFrame.origin.y = webViewBounds.size.height;
             self.addressLabel.frame = locationbarFrame;
         } else {
             // no locationBar, expand webView to screen dimensions
-
-            CGRect webViewBounds = self.view.bounds;
-            self.webView.frame = webViewBounds;
+            [self setWebViewFrame:self.view.bounds];
         }
     }
 }
@@ -703,10 +714,20 @@
     if (IsAtLeastiOSVersion(@"7.0")) {
         [[UIApplication sharedApplication] setStatusBarStyle:[self 
preferredStatusBarStyle]];
     }
+    [self rePositionViews];
     
     [super viewWillAppear:animated];
 }
 
+- (void) rePositionViews {
+    if ([_browserOptions.toolbarbarposition 
isEqualToString:kInAppBrowserToolbarBarPositionTop]) {
+        [self.webView setFrame:CGRectMake(self.webView.frame.origin.x, 
TOOLBAR_HEIGHT, self.webView.frame.size.width, self.webView.frame.size.height)];
+        
+        float offsetForStatusBar = IsAtLeastiOSVersion(@"7.0") ? 21.0 : 0.0;
+        [self.toolbar setFrame:CGRectMake(self.toolbar.frame.origin.x, 
self.toolbar.frame.origin.y + offsetForStatusBar, 
self.toolbar.frame.size.width, self.toolbar.frame.size.height)];
+    }
+}
+
 #pragma mark UIWebViewDelegate
 
 - (void)webViewDidStartLoad:(UIWebView*)theWebView
@@ -764,7 +785,7 @@
 - (void)webView:(UIWebView*)theWebView didFailLoadWithError:(NSError*)error
 {
     // log fail message, stop spinner, update back/forward
-    NSLog(@"webView:didFailLoadWithError - %@", [error localizedDescription]);
+    NSLog(@"webView:didFailLoadWithError - %i: %@", error.code, [error 
localizedDescription]);
 
     self.backButton.enabled = theWebView.canGoBack;
     self.forwardButton.enabled = theWebView.canGoForward;
@@ -814,6 +835,7 @@
         self.location = YES;
         self.toolbar = YES;
         self.closebuttoncaption = nil;
+        self.toolbarbarposition = kInAppBrowserToolbarBarPositionBottom;
 
         self.enableviewportscale = NO;
         self.mediaplaybackrequiresuseraction = NO;

Reply via email to