diff --git a/SafariViewManager.android.js b/SafariViewManager.android.js index 833d367..1c2b751 100644 --- a/SafariViewManager.android.js +++ b/SafariViewManager.android.js @@ -5,21 +5,8 @@ */ 'use strict'; -const {NativeModules} = require('react-native'); -const NativeSafariViewManager = NativeModules.SafariViewManager; - -var SafariViewManager = { - isAvailable: function() { - return new Promise(function(resolve, reject) { - NativeSafariViewManager.isAvailable(function(error) { - if (error) { - return reject(error); - } - - resolve(false); - }); - }); +export default { + isAvailable() { + return Promise.reject(new Error('SafariView is unavailable on Android')); } }; - -module.exports = SafariViewManager; diff --git a/SafariViewManager.ios.js b/SafariViewManager.ios.js index cb0e5a9..d2c0042 100644 --- a/SafariViewManager.ios.js +++ b/SafariViewManager.ios.js @@ -20,15 +20,7 @@ export default { options.barTintColor = processColor(options.barTintColor); } - return new Promise((resolve, reject) => { - NativeSafariViewManager.show(options, (error) => { - if (error) { - return reject(error); - } - - resolve(true); - }); - }); + return NativeSafariViewManager.show(options); }, dismiss() { @@ -36,15 +28,7 @@ export default { }, isAvailable() { - return new Promise((resolve, reject) => { - NativeSafariViewManager.isAvailable((error) => { - if (error) { - return reject(error); - } - - resolve(true); - }); - }); + return NativeSafariViewManager.isAvailable(); }, addEventListener(event, listener) { diff --git a/SafariViewManager.m b/SafariViewManager.m index 2716802..0bb7c0c 100644 --- a/SafariViewManager.m +++ b/SafariViewManager.m @@ -31,20 +31,22 @@ - (void)stopObserving return @[@"SafariViewOnShow", @"SafariViewOnDismiss"]; } -RCT_EXPORT_METHOD(show:(NSDictionary *)args callback:(RCTResponseSenderBlock)callback) +RCT_EXPORT_METHOD(show:(NSDictionary *)args resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { - UIColor *tintColorString = args[@"tintColor"]; - UIColor *barTintColorString = args[@"barTintColor"]; - BOOL fromBottom = [args[@"fromBottom"] boolValue]; - // Error if no url is passed if (!args[@"url"]) { - RCTLogError(@"[SafariView] You must specify a url."); + reject(@"E_SAFARI_VIEW_NO_URL", @"You must specify a url.", nil); return; } + NSURL *url = [NSURL URLWithString:args[@"url"]]; + BOOL readerMode = [args[@"readerMode"] boolValue]; + UIColor *tintColorString = args[@"tintColor"]; + UIColor *barTintColorString = args[@"barTintColor"]; + BOOL fromBottom = [args[@"fromBottom"] boolValue]; + // Initialize the Safari View - self.safariView = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:args[@"url"]] entersReaderIfAvailable:[args[@"readerMode"] boolValue]]; + self.safariView = [[SFSafariViewController alloc] initWithURL:url entersReaderIfAvailable:readerMode]; self.safariView.delegate = self; // Set tintColor if available @@ -83,15 +85,17 @@ - (void)stopObserving if (hasListeners) { [self sendEventWithName:@"SafariViewOnShow" body:nil]; } + + resolve(@YES); } -RCT_EXPORT_METHOD(isAvailable:(RCTResponseSenderBlock)callback) +RCT_EXPORT_METHOD(isAvailable:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { if ([SFSafariViewController class]) { // SafariView is available - callback(@[[NSNull null], @true]); + resolve(@YES); } else { - callback(@[RCTMakeError(@"[SafariView] SafariView is unavailable.", nil, nil)]); + reject(@"E_SAFARI_VIEW_UNAVAILABLE", @"SafariView is unavailable", nil); } }