Skip to content

Commit 4294024

Browse files
committed
CB-12320: (iOS) Add support for navigation bar
1 parent f32917d commit 4294024

File tree

2 files changed

+46
-19
lines changed

2 files changed

+46
-19
lines changed

src/ios/CDVInAppBrowser.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848

4949
@property (nonatomic, assign) BOOL location;
5050
@property (nonatomic, assign) BOOL toolbar;
51+
@property (nonatomic, assign) BOOL navigationbar;
52+
@property (nonatomic, assign) BOOL navigationbuttons;
53+
@property (nonatomic, copy) NSString* navigationbartitle;
5154
@property (nonatomic, copy) NSString* closebuttoncaption;
5255
@property (nonatomic, copy) NSString* toolbarposition;
5356
@property (nonatomic, assign) BOOL clearcache;
@@ -74,13 +77,13 @@
7477
NSString* _prevUserAgent;
7578
NSInteger _userAgentLockToken;
7679
CDVInAppBrowserOptions *_browserOptions;
77-
80+
7881
#ifdef __CORDOVA_4_0_0
7982
CDVUIWebViewDelegate* _webViewDelegate;
8083
#else
8184
CDVWebViewDelegate* _webViewDelegate;
8285
#endif
83-
86+
8487
}
8588

8689
@property (nonatomic, strong) IBOutlet UIWebView* webView;

src/ios/CDVInAppBrowser.m

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ - (void)close:(CDVInvokedUrlCommand*)command
7070

7171
- (BOOL) isSystemUrl:(NSURL*)url
7272
{
73-
if ([[url host] isEqualToString:@"itunes.apple.com"]) {
74-
return YES;
75-
}
73+
if ([[url host] isEqualToString:@"itunes.apple.com"]) {
74+
return YES;
75+
}
7676

77-
return NO;
77+
return NO;
7878
}
7979

8080
- (void)open:(CDVInvokedUrlCommand*)command
@@ -161,7 +161,7 @@ - (void)openInInAppBrowser:(NSURL*)url withOptions:(NSString*)options
161161
}
162162

163163
[self.inAppBrowserViewController showLocationBar:browserOptions.location];
164-
[self.inAppBrowserViewController showToolBar:browserOptions.toolbar :browserOptions.toolbarposition];
164+
[self.inAppBrowserViewController showToolBar:browserOptions.navigationbar ? NO : browserOptions.toolbar :browserOptions.toolbarposition];
165165
if (browserOptions.closebuttoncaption != nil) {
166166
[self.inAppBrowserViewController setCloseButtonTitle:browserOptions.closebuttoncaption];
167167
}
@@ -230,9 +230,6 @@ - (void)show:(CDVInvokedUrlCommand*)command
230230

231231
__block CDVInAppBrowserNavigationController* nav = [[CDVInAppBrowserNavigationController alloc]
232232
initWithRootViewController:self.inAppBrowserViewController];
233-
nav.orientationDelegate = self.inAppBrowserViewController;
234-
nav.navigationBarHidden = YES;
235-
nav.modalPresentationStyle = self.inAppBrowserViewController.modalPresentationStyle;
236233

237234
__weak CDVInAppBrowser* weakSelf = self;
238235

@@ -591,7 +588,7 @@ - (void)createViews
591588
self.toolbar.alpha = 1.000;
592589
self.toolbar.autoresizesSubviews = YES;
593590
self.toolbar.autoresizingMask = toolbarIsAtBottom ? (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin) : UIViewAutoresizingFlexibleWidth;
594-
self.toolbar.barStyle = UIBarStyleBlackOpaque;
591+
self.toolbar.barStyle = UIBarStyleBlackOpaque;
595592
self.toolbar.clearsContextBeforeDrawing = NO;
596593
self.toolbar.clipsToBounds = NO;
597594
self.toolbar.contentMode = UIViewContentModeScaleToFill;
@@ -642,7 +639,14 @@ - (void)createViews
642639
self.backButton.enabled = YES;
643640
self.backButton.imageInsets = UIEdgeInsetsZero;
644641

