Skip to content

Commit 4a0d57c

Browse files
committed
Present the SafariViewController modally or as push instead
1 parent 3c23fcd commit 4a0d57c

File tree

5 files changed

+29
-15
lines changed

5 files changed

+29
-15
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ Property | Description
9797
`animated` (Boolean) | Animate the presentation. [`true`/`false`]
9898
`modalPresentationStyle` (String) | The presentation style for modally presented view controllers. [`none`/`fullScreen`/`pageSheet`/`formSheet`/`currentContext`/`custom`/`overFullScreen`/`overCurrentContext`/`popover`]
9999
`modalTransitionStyle` (String) | The transition style to use when presenting the view controller. [`coverVertical`/`flipHorizontal`/`crossDissolve`/`partialCurl`]
100+
`modalEnabled` (Boolean) | Present the **SafariViewController** modally or as push instead. [`true`/`false`]
100101
101102
### Android Options
102103
Property | Description

example/App.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export default class App extends Component {
5454
animated: true,
5555
modalPresentationStyle: 'fullScreen',
5656
modalTransitionStyle: 'partialCurl',
57+
modalEnabled: true,
5758
// Android Properties
5859
showTitle: true,
5960
toolbarColor: '#6200EE',

index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ declare module 'react-native-inappbrowser-reborn' {
3232
| 'coverVertical'
3333
| 'flipHorizontal'
3434
| 'crossDissolve'
35-
| 'partialCurl'
35+
| 'partialCurl',
36+
modalEnabled?: boolean
3637
}
3738

3839
type InAppBrowserAndroidOptions = {

index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ type InAppBrowseriOSOptions = {
3838
| 'coverVertical'
3939
| 'flipHorizontal'
4040
| 'crossDissolve'
41-
| 'partialCurl'
41+
| 'partialCurl',
42+
modalEnabled?: boolean
4243
};
4344

4445
type InAppBrowserAndroidOptions = {
@@ -63,12 +64,15 @@ async function open(
6364
url: string,
6465
options: InAppBrowserOptions = {}
6566
): Promise<BrowserResult> {
67+
const modalEnabled =
68+
options.modalEnabled !== undefined ? options.modalEnabled : true;
6669
const inAppBrowseroptions = {
6770
...options,
6871
url,
6972
dismissButtonStyle: options.dismissButtonStyle || 'close',
7073
readerMode: options.readerMode !== undefined ? options.readerMode : false,
71-
animated: options.animated !== undefined ? options.animated : true
74+
animated: options.animated !== undefined ? options.animated : true,
75+
modalEnabled
7276
};
7377
if (inAppBrowseroptions.preferredBarTintColor) {
7478
inAppBrowseroptions.preferredBarTintColor = processColor(

ios/RNInAppBrowser.m

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ - (dispatch_queue_t)methodQueue
113113
self.animated = [options[@"animated"] boolValue];
114114
NSString* modalPresentationStyle = [options valueForKey:@"modalPresentationStyle"];
115115
NSString* modalTransitionStyle = [options valueForKey:@"modalTransitionStyle"];
116+
BOOL modalEnabled = [options[@"modalEnabled"] boolValue];
116117

117118
// Safari View Controller to authorize request
118119
NSURL *url = [[NSURL alloc] initWithString:authURL];
@@ -138,15 +139,21 @@ - (dispatch_queue_t)methodQueue
138139
}
139140
}
140141

141-
safariVC.modalPresentationStyle = [self modalPresentationStyleWithString: modalPresentationStyle];
142-
safariVC.modalTransitionStyle = [self modalTransitionStyleWithString: modalTransitionStyle];
143-
144-
// This is a hack to present the SafariViewController modally
145-
UINavigationController *safariHackVC = [[UINavigationController alloc] initWithRootViewController:safariVC];
146-
[safariHackVC setNavigationBarHidden:true animated:false];
142+
if(self.animated) {
143+
safariVC.modalPresentationStyle = [self getPresentationStyle: modalPresentationStyle];
144+
safariVC.modalTransitionStyle = [self getTransitionStyle: modalTransitionStyle];
145+
}
147146

148147
UIViewController *ctrl = RCTPresentedViewController();
149-
[ctrl presentViewController:safariHackVC animated:[self animated] completion:nil];
148+
if (modalEnabled) {
149+
// This is a hack to present the SafariViewController modally
150+
UINavigationController *safariHackVC = [[UINavigationController alloc] initWithRootViewController:safariVC];
151+
[safariHackVC setNavigationBarHidden:true animated:false];
152+
[ctrl presentViewController:safariHackVC animated:self.animated completion:nil];
153+
}
154+
else {
155+
[ctrl presentViewController:safariVC animated:self.animated completion:nil];
156+
}
150157
}
151158

152159
- (void)performSynchronouslyOnMainThread:(void (^)(void))block
@@ -163,7 +170,7 @@ - (void)_close
163170
__weak typeof(self) weakSelf = self;
164171
[self performSynchronouslyOnMainThread:^{
165172
UIViewController *ctrl = RCTPresentedViewController();
166-
[ctrl dismissViewControllerAnimated:[weakSelf animated] completion:^{
173+
[ctrl dismissViewControllerAnimated:weakSelf.animated completion:^{
167174
__strong typeof(self) strongSelf = weakSelf;
168175
if (strongSelf) {
169176
strongSelf.redirectResolve(@{
@@ -235,7 +242,7 @@ -(void)flowDidFinish
235242
_redirectReject = nil;
236243
}
237244

238-
- (UIModalPresentationStyle)modalPresentationStyleWithString:(NSString *)modalPresentationStyleString {
245+
- (UIModalPresentationStyle)getPresentationStyle:(NSString *)styleKey {
239246
NSDictionary *styles = @{
240247
@"fullScreen": @(UIModalPresentationFullScreen),
241248
@"pageSheet": @(UIModalPresentationPageSheet),
@@ -247,21 +254,21 @@ - (UIModalPresentationStyle)modalPresentationStyleWithString:(NSString *)modalPr
247254
@"popover": @(UIModalPresentationPopover)
248255
};
249256
UIModalPresentationStyle modalPresentationStyle = UIModalPresentationNone;
250-
NSNumber *style = [styles objectForKey: modalPresentationStyleString];
257+
NSNumber *style = [styles objectForKey: styleKey];
251258
if (style != nil) {
252259
modalPresentationStyle = [style intValue];
253260
}
254261
return modalPresentationStyle;
255262
}
256263

257-
- (UIModalTransitionStyle)modalTransitionStyleWithString:(NSString *)modalTransitionStyleString {
264+
- (UIModalTransitionStyle)getTransitionStyle:(NSString *)styleKey {
258265
NSDictionary *styles = @{
259266
@"flipHorizontal": @(UIModalTransitionStyleFlipHorizontal),
260267
@"crossDissolve": @(UIModalTransitionStyleCrossDissolve),
261268
@"partialCurl": @(UIModalTransitionStylePartialCurl)
262269
};
263270
UIModalTransitionStyle modalTransitionStyle = UIModalTransitionStyleCoverVertical;
264-
NSNumber *style = [styles objectForKey: modalTransitionStyleString];
271+
NSNumber *style = [styles objectForKey: styleKey];
265272
if (style != nil) {
266273
modalTransitionStyle = [style intValue];
267274
}

0 commit comments

Comments
 (0)