cordova-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Li, Jonathan" <>
Subject optimizing ios inappbrowser plugin's open and openInSystem method s
Date Wed, 07 Sep 2016 04:41:45 GMT

Currently there are two issues in iOS inappbrowser’s open and  openInSystem methods.

First, in the inappbrowser open method, it sets the instance variable at below
        self.callbackId = command.callbackId;
this causes the problem when opening a _system targe from an inappbrowser viewController,
even if the callbackID are not applicable to system browser.

Second, the _system target will always be opened in external mobile safari browser, although
SFSafariViewController for iOS 9 device provides a better user experience. The major benefit
with SFSafariViewController is the application staying in foreground, so that the application
does not need to handle the application life cycle events triggered by opening the mobile

The below code is a prototype handling the two issues. Just want to know your comment about
this change, and if it is fine, a pull request will be submitted for it.

- (void)open:(CDVInvokedUrlCommand*)command
    CDVPluginResult* pluginResult;

    NSString* url = [command argumentAtIndex:0];
    NSString* target = [command argumentAtIndex:1 withDefault:kInAppBrowserTargetSelf];
    NSString* options = [command argumentAtIndex:2 withDefault:@"" andClass:[NSString class]];

   //**********the below line causes trouble to open system url from inappbrowser
   //self.callbackId = command.callbackId;

    if (url != nil) {
#ifdef __CORDOVA_4_0_0
        NSURL* baseUrl = [self.webViewEngine URL];
        NSURL* baseUrl = [self.webView.request URL];
        NSURL* absoluteUrl = [[NSURL URLWithString:url relativeToURL:baseUrl] absoluteURL];

        if ([self isSystemUrl:absoluteUrl]) {
            target = kInAppBrowserTargetSystem;

        if ([target isEqualToString:kInAppBrowserTargetSelf]) {
            [self openInCordovaWebView:absoluteUrl withOptions:options];
        } else if ([target isEqualToString:kInAppBrowserTargetSystem]) {
            [self openInSystem:absoluteUrl];
        } else { // _blank or anything else
            [self openInInAppBrowser:absoluteUrl withOptions:options];

        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
    } else {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"incorrect
number of arguments"];

    //***********new code
    if (![target isEqualToString:kInAppBrowserTargetSystem]) {
        [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
        self.callbackId = command.callbackId;

    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];

- (void)openInSystem:(NSURL*)url
    //************ customization: open _system web url in Safari view controller on ios 9
    if (([[[UIDevice currentDevice] systemVersion] compare:@"9.0" options:NSNumericSearch]
!= NSOrderedAscending) &&
          ([[url.scheme lowercaseString] isEqualToString:@"http"] || [[url.scheme lowercaseString]
isEqualToString:@"https"])) {
        SFSafariViewController* sf = [[SFSafariViewController alloc] initWithURL:url];
        sf.delegate = self;
        //check whether inappbrowser is presented, if so, use it to present the safari viewcontroller
        if (self.viewController.presentedViewController != nil){
            [self.viewController.presentedViewController presentViewController:sf animated:YES
            [self.viewController presentViewController:sf animated:YES completion:nil];
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url];
        } else { // handle any custom schemes to plugins
            [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification

- (void)safariViewControllerDidFinish:(SFSafariViewController *)controller{
    [controller dismissViewControllerAnimated:YES completion:nil];


View raw message