Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I replace the deprecated methods toSuccessCallbackString and writeJavascript in Objective-C?

Since I am coming from a Java background I am not an Objective-C expert and thus struggling a bit to modify the following code:

- (void) loadHTML:(CDVInvokedUrlCommand*)command
{

    NSString* callbackId = command.callbackId;
    NSArray *arguments = command.arguments;

    CDVPluginResult* pluginResult;

    if (webView)
    {
        NSString *stringObtainedFromJavascript = [arguments objectAtIndex:0]; 
        [webView loadHTMLString:stringObtainedFromJavascript baseURL:baseURL];

        if (screenNeedsInit) {
            [self makeScreenVisible];
        }

        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: WEBVIEW_OK];
        [self writeJavascript: [pluginResult toSuccessCallbackString:callbackId]];
    }
    else
    {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString: WEBVIEW_UNAVAILABLE];        
        [self writeJavascript: [pluginResult toErrorCallbackString:callbackId]];    
    }

}

The compiler complains that both, writeJavascript as well as toErrorCallbackString are deprecated and I should replace them with evalJS and pluginResult.

So, my first step was to change this line:

[self writeJavascript: [pluginResult toSuccessCallbackString:callbackId]];

like this:

[self.commandDelegate evalJs: [pluginResult toSuccessCallbackString:callbackId]];

So, this worked, but I still need to replace toSuccessCallbackString with sendPluginResult, so I googled up this:

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

But how do I combine evalJS with sendPluginResult now? In the old version it seemed to me that pluginResult toSuccessCallbackString simply returned a NSString* but now with sendPluginResult there seems to be a callback involved? How do I manage this to pass the result of sendPluginResult to evalJS.

Note: I am using the cordova api for this.

Please be gentle, I didn't write much Objective-C yet and I struggle with the syntax.

like image 255
Timo Ernst Avatar asked Mar 04 '15 08:03

Timo Ernst


1 Answers

Use this:

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

if you used CDVCommandStatus_OK on the pluginResult, then it will call the success callback, if you used CDVCommandStatus_ERROR then it will call the error callback

Your javascript should be something like this:

cordova.exec(successCallback, errorCallback, "YourPluginName", "loadHTML",["yourHtmlString"]);
like image 112
jcesarmobile Avatar answered Oct 03 '22 07:10

jcesarmobile