0

I have created a Cordova App and created iOS platform. I have a requirement to create an Objective-C method in Xcode and invoke it from JavaScript/HTML page.

The code in AppDelegate.h is

#import <Cordova/CDVViewController.h>
#import <Cordova/CDVAppDelegate.h>

@interface AppDelegate : CDVAppDelegate {}

@end

The code in AppDelegate.m is

#import "AppDelegate.h"
#import "MainViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    self.viewController = [[MainViewController alloc] init];
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

@end

Any step-by-step tutorial or example?

2
  • What is an "xcode function"? Commented Jul 15, 2016 at 10:31
  • Objective C function? I am new to xocde/objective C. Need is a call from javascript and print NSLOG(@"Successfully a obj c method called from a javascript function"); Commented Jul 15, 2016 at 10:34

1 Answer 1

1

In Cordova, a native function that can be called from JavaScript is made using a plugin. Take a look at the Plugin Development Guide to create your own plugin or search for already existing plugins that implement the functionality that you need.


This sample is extracted from the iOS Plugin Development Guide:

1. Declare javascript function

window.echo = function(str, callback) {
    cordova.exec(callback, function(err) {
        callback('Nothing to echo.');
    }, "Echo", "echo", [str]);
};

2. Implement platform specific code:

/********* Echo.h Cordova Plugin Header *******/

#import 

@interface Echo : CDVPlugin

- (void)echo:(CDVInvokedUrlCommand*)command;

@end

/********* Echo.m Cordova Plugin Implementation *******/

#import "Echo.h"
#import 

@implementation Echo

- (void)echo:(CDVInvokedUrlCommand*)command
{
    CDVPluginResult* pluginResult = nil;
    NSString* echo = [command.arguments objectAtIndex:0];

    if (echo != nil && [echo length] > 0) {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:echo];
    } else {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
    }

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

@end

3. Create plugin config file:

<platform name="ios">
    <config-file target="config.xml" parent="/*">
        <feature name="Echo">
            <param name="ios-package" value="Echo" />
        </feature>
    </config-file>
</platform>

4. Call this function from Javascript:

window.echo("echome", function(echoValue) {
    alert(echoValue == "echome"); // should alert true.
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.