645-
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton, self.backButton, fixedSpaceButton, self.forwardButton]];
642+
if(_browserOptions.navigationbar) {
643+
self.navigationItem.leftBarButtonItem = self.closeButton;
644+
if(_browserOptions.navigationbuttons)
645+
self.navigationItem.rightBarButtonItems = @[self.backButton, fixedSpaceButton, self.forwardButton];
646+
if(_browserOptions.navigationbartitle)
647+
self.navigationItem.title = _browserOptions.navigationbartitle;
648+
} else
649+
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton, self.backButton, fixedSpaceButton, self.forwardButton]];
646650

647651
self.view.backgroundColor = [UIColor grayColor];
648652
[self.view addSubview:self.toolbar];
@@ -664,9 +668,13 @@ - (void)setCloseButtonTitle:(NSString*)title
664668
self.closeButton.enabled = YES;
665669
self.closeButton.tintColor = [UIColor colorWithRed:60.0 / 255.0 green:136.0 / 255.0 blue:230.0 / 255.0 alpha:1];
666670

667-
NSMutableArray* items = [self.toolbar.items mutableCopy];
668-
[items replaceObjectAtIndex:0 withObject:self.closeButton];
669-
[self.toolbar setItems:items];
671+
if(_browserOptions.navigationbar) {
672+
self.navigationItem.leftBarButtonItem = self.closeButton;
673+
} else {
674+
NSMutableArray* items = [self.toolbar.items mutableCopy];
675+
[items replaceObjectAtIndex:0 withObject:self.closeButton];
676+
[self.toolbar setItems:items];
677+
}
670678
}
671679

672680
- (void)showLocationBar:(BOOL)show
@@ -985,6 +993,7 @@ - (id)init
985993
self.toolbar = YES;
986994
self.closebuttoncaption = nil;
987995
self.toolbarposition = kInAppBrowserToolbarBarPositionBottom;
996+
self.navigationbar = NO;
988997
self.clearcache = NO;
989998
self.clearsessioncache = NO;
990999

@@ -1041,6 +1050,19 @@ + (CDVInAppBrowserOptions*)parseOptions:(NSString*)options
10411050

10421051
@implementation CDVInAppBrowserNavigationController : UINavigationController
10431052

1053+
-(instancetype)initWithRootViewController:(UIViewController *)rootViewController {
1054+
if(self = [super initWithRootViewController:rootViewController]) {
1055+
if([rootViewController isMemberOfClass:[CDVInAppBrowserViewController class]]) {
1056+
CDVInAppBrowserViewController* browserVC = (CDVInAppBrowserViewController*)rootViewController;
1057+
self.orientationDelegate = browserVC;
1058+
self.navigationBarHidden = !browserVC.navigationItem.leftBarButtonItem;
1059+
self.modalPresentationStyle = browserVC.modalPresentationStyle;
1060+
}
1061+
}
1062+
1063+
return self;
1064+
}
1065+
10441066
- (void) dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion {
10451067
if ( self.presentedViewController) {
10461068
[super dismissViewControllerAnimated:flag completion:completion];
@@ -1053,10 +1075,12 @@ - (void) viewDidLoad {
10531075
statusBarFrame.size.height = STATUSBAR_HEIGHT;
10541076
// simplified from: http://stackoverflow.com/a/25669695/219684
10551077

1056-
UIToolbar* bgToolbar = [[UIToolbar alloc] initWithFrame:statusBarFrame];
1057-
bgToolbar.barStyle = UIBarStyleDefault;
1058-
[bgToolbar setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
1059-
[self.view addSubview:bgToolbar];
1078+
if(self.navigationBarHidden) {
1079+
UIToolbar* bgToolbar = [[UIToolbar alloc] initWithFrame:statusBarFrame];
1080+
bgToolbar.barStyle = UIBarStyleDefault;
1081+
[bgToolbar setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
1082+
[self.view addSubview:bgToolbar];
1083+
}
10601084

10611085
[super viewDidLoad];
10621086
}

0 commit comments

Comments
 (0)