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.
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"]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With