Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
23 changes: 23 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ <h1>Safari VC</h1>
<button onclick="openUrl('https://en.m.wikipedia.org/wiki/Safari', false)">Open Wikipedia</button><br/><br/>
<button onclick="openUrl('https://en.m.wikipedia.org/wiki/Safari', true)">Open Wikipedia in reader mode</button><br/><br/>
<button onclick="openUrl('https://en.m.wikipedia.org/wiki/Safari', false);setTimeout(dismissSafari, 10000)">Open Wikipedia, auto-dismiss after 10s</button>

<p>Events</o>
<br/>
<input type="checkbox" name="exit" onchange="toggleEventAlert(this.checked, this.name, 'Browser was dismissed')">
</div>
</div>
<script type="text/javascript" src="cordova.js"></script>
Expand Down Expand Up @@ -52,6 +56,25 @@ <h1>Safari VC</h1>
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);
}
}
</script>
</body>
</html>
24 changes: 21 additions & 3 deletions src/ios/SafariViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
@implementation SafariViewController
{
SFSafariViewController *vc;
NSString *callbackId;
}

- (void) isAvailable:(CDVInvokedUrlCommand*)command {
Expand All @@ -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
Expand All @@ -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.
Expand Down
32 changes: 31 additions & 1 deletion www/SafariViewController.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
};