diff --git a/README.md b/README.md index a16f6b9..4aea983 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,13 @@ function openUrl(url, readerMode) { }, function(msg) { alert("KO: " + msg); - }) + }); + + SafariViewController.addEventListener("exit", function onExit() { + console.log("Browser was dismissed"); + + SafariViewController.removeEventListener("exit", onExit); + }); } else { // potentially powered by InAppBrowser because that (currently) clobbers window.open window.open(url, '_blank', 'location=yes'); diff --git a/demo/index.html b/demo/index.html index fd16bae..28e49e1 100644 --- a/demo/index.html +++ b/demo/index.html @@ -24,6 +24,10 @@

Safari VC





+ +

Events +
+ @@ -52,6 +56,25 @@

Safari VC

function dismissSafari() { SafariViewController.hide() } + + function addEventAlert(eventName, alertMessage) { + SafariViewController.addEventListener(eventName, function() { + alert(alertMessage); + }); + } + + function removeEvent(eventName) { + SafariViewController.removeEventListener(eventName); + } + + function toggleEventAlert(on, eventName, alertMessage) { + if (on) { + addEventAlert(eventname, alertMessage); + } + else { + removeEvent(eventName); + } + } \ No newline at end of file diff --git a/src/ios/SafariViewController.m b/src/ios/SafariViewController.m index 5f35856..b0db545 100644 --- a/src/ios/SafariViewController.m +++ b/src/ios/SafariViewController.m @@ -3,6 +3,7 @@ @implementation SafariViewController { SFSafariViewController *vc; + NSString *callbackId; } - (void) isAvailable:(CDVInvokedUrlCommand*)command { @@ -21,19 +22,36 @@ - (void) show:(CDVInvokedUrlCommand*)command { } NSURL *url = [NSURL URLWithString:urlString]; bool readerMode = [[options objectForKey:@"enterReaderModeIfAvailable"] isEqualToNumber:[NSNumber numberWithBool:YES]]; + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; vc = [[SFSafariViewController alloc] initWithURL:url entersReaderIfAvailable:readerMode]; vc.delegate = self; [self.viewController presentViewController:vc animated:YES completion:nil]; - [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] callbackId:command.callbackId]; + + callbackId = command.callbackId; + [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]]; + + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } - (void) hide:(CDVInvokedUrlCommand*)command { if (vc != nil) { [vc dismissViewControllerAnimated:YES completion:nil]; - vc = nil; } [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] callbackId:command.callbackId]; + + // -safariViewControllerDidFinish: is not called when the controller is + // hidden programmatically + [self didDismissSafariViewController]; +} + +- (void) didDismissSafariViewController { + if (callbackId != nil) { + [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:@{ @"type": @"exit" }] callbackId:callbackId]; + callbackId = nil; + } + + vc = nil; } # pragma mark - SFSafariViewControllerDelegate @@ -42,7 +60,7 @@ - (void) hide:(CDVInvokedUrlCommand*)command { Upon this call, the view controller is dismissed modally. */ - (void)safariViewControllerDidFinish:(SFSafariViewController *)controller { - // could emit event to JS, but don't see the usecase yet - perhaps check InAppBrowser impl + [self didDismissSafariViewController]; } /*! @abstract Invoked when the initial URL load is complete. diff --git a/www/SafariViewController.js b/www/SafariViewController.js index a72f252..1f69a03 100644 --- a/www/SafariViewController.js +++ b/www/SafariViewController.js @@ -1,12 +1,42 @@ var exec = require("cordova/exec"); +var channel = require("cordova/channel"); + +var channels = { + exit: channel.create("exit") +}; + +function eventHandler(event) { + if (event && (event.type in channels)) { + channels[event.type].fire(event); + } +} + module.exports = { isAvailable: function (onSuccess, onError) { exec(onSuccess, onError, "SafariViewController", "isAvailable", []); }, show: function (options, onSuccess, onError) { - exec(onSuccess, onError, "SafariViewController", "show", [options]); + // This callback remains open for event handling; call onSuccess only once + // then await events. + exec(function(event) { + if (onSuccess) { + onSuccess.apply(null, arguments); + onSuccess = null; + } + eventHandler(event); + }, onError, "SafariViewController", "show", [options]); }, hide: function (onSuccess, onError) { exec(onSuccess, onError, "SafariViewController", "hide", []); + }, + addEventListener: function (eventname,f) { + if (eventname in channels) { + channels[eventname].subscribe(f); + } + }, + removeEventListener: function(eventname, f) { + if (eventname in channels) { + channels[eventname].unsubscribe(f); + } } }; \ No newline at end of file