我有一個Phonegap Cordova插件。在這個插件中,我收到了來自javascript的一個點擊事件。這個點擊使用我的客戶端庫觸發了一個文件下載。 此文件下載在我的插件中發送事件和調用方法,因爲我已將其設置爲委託。Phonegap - 從插件代理的Objective-c發送消息到Javascript
我無法設法使用'stringByEvaluatingJavaScriptFromString'將這些事件發送回javascript這似乎不會觸發該調用。
它正在工作,當我試圖調用它後,JavaScript點擊回聲插件方法。
這裏是.M插件類:
#import "CCDataBundlePlugin.h"
#import <Cordova/CDV.h>
@implementation CCDataBundlePlugin
-(id)init{
[MYCLIENTLIB sharedInstance].delegate = self;
self = [super init];
return self;
}
- (void)echo:(CDVInvokedUrlCommand*)command
{
NSLog(@"------ Received click");
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];
}
// When latest extraction succeeded, call next download
[[MYCLIENTLIB sharedInstance] downloadBundlesForChannel:@"myChannel"];
// When latest download succeeded, call next extraction
[[MYCLIENTLIB sharedInstance] extractBundlesForChannel:@"myChannel"];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
-(void)callJavascriptMethodWithString:(NSString*)stringParameter{
NSString* jsString = [NSString stringWithFormat:@"CN.Interface.Recepter.receiveWithString(\"%@\");", stringParameter];
//The line under is not working, I don't get the function call in JS
[self.webView stringByEvaluatingJavaScriptFromString:jsString];
}
// DOWNLOAD EVENTS Called from the Library
- (void)dataBundle:(MYCLIENTLIB *)dataBundle downloadDidStartForChannelIdentifier:(NSString *)channelIdentifier {
NSLog(@"download in progress..."); // this is logged
[self callJavascriptMethodWithString:@"download in progress..."];
// The line above do calls the method
}
我不想有什麼可以在插件中調用Obj-c方法,我已經有了。我想從Obj-c方法調用JS函數。我看過你用 [self writeJavascript:javaScript]; 它爲我工作,但只有一次,這很奇怪! – arlg
其實它的工作,當我把它放入回聲方法。但在另一種方法,如downloadDidStartForChannelIdentifier,它does not – arlg
你需要始終有一個回調ID爲它發送/寫入javascript。這是從調用js方法收到的。我非常確定回調ID只有在js方法被接收到回調成功/錯誤時觸發的時間纔有生命週期。 – ezekielDFM