Skip to content

Instantly share code, notes, and snippets.

@typeoneerror
Last active September 17, 2019 11:58
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save typeoneerror/5097118 to your computer and use it in GitHub Desktop.
Save typeoneerror/5097118 to your computer and use it in GitHub Desktop.
<cordova>
<!-- ... -->
<plugins>
<!-- ... -->
<plugin name="OpenUrl" value="OpenUrl" />
</plugins>
<!-- ... -->
</cordova>
<script type="text/javascript" src="cordova-2.3.0.js"></script>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
document.addEventListener("deviceready", function() {
$(document).on('click', 'a._blank', function(event) {
event.preventDefault();
var url = $(this).attr('href');
var success = function(url) {
console.log('success! ' + url);
}
var error = function() {
console.log('error');
}
// execute the plugin
// exec(successCallback, errorCallback, pluginName, pluginMethod, params)
cordova.exec(success, error, "OpenUrl", "openUrl", [url]);
// the other way that opens in the InAppBrowser (which sucks)
// window.open(url, '_blank');
});
});
</script>
/********* OpenUrl.h Cordova Plugin Header *******/
#import <Cordova/CDV.h>
@interface OpenUrl : CDVPlugin
- (void)openUrl:(CDVInvokedUrlCommand*)command;
@end
/********* OpenUrl.m Cordova Plugin Implementation *******/
#import "OpenUrl.h"
#import <Cordova/CDV.h>
@implementation OpenUrl
- (void)openUrl:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* pluginResult = nil;
NSString *url = [command.arguments objectAtIndex:0];
if (url != nil && [url length] > 0) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:url];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
@end
@typeoneerror
Copy link
Author

Add the .h and the .m files to the Plugins folder of your iOS project. Add the plugin entry to the config.xml file. Call the plugin from your .js code after deviceready.

@iamsonumalik
Copy link

I am getting this error, can you help me out?

Failed to restore plugin "OpenUrl" from config.xml. You might need to try adding it again. Error: Error: npm: Command failed with exit code 1 Error output:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment