[英]Sending pluginResult from delegate method in iOS cordova
我有一個似乎尚未得到解決的問題。 我是Cordova的新手,所以我不確定這是否是框架中的要求。 我有一個使用Zbar libaray掃描條形碼的插件。 掃描的結果由委托管理,並在方法imagePickerController:didFinishPickingMediaWithInfo:中返回。 我的插件調用了scan方法,但是在我的scan方法結束后返回了。 我需要將日期返回到要求它的網站。 我需要知道如何獲取掃描方法以等待zbar委托完成,然后再發送對Webview的響應。 如果您能為我解決這個問題,請先謝謝您。 而且沒有調用[super writeJavascript:jsCallback]不起作用,我使用的是cordova而不是phonegap。
#import "Camera.h"
#import <Cordova/CDV.h>
#import "AppDelegate.h"
@implementation Camera
@synthesize resultStr, command, hasPendingOperation;
//Override
- (void)pluginInitialize
{
NSLog(@"%@", @"init Camera");
}
- (void)scan:(CDVInvokedUrlCommand*)mycommand{
NSLog(@"%@", @"Camera.scan");
self.command = mycommand;
ZBarReaderViewController *reader = [ZBarReaderViewController new];
reader.readerDelegate = self;
reader.supportedOrientationsMask = ZBarOrientationMaskAll;
ZBarImageScanner *scanner = reader.scanner;
// TODO: (optional) additional reader configuration here
// EXAMPLE: disable rarely used I2/5 to improve performance
[scanner setSymbology: ZBAR_I25
config: ZBAR_CFG_ENABLE
to: 0];
// present and release the controller
[self.viewController presentViewController:reader animated:YES completion:nil];
NSLog(@"%@", @"finsihed plugin");
}
- (void) imagePickerController: (UIImagePickerController*) reader
didFinishPickingMediaWithInfo: (NSDictionary*) info
{
// ADD: get the decode results
id<NSFastEnumeration> results =
[info objectForKey: ZBarReaderControllerResults];
ZBarSymbol *symbol = nil;
for(symbol in results)
// EXAMPLE: just grab the first barcode
break;
self.resultStr = symbol.data;
// ADD: dismiss the controller (NB dismiss from the *reader*!)
[reader dismissViewControllerAnimated:YES completion:nil];
CDVPluginResult* pluginResult = nil;
if (self.resultStr != nil && [self.resultStr length] > 0) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:self.resultStr];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Failed to scan barcode!"];
}
NSLog(@"%@", self.resultStr); //<----- this is the date I need to return to my //webview this issue is scan: has already completed and returned
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
@end
從javascript調用插件時,您將提供成功和失敗的回調。 此回調是插件將值傳遞回Web層的唯一方法。
具體來說,您的JavaScript看起來像這樣。
cordova.exec(
function (result) {
// do stuff with plugin result! Hurray!
},
function (error) {
self.alert("Things went downhill, sorry... :\r\r" + error);
},
"PluginName", "MethodName", [parameters]
);
由於插件結果已經通過回調傳遞,因此由委托提供的事實應該是無關緊要的。
更新資料
您的問題詢問是否要在Objective-C端等待。 不要等 。 那不是Cordova的設計方式,如果您不立即從通話中返回,甚至會在控制台中彈出警告。
Cordova插件調用旨在調用異步回調,而您必須圍繞它們設計Web界面。
一種常見的方法是在執行呼叫時顯示微調框或占位符文本
在回調中:
我發現了問題,這與UIImagePickerController
的范圍有關。 當委托方法運行時,此對象是唯一仍在范圍內的對象。 我錯誤地將回調ID保存為類屬性,我以為在imagePickerController:didFinishPickingMediaWithInfo:
方法時可以檢索到它。
解決方法只是擴展ZBAR閱讀器類並添加屬性,以便我可以存儲回叫ID。 所有Cordova插件都需要正確的回調ID才能返回JS-side屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.