【问题标题】:worklight 6.0 adapter native ios to hybrids applicationworklight 6.0 将本机 ios 适配到混合应用程序
【发布时间】:2015-07-04 00:56:04
【问题描述】:

我可以在 IOS 本机代码中调用 worklight 6.0 适配器调用(使用 Objective-C),但我无法使用混合应用程序中的 cordova 插件读取适配器 JSON 响应。

// 调用适配器

MyConnectListener *connectListener = [[MyConnectListener alloc] initWithController:self];
[[WLClient sharedInstance] wlConnectWithDelegate:connectListener];
    // calling the adapter using objective-c
    WLProcedureInvocationData *myInvocationData = [[WLProcedureInvocationData alloc] initWithAdapterName:@"HTTP_WS_ADPTR" procedureName:@"getBalance"];
    MyInvokeListener *invokeListener = [[MyInvokeListener alloc] initWithController: self];
    [[WLClient sharedInstance] invokeProcedure:myInvocationData withDelegate:invokeListener];

CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:responseString];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];

// 混合应用程序调用本机代码

cordova.exec(sayHelloSuccess, sayHelloFailure, "HelloWorldPlugin", "sayHello", [name]);

以上cordova.exec成功和失败的方法是不返回值的。

但我无法解析来自 CDVPluginResult 方法的值。任何人请给我建议。如何从混合应用程序中读取 IOS 本机适配器。

【问题讨论】:

  • 你能解释一下这个场景吗?你有一个 iPhone 环境的混合应用程序,你正在调用一个 Cordova 插件(一个类......),你试图在其中调用一个适配器?为什么?为什么不直接在 JS 代码中调用适配器?还要提及您遇到的 错误 以及 sayHelloSuccesssayHelloFailure 的实现。
  • 对不起,打错了...等待我的回复
  • 实际上,我们正在尝试使用 Objective-C 将适配器从 IOS 本地调用到使用 Cordova 插件的 Hybrid。所以,问题是,使用上述方法没有按预期触发。
  • 以下是您查询的答案;你试图在其中调用适配器?为什么?为什么不直接在 JS 代码中调用适配器?因为,根据我们的场景,要求是证明我们可以在原生应用和混合应用之间调用。
  • 还提到你得到的错误以及sayHelloSuccess和sayHelloFailure的实现没有错误,我们没有得到任何回应。

标签: ibm-mobilefirst worklight-adapters


【解决方案1】:

注意几点:

  • 您使用的是 Worklight 6.0.0.x。在 Worklight 6.0.0.x 中,Web 视图和本机视图之间没有适当的会话共享。这意味着,例如,如果您将在 Web 视图中调用 WL.Client.connect() 方法,然后在本机视图中执行 connect() 和适配器调用 - 这些调用将不会共享相同的会话,这可能导致竞争条件错误,无法在视图和其他意外事件之间共享状态。不推荐。

  • 如果这是您希望在混合应用程序中实施的方法,因此强烈建议您升级到 MobileFirst(以前称为“Worklight”)v6.3 或 v7.0,其中会话现在可以开箱即用地在 Web 视图和原生视图之间共享。

  • 虽然您可能只想选择从 JS 代码调用适配器...


要使其在您提供的项目中按原样工作,您可以根据以下内容更改实现。

请注意,以下实现基于 MFP 7.0,因此适配器调用代码在您的 6.0.0.0x 代码库中不起作用。您需要根据自己在 v6.0.0.x 中的代码对其进行更改:

sayHello.h

#import <Foundation/Foundation.h>
#import <Cordova/CDV.h>
#import "WLClient.h"
#import "WLDelegate.h"

@interface SayHelloPlugin : CDVPlugin

- (void)sayHello:(CDVInvokedUrlCommand*)command;
- (void)callResult:(NSString*)response;

@end

sayHello.m

#import "SayHelloPlugin.h"
#import "MyConnectListener.h"

CDVInvokedUrlCommand *tempCommand;

@implementation SayHelloPlugin

- (void)sayHello:(CDVInvokedUrlCommand*)command {
    MyConnectListener *connectListener = [[MyConnectListener alloc] init:self];
    [[WLClient sharedInstance] wlConnectWithDelegate:connectListener];

    tempCommand = command;
}

-(void)callResult:(NSString*)response{    
    CDVPluginResult *pluginResult =
    [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:response];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:tempCommand.callbackId];
}

@end

MyConnectListener.h

#import <Foundation/Foundation.h>
#import "WLClient.h"
#import "WLDelegate.h"
#import "SayHelloPlugin.h"

@interface MyConnectListener : NSObject <WLDelegate> {
    @private
   SayHelloPlugin *sh;
}

- (id)init: (SayHelloPlugin *)sayHello;
@end

MyConnctListener.m

responseText 行已被注释掉,因为我想从适配器检索到的数据太大,所以最好只返回你真正需要的,而不是全部。

#import "MyConnectListener.h"
#import "WLResourceRequest.h"


NSString *resultText;
NSString *request;

@implementation MyConnectListener
- (id)init: (SayHelloPlugin *) sayHello{
    if ( self = [super init] )
    {
        sh = sayHello;
    }
    return self;
}

-(void)onSuccess:(WLResponse *)response{
    NSURL* url = [NSURL URLWithString:@"/adapters/testAdapter/getStories"];
    WLResourceRequest* request = [WLResourceRequest requestWithURL:url method:WLHttpMethodGet];

    [request setQueryParameterValue:@"['technology']" forName:@"params"];

    [request sendWithCompletionHandler:^(WLResponse *response, NSError *error) {
        if(error != nil){
            resultText = @"Invocation failure: ";
            resultText = [resultText stringByAppendingString: error.description];

            [sh callResult:resultText];
        }
        else{
            resultText = @"Invocation success. ";
            //resultText = [resultText stringByAppendingString:response.responseText];

            [sh callResult:resultText];
        }
    }];
}

-(void)onFailure:(WLFailResponse *)response{
    resultText = @"Connection failure: ";

    resultText = [resultText stringByAppendingString:[response errorMsg]];
    NSLog(@"***** failure response: %@", resultText);
    [sh callResult:resultText];
}

@end

【讨论】:

  • 非常感谢兄弟
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多