2013-07-23 22 views
3

我有一個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 

} 

回答

1

我寫了一個插件科爾多瓦教程不是早就在這裏:http://www.zen-sign.com/writing-a-cordova-plugin/。這對你應該是一個很好的幫助。在此期間,這裏是基本結構我已經成功地使用了內我的init/congifure方法爲我的本地端插件包裝:

- (void)configurePlugin:(CDVInvokedUrlCommand*)command 
{ 
     // Cordova Result object and stirng to send to JS 
     CDVPluginResult* pluginResult = nil; 
     NSString* javaScript = nil; 

     @try 
     { 
      // Create an instance of the class we want to wrap. This could be your 3rd party library or whatever you want. 
      wrappedClass = [[WrapMeUp alloc] init]; 

      pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"{'success' : '1'}"]; 
      javaScript = [pluginResult toSuccessCallbackString:command.callbackId]; 

     } 
     @catch (NSException* exception) 
     { 
      //Sumptin went worng! 
      pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION messageAsString:[exception reason]]; 
      javaScript = [pluginResult toErrorCallbackString:command.callbackId]; 
     } 

     // Write plugin results back to JS callback 
     [self writeJavascript:javaScript]; 
} 
+0

我不想有什麼可以在插件中調用Obj-c方法,我已經有了。我想從Obj-c方法調用JS函數。我看過你用 [self writeJavascript:javaScript]; 它爲我工作,但只有一次,這很奇怪! – arlg

+0

其實它的工作,當我把它放入回聲方法。但在另一種方法,如downloadDidStartForChannelIdentifier,它does not – arlg

+0

你需要始終有一個回調ID爲它發送/寫入javascript。這是從調用js方法收到的。我非常確定回調ID只有在js方法被接收到回調成功/錯誤時觸發的時間纔有生命週期。 – ezekielDFM

1

只是如果有人想這樣做,我設法做到這一點使用JavaScript定時器調用本機插件方法。我會搜索如果我可以找到一個更清潔的方式