Skip to content

Instantly share code, notes, and snippets.

@fallanic
Created August 4, 2012 00:17
Show Gist options
  • Save fallanic/3252986 to your computer and use it in GitHub Desktop.
Save fallanic/3252986 to your computer and use it in GitHub Desktop.
Cordova Lib customizations

To open a remote URL at application startup with phoneGap 2.0 :

after project creation, in XCode expand "CordovaLib.xcodeproj" and open "Classes/Cleaver/CDVViewController.m" then edit the code : replace

NSString* startFilePath = [self pathForResource:self.startPage];

by

NSString* startFilePath = self.startPage;

and replace

appURL = [NSURL fileURLWithPath:startFilePath];

by

appURL = [NSURL URLWithString:startFilePath];

You can now open any URL directly from AppDelegate.m

self.viewController.startPage = @"http://www.foodtree.com/m/certifiedhumane";

Mechanism to open some URLs in safari instead of the WebView

In CDVViewController.m

else if ([self.whitelist schemeIsAllowed:[url scheme]])
{
    
    NSDictionary *openInSafariSetting = [self.settings objectForKey:@"OpenInSafariList"];
    // iterate through settings OpenInSafariList, check for equality
    NSEnumerator* enumerator = [openInSafariSetting keyEnumerator];
    
    NSString* urlToOpenInSafari;
    while ( urlToOpenInSafari = [enumerator nextObject])
    {            
        if ([[url host] isEqualToString:urlToOpenInSafari]  ) {
            NSLog(@"URL found in OpenInSafariList: opening in SAFARI");
            [[UIApplication sharedApplication] openURL:url];
            return NO;
        }        
    }
    if ([self.whitelist URLIsAllowed:url] == YES)
    {
        NSNumber *openAllInWhitelistSetting = [self.settings objectForKey:@"OpenAllWhitelistURLsInWebView"];
        if ((nil != openAllInWhitelistSetting) && [openAllInWhitelistSetting boolValue]) {
            
            NSLog(@"OpenAllWhitelistURLsInWebView set: opening in webview");
            return YES;
        }
		
        // mainDocument will be nil for an iFrame
        NSString* mainDocument = [theWebView.request.mainDocumentURL absoluteString];
		
        // anchor target="_blank" - load in Mobile Safari
        if (navigationType == UIWebViewNavigationTypeOther && mainDocument != nil)
        {
            [[UIApplication sharedApplication] openURL:url];
            return NO;
        }
        // other anchor target - load in Cordova webView
        else
        {
            return YES;
        }
    }
    
    return NO;
}

In Cordova.plist set/create these params

OpenAllWhitelistURLsInWebview    Boolean           YES
OpenInSafariList                 Dictionnary
    maps.google.com              String
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment