UNPKG

@akylas/nativescript-inappbrowser

Version:
220 lines 10.8 kB
import { Utils } from '@nativescript/core'; import { parseColor, log } from './utils.common'; import { getDefaultOptions, BROWSER_TYPES, DISMISS_BUTTON_STYLES, InAppBrowserErrorMessage, } from './InAppBrowser.common'; import { getTransitionStyle, getPresentationStyle, setModalInPresentation, dismissWithoutAnimation, InAppBrowserOpenAuthErrorMessage, } from './utils.ios'; const DEFAULT_PROTOCOLS = [ SFSafariViewControllerDelegate, UIAdaptivePresentationControllerDelegate ]; const protocols = Utils.ios.MajorVersion >= 13 ? [ ...DEFAULT_PROTOCOLS, ASWebAuthenticationPresentationContextProviding ] : DEFAULT_PROTOCOLS; let InAppBrowserModuleInstance; function setup() { var InAppBrowserModule = /** @class */ (function (_super) { __extends(InAppBrowserModule, _super); function InAppBrowserModule() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.safariVC = null; _this.redirectResolve = null; _this.redirectReject = null; _this.authSession = null; _this.animated = false; return _this; } InAppBrowserModule.prototype.isAvailable = function () { return Promise.resolve(Utils.ios.MajorVersion >= 9); }; InAppBrowserModule.prototype.initializeWebBrowser = function (resolve, reject) { if (this.redirectReject) { this.redirectReject(new Error(InAppBrowserErrorMessage)); return false; } this.redirectResolve = resolve; this.redirectReject = reject; return true; }; InAppBrowserModule.prototype.open = function (authURL, options) { var _this = this; return new Promise(function (resolve, reject) { if (!_this.initializeWebBrowser(resolve, reject)) return; var inAppBrowserOptions = getDefaultOptions(authURL, options); _this.animated = inAppBrowserOptions.animated; try { // Safari View Controller to authorize request var url = NSURL.URLWithString(inAppBrowserOptions.url); if (Utils.ios.MajorVersion >= 11) { var config = SFSafariViewControllerConfiguration.alloc().init(); config.barCollapsingEnabled = inAppBrowserOptions.enableBarCollapsing; config.entersReaderIfAvailable = inAppBrowserOptions.readerMode; _this.safariVC = SFSafariViewController.alloc().initWithURLConfiguration(url, config); } else { _this.safariVC = SFSafariViewController.alloc().initWithURLEntersReaderIfAvailable(url, inAppBrowserOptions.readerMode); } } catch (error) { reject(new Error("Unable to open url.")); _this.flowDidFinish(); log("InAppBrowser: " + error); return; } _this.safariVC.delegate = _this; if (Utils.ios.MajorVersion >= 11) { if (inAppBrowserOptions.dismissButtonStyle === DISMISS_BUTTON_STYLES.DONE) { _this.safariVC.dismissButtonStyle = SFSafariViewControllerDismissButtonStyle.Done; } else if (inAppBrowserOptions.dismissButtonStyle === DISMISS_BUTTON_STYLES.CLOSE) { _this.safariVC.dismissButtonStyle = SFSafariViewControllerDismissButtonStyle.Close; } else if (inAppBrowserOptions.dismissButtonStyle === DISMISS_BUTTON_STYLES.CANCEL) { _this.safariVC.dismissButtonStyle = SFSafariViewControllerDismissButtonStyle.Cancel; } } if (Utils.ios.MajorVersion >= 10) { if (inAppBrowserOptions.preferredBarTintColor) { var color = parseColor(inAppBrowserOptions.preferredBarTintColor); if (color) { _this.safariVC.preferredBarTintColor = color.ios; } } if (inAppBrowserOptions.preferredControlTintColor) { var color = parseColor(inAppBrowserOptions.preferredControlTintColor); if (color) { _this.safariVC.preferredControlTintColor = color.ios; } } } var ctrl = UIApplication.sharedApplication.keyWindow.rootViewController; if (inAppBrowserOptions.modalEnabled) { // This is a hack to present the SafariViewController modally var safariHackVC = UINavigationController.alloc().initWithRootViewController(_this.safariVC); safariHackVC.setNavigationBarHiddenAnimated(true, false); // To disable "Swipe to dismiss" gesture which sometimes causes a bug where `safariViewControllerDidFinish` // is not called. _this.safariVC.modalPresentationStyle = UIModalPresentationStyle.OverFullScreen; safariHackVC.modalPresentationStyle = getPresentationStyle(inAppBrowserOptions.modalPresentationStyle); if (_this.animated) { safariHackVC.modalTransitionStyle = getTransitionStyle(inAppBrowserOptions.modalTransitionStyle); } if (Utils.ios.MajorVersion >= 13) { safariHackVC.modalInPresentation = true; if (safariHackVC[setModalInPresentation]) safariHackVC[setModalInPresentation](true); } safariHackVC.presentationController.delegate = _this; ctrl.presentViewControllerAnimatedCompletion(safariHackVC, inAppBrowserOptions.animated, null); } else { ctrl.presentViewControllerAnimatedCompletion(_this.safariVC, inAppBrowserOptions.animated, null); } }); }; InAppBrowserModule.prototype.close = function () { var _this = this; var ctrl = UIApplication.sharedApplication.keyWindow.rootViewController; ctrl.dismissViewControllerAnimatedCompletion(this.animated, function () { if (_this.redirectResolve) { _this.redirectResolve({ type: 'dismiss' }); _this.flowDidFinish(); } }); }; InAppBrowserModule.prototype.openAuth = function (authUrl, redirectUrl, options) { if (options === void 0) { options = {}; } return __awaiter(this, void 0, Promise, function () { var inAppBrowserOptions, response; var _this = this; return __generator(this, function (_a) { inAppBrowserOptions = __assign(__assign({}, options), { ephemeralWebSession: options.ephemeralWebSession !== undefined ? options.ephemeralWebSession : false }); if (Utils.ios.MajorVersion >= 11) { return [2 /*return*/, new Promise(function (resolve, reject) { if (!_this.initializeWebBrowser(resolve, reject)) return; var url = NSURL.URLWithString(authUrl); var escapedRedirectURL = NSURL.URLWithString(redirectUrl).scheme; _this.authSession = (Utils.ios.MajorVersion >= 12 ? ASWebAuthenticationSession : SFAuthenticationSession).alloc().initWithURLCallbackURLSchemeCompletionHandler(url, escapedRedirectURL, function (callbackURL, error) { if (_this.redirectResolve) { if (!error) { _this.redirectResolve({ type: BROWSER_TYPES.SUCCESS, url: callbackURL.absoluteString }); } else { _this.redirectResolve({ type: BROWSER_TYPES.CANCEL }); } _this.flowDidFinish(); } }); if (Utils.ios.MajorVersion >= 13) { var webAuthSession = _this.authSession; // Prevent re-use cookie from last auth session webAuthSession.prefersEphemeralWebBrowserSession = inAppBrowserOptions.ephemeralWebSession; webAuthSession.presentationContextProvider = _this; } _this.authSession.start(); })]; } else { this.flowDidFinish(); response = { type: BROWSER_TYPES.CANCEL, message: InAppBrowserOpenAuthErrorMessage }; return [2 /*return*/, Promise.resolve(response)]; } return [2 /*return*/]; }); }); }; InAppBrowserModule.prototype.closeAuth = function () { if (Utils.ios.MajorVersion >= 11) { var authSession = this.authSession; authSession.cancel(); if (this.redirectResolve) { this.redirectResolve({ type: BROWSER_TYPES.DISMISS }); this.flowDidFinish(); } } else { this.close(); } }; InAppBrowserModule.prototype.presentationAnchorForWebAuthenticationSession = function (_) { return UIApplication.sharedApplication.keyWindow; }; InAppBrowserModule.prototype.safariViewControllerDidFinish = function (controller) { if (this.redirectResolve) { this.redirectResolve({ type: BROWSER_TYPES.CANCEL }); } this.flowDidFinish(); if (!this.animated) { dismissWithoutAnimation(controller); } }; InAppBrowserModule.prototype.flowDidFinish = function () { this.safariVC = null; this.redirectResolve = null; this.redirectReject = null; }; InAppBrowserModule.ObjCProtocols = protocols; return InAppBrowserModule; }(NSObject)); return InAppBrowserModule.new(); } if (typeof InAppBrowserModuleInstance === 'undefined') { InAppBrowserModuleInstance = setup(); } export const InAppBrowser = InAppBrowserModuleInstance; //# sourceMappingURL=InAppBrowser.ios.js.map