PIOTR CIESIELSKI
|
MOBILE APPS DEVELOPER, WEB DESIGNER
PIOTR CIESIELSKI
|
MOBILE APPS DEVELOPER, WEB DESIGNER
Cordova and iOS

Cordova and iOS

Apache Cordova (i assume that you use latest 3.5 version) is a nice framework for build hybrid application for most popular mobile operating systems. But with iOS there is one problem if you have links in your app and want it to be opened external in Safari and not in webview itself. When link opens a website inside a webview, user has no way to return to application – is a well known problem with Cordova. So, what to do to open links external in Safari?

At first we must install inappbrowser Cordova plugin:

cordova plugin add org.apache.cordova.inappbrowser

A little suprise when plugin is installed – opening links in Safari still did not work. It’s a Cordova webview bug ofcourse. OK, here we have a solution for that issue. Open your project in Xcode and in file explorer find MainViewController.m file in Classes folder, then insert this code:

/**
 * Start Loading Request
 * This is where most of the magic happens... We take the request(s) and process the response.
 * From here we can re direct links and other protocalls to different internal methods.
 */
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
 NSURL *url = [request URL];
 // add any other schemes you want to support, or perform additional
 // tests on the url before deciding what to do -jm
 if( [[url scheme] isEqualToString:@"http"] ||
 [[url scheme] isEqualToString:@"https"])
 {
 [[UIApplication sharedApplication] openURL:url];
 return NO;
 }
 else 
 { 
 return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
 }
}

right after this comment:

/* Comment out the block below to over-ride */
/*
- (void) webViewDidStartLoad:(UIWebView*)theWebView
{
 return [super webViewDidStartLoad:theWebView];
}
- (void) webView:(UIWebView*)theWebView didFailLoadWithError:(NSError*)error
{
 return [super webView:theWebView didFailLoadWithError:error];
}
- (BOOL) webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
 return [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
}
*/

Save, compile and voila! – magic happens 🙂



About Piotr Ciesielski

For over 25 years I am fascinated by information technologies. Especially the Internet and mobile. For more than 15 years dealing with them professionally. I specialize in mobile technologies, particularly in building applications for smartphones, tablets and other mobile devices, and the design and development of web-based solutions.
  • Thanks for the tip. You saved me several hours of searching why it does not work.

  • I join. Thanks – useful info

  • How do we override webview:shouldLoadWithRequest in cordova 4.4.0? Everything seems to be taken care by CDUIWebview plugins. But, how do I set delegate so that it calls my delegate in my view controller?

    • I don’t exactly understand where is the problem, because you not describe it too detailed, but something tells me that’s a configuration issue. See in documentation here:

      https://cordova.apache.org/docs/en/latest/config_ref/

      Focus on keys „CordovaWebViewEngine” and „CordovaDefaultWebViewEngine”. Especially „CordovaDefaultWebViewEngine” enables you to override the default fallback WebView, as described.

Leave a reply

Your email address will not be published. Website Field Is Optional