forked from droibit/react-native-custom-tabs
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In App Safari View Controller Support.
1- Added SafariViewController support 2- Added dismiss method to close safari. 3- Modified header search path Signed-off-by: Abhishek Singh <[email protected]>
- Loading branch information
1 parent
f3368bf
commit e100210
Showing
8 changed files
with
1,131 additions
and
924 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// SafariViewManager.h | ||
// DBCustomTabs | ||
// | ||
// Created by Avinash on 10/12/2019. | ||
// Copyright © 2019 Facebook. All rights reserved. | ||
// | ||
|
||
#import "RCTEventEmitter.h" | ||
#import <React/RCTBridgeModule.h> | ||
|
||
@import SafariServices; | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface SafariViewManager : RCTEventEmitter<RCTBridgeModule, SFSafariViewControllerDelegate> | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// | ||
// SafariViewManager.m | ||
// DBCustomTabs | ||
// | ||
// Created by Avinash on 10/12/2019. | ||
// Copyright © 2019 Facebook. All rights reserved. | ||
// | ||
|
||
#import "SafariViewManager.h" | ||
#import <React/RCTUtils.h> | ||
#import <React/RCTLog.h> | ||
#import <React/RCTConvert.h> | ||
|
||
@implementation SafariViewManager | ||
{ | ||
bool hasListeners; | ||
SFSafariViewController *_safariView; | ||
} | ||
|
||
RCT_EXPORT_MODULE() | ||
|
||
- (dispatch_queue_t)methodQueue | ||
{ | ||
return dispatch_get_main_queue(); | ||
} | ||
|
||
- (void)startObserving | ||
{ | ||
hasListeners = YES; | ||
} | ||
|
||
- (void)stopObserving | ||
{ | ||
hasListeners = NO; | ||
} | ||
|
||
- (NSArray<NSString *> *)supportedEvents | ||
{ | ||
return @[@"SafariViewOnShow", @"SafariViewOnDismiss"]; | ||
} | ||
|
||
RCT_EXPORT_METHOD(show:(NSDictionary *)args resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) | ||
{ | ||
// Error if no url is passed | ||
if (!args[@"url"]) { | ||
reject(@"E_SAFARI_VIEW_NO_URL", @"You must specify a url.", nil); | ||
return; | ||
} | ||
|
||
NSURL *url = [RCTConvert NSURL:args[@"url"]]; | ||
BOOL readerMode = [args[@"readerMode"] boolValue]; | ||
UIColor *tintColorString = args[@"tintColor"]; | ||
UIColor *barTintColorString = args[@"barTintColor"]; | ||
BOOL fromBottom = [args[@"fromBottom"] boolValue]; | ||
|
||
// Initialize the Safari View | ||
_safariView = [[SFSafariViewController alloc] initWithURL:url entersReaderIfAvailable:readerMode]; | ||
_safariView.delegate = self; | ||
|
||
// Set tintColor if available | ||
if (tintColorString) { | ||
UIColor *tintColor = [RCTConvert UIColor:tintColorString]; | ||
if ([_safariView respondsToSelector:@selector(setPreferredControlTintColor:)]) { | ||
[_safariView setPreferredControlTintColor:tintColor]; | ||
} else { | ||
[_safariView.view setTintColor:tintColor]; | ||
} | ||
} | ||
|
||
// Set barTintColor if available | ||
if (barTintColorString) { | ||
UIColor *barTintColor = [RCTConvert UIColor:barTintColorString]; | ||
if ([_safariView respondsToSelector:@selector(setPreferredBarTintColor:)]) { | ||
[_safariView setPreferredBarTintColor:barTintColor]; | ||
} | ||
} | ||
|
||
// Set modal transition style | ||
if (fromBottom) { | ||
_safariView.modalPresentationStyle = UIModalPresentationOverFullScreen; | ||
} | ||
|
||
// get the view controller closest to the foreground | ||
UIViewController *ctrl = RCTPresentedViewController(); | ||
|
||
// Display the Safari View | ||
[ctrl presentViewController:_safariView animated:YES completion:nil]; | ||
|
||
if (hasListeners) { | ||
[self sendEventWithName:@"SafariViewOnShow" body:nil]; | ||
} | ||
|
||
resolve(@YES); | ||
} | ||
|
||
RCT_EXPORT_METHOD(isAvailable:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) | ||
{ | ||
if (@available(iOS 9.0, *)) { | ||
// SafariView is available | ||
resolve(@YES); | ||
} else { | ||
reject(@"E_SAFARI_VIEW_UNAVAILABLE", @"SafariView is unavailable", nil); | ||
} | ||
} | ||
|
||
RCT_EXPORT_METHOD(dismiss) | ||
{ | ||
[_safariView dismissViewControllerAnimated:true completion:nil]; | ||
} | ||
|
||
-(void)safariViewControllerDidFinish:(nonnull SFSafariViewController *)controller | ||
{ | ||
_safariView = nil; | ||
NSLog(@"[SafariView] SafariView dismissed."); | ||
if (hasListeners) { | ||
[self sendEventWithName:@"SafariViewOnDismiss" body:nil]; | ||
} | ||
} | ||
@end |
Oops, something went wrong.