2

In order to allow for Vimeo video embeds I have "OpenAllWhitelistURLsInWebView" = "yes". But since I did that it only opens whitelisted items and opens them up in webview obviously. I need all non whitelisted items to open up in the safari browser, not webview. Any thoughts on how to accomplish this?

Cordova 1.7 | XCode 4.3.2 | Jquery 1.7.1 | JqueryMobile 1.1.0 | ios 5.1

nate8684
  • 539
  • 1
  • 9
  • 27
  • This guy has it almost exactly as I need it: http://stackoverflow.com/a/9748173/1193081. Now I just need to understand how to format the conditional to say "if [url scheme] === '*vimeo.com*'" ... any thoughts? – nate8684 Jun 04 '12 at 19:28
  • You don't want to test the url scheme. You want to test the full URL for vimeo so you use rangeOfString, similar to what I posted. I typed that code in since I don't have access to my Mac so there might be some syntax errors to fix :-) – codemonkey Jun 06 '12 at 14:28
  • This post answers your question http://stackoverflow.com/questions/11035059/cordova-phonegap-and-iframes-desperate – dhaval Jun 15 '12 at 20:35

3 Answers3

2

I don't know exactly the difference with Cordovoa but I'm working with PG 1.4.1 and I have this settings in my PhoneGap.plist

enter image description here

And this in my AppDelegate.m

- (BOOL) webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *url = [request URL];

    if([[url absoluteString] rangeOfString:@"vimeo.com"].length > 0 || [[url scheme] isEqualToString:@"file"]){
        return [self.viewController webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
    }

    [[UIApplication sharedApplication] openURL:url];
    return NO;
}

This is my pretty simple index.html opened by PG

<body>
   <a href="http://www.vimeo.com">Vimeo</a>
   <a href="http://www.google.com">Google</a>
</body>

The vimeo link is opened within the webview and the google link is opened in Safari.

UPDATE Cordova 1.7

Apparently, the shouldSTartLoadWithRequest function is not called in latests versions of PhoneGap/Cordova (from 1.6.1 I think). So, now if you want to open a link within Safari, you need to set the target attribute a the a tag to _blank. Since you don't always have access to the code, here's a script to help.

<head>
    <script type="text/javascript" src="cordova-1.7.0.js"></script>
    <script>  
        document.onclick = checkLink;
        function checkLink(e) {
            var url = e.target.href;
            if(url.indexOf('vimeo.com') == -1){
                window.open(url,'_blank');
            }
        }
    </script>
</head>
<body>
    <a href="http://www.vimeo.com">Vimeo</a>
    <a href="http://www.google.com" target="_blank">Google</a>
</body>
Titouan de Bailleul
  • 12,920
  • 11
  • 66
  • 121
  • With cordova 1.7, adding that code near the bottom of AppDelegate.m doesn't do anything. Putting a breakpoint in the method indicates it's not getting called. – Leopd Jun 04 '12 at 22:47
  • I don't think the `_blank` change has been implemented yet for iOS. https://issues.apache.org/jira/browse/CB-362 says it's punted to version 2.0. – Leopd Jun 05 '12 at 00:38
  • I've tried it on the Simulator 5.1 it opens google.com in Safari and Vimeo within the app. – Titouan de Bailleul Jun 05 '12 at 07:56
  • For cordova 1.7 do I need to put the script in the html AS WELL as the script in the appdelegate...or can I skip the app delegate? Thanks for the help! – nate8684 Jun 05 '12 at 13:11
  • No the AppDelegate script is useless – Titouan de Bailleul Jun 05 '12 at 13:22
  • 1
    Hey, I got the links to work, but this doesn't work for iFrames. The iframes kick me out of the app to the actual video. ... thoughts on how I can correct this action, almost there. Thanks! – nate8684 Jun 05 '12 at 13:48
  • Could you be more explicit on how you uses iframes. Because there might probably a better solution. – Titouan de Bailleul Jun 05 '12 at 17:54
  • Sure, I use iframes to embed vimeo videos. Essentially I have each link hyperlinked to a hidden page that has the vimeo iframe. Once clicked that hidden page is shown and has the thumbnail of the video (iframe). When you click the video webview is launched. – nate8684 Jun 05 '12 at 19:33
  • Did you try to embed the vimeo video with tags ? http://www.miracletutorials.com/howto-embed-vimeo/ – Titouan de Bailleul Jun 05 '12 at 19:43
  • I really can't as I have a ton of videos that are being pulled from a CMS Database. Editing those videos would be a HUGE job... – nate8684 Jun 05 '12 at 20:10
  • Any further thoughts on this one? – nate8684 Jun 06 '12 at 14:26
0

It should already do that but since you've posted here I guess it doesn't.

What you can do is override the shouldStartLoadWithRequest method in your AppDelegate.m file. You can add a condition you would like to check for (such as the URL containing vimeo) and return true, else return false.

    if ( [request.URL.absoluteString rangeOfString:@"vimeo.com"].location != NSNotFound) {
            return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}

    //open in Safari
    [[UIApplication sharedApplication]openURL:request.URL];
    return NO;
codemonkey
  • 5,257
  • 2
  • 18
  • 16
  • That code seems to be giving me some errors. I just put it in the end of my appdelegate.m. It gives me the following error twice: "Expected identifier or '('" ... thoughts? – nate8684 Jun 01 '12 at 16:15
0

So I'm using Cordova 1.9. After debugging a little, I see that the function shouldStartLoadWithRequest should now be implemented in the MainViewController.m file not in the AppDelegate.m file. This is why it is never triggered.

Discovered this after updating from PhoneGap 1.4.1 to Cordova 1.9.

Kristian
  • 2,071
  • 23
  • 15