UNPKG

nativescript-advanced-webview

Version:

An advanced webview using Chrome CustomTabs on Android and SFSafariViewController on iOS.

78 lines (77 loc) 3.02 kB
import { Color, Observable } from '@nativescript/core'; import { getRootView } from '@nativescript/core/application'; import { AdvancedWebviewEvents } from './interfaces'; export { AdvancedWebviewEvents } from './interfaces'; export const NSAdvancedWebViewEventEmitter = new Observable(); let ctrl; let delegate; var SFSafariViewControllerDelegateImpl = /** @class */ (function (_super) { __extends(SFSafariViewControllerDelegateImpl, _super); function SFSafariViewControllerDelegateImpl() { return _super !== null && _super.apply(this, arguments) || this; } SFSafariViewControllerDelegateImpl.initWithOwnerCallback = function (owner, callback) { var delegate = (SFSafariViewControllerDelegateImpl.new()); delegate._owner = owner; delegate._callback = callback; return delegate; }; /** * Tells the delegate that the initial URL load completed. * @param controller * @param didLoadSuccessfully */ SFSafariViewControllerDelegateImpl.prototype.safariViewControllerDidCompleteInitialLoad = function (controller, didLoadSuccessfully) { if (didLoadSuccessfully === true) { NSAdvancedWebViewEventEmitter.notify({ eventName: AdvancedWebviewEvents.LoadFinished }); } else { NSAdvancedWebViewEventEmitter.notify({ eventName: AdvancedWebviewEvents.LoadError }); } }; /** * Tells the delegate that the user dismissed the view. */ SFSafariViewControllerDelegateImpl.prototype.safariViewControllerDidFinish = function (controller) { NSAdvancedWebViewEventEmitter.notify({ eventName: AdvancedWebviewEvents.Closed }); delegate = null; }; SFSafariViewControllerDelegateImpl.ObjCProtocols = [SFSafariViewControllerDelegate]; return SFSafariViewControllerDelegateImpl; }(NSObject)); export function init() { console.log('init not needed on iOS.'); } export function openAdvancedUrl(options) { if (!options.url) { throw new Error('No url set in the Advanced WebView Options object.'); } const sfc = SFSafariViewController.alloc().initWithURL(NSURL.URLWithString(options.url)); if (options.toolbarColor) { sfc.preferredBarTintColor = new Color(options.toolbarColor).ios; } if (options.toolbarControlsColor) { sfc.preferredControlTintColor = new Color(options.toolbarControlsColor).ios; } delegate = SFSafariViewControllerDelegateImpl.initWithOwnerCallback(new WeakRef({}), null); sfc.delegate = delegate; ctrl = getRootView().viewController; if (options.ios?.viewController) { ctrl = options.ios.viewController; } ctrl.presentViewControllerAnimatedCompletion(sfc, true, null); NSAdvancedWebViewEventEmitter.notify({ eventName: AdvancedWebviewEvents.LoadStarted }); } export function close() { if (ctrl) { ctrl.dismissModalViewControllerAnimated(true); } }