【问题标题】:Launching ViewController using objective-c within Cordova project在 Cordova 项目中使用 Objective-c 启动 ViewController
【发布时间】:2017-11-16 14:58:41
【问题描述】:

首先解释一下app结构是如何维护的

我这里有两个应用程序

首先,一个基于 Objective-C 的原生 iOS 应用程序运行良好,原生应用程序的任务是在应用程序启动后启动相机,捕获图像并进行一些 OpenGL 处理,显示捕获的图像。

这是通过在我的 main.m 文件中调用 ViewController 类来完成的,如下所示

#import <UIKit/UIKit.h>
#import "ViewController.h"

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([ViewController class]));
    }
}

这个 ViewController 类有一些属性可以让应用程序启动和运行并启动故事板

现在对于 cordova 应用程序,我创建了一个插件并将所有本机文件集成到插件中,因此一旦我们添加插件,它就会添加所有资源,包括源文件、资源文件、资产、故事板等。

当我们在javascript中触发插件时,它会调用原生类CustomPlugin,这是插件的起点,下面是相同的头文件和实现代码

CustomPlugin.h

#import <Cordova/CDV.h>
#import "ViewController.h"

@interface CustomPlugin : CDVPlugin

- (void) pluginInitialize;

- (void) process:(CDVInvokedUrlCommand*)command;

@end

CustomPlugin.h

#import "CustomPlugin.h"
#import "ViewController.h"


@interface CustomPlugin()

@end

@implementation CustomPlugin

NSString *_routeChangedCallbackId;
@synthesize viewc;


- (void) pluginInitialize {
    NSLog(@"CustomPlugin:pluginInitialize");

    _routeChangedCallbackId = nil;
}

- (void) process:(CDVInvokedUrlCommand*)command
{
    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"test"];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

@end

此实现当前只是将响应示例消息发送回调用者

我想要实现的是能够像在本机中一样调用/启动/初始化 ViewController 功能

请回答我这个冗长的问题,但这是必需的,因为我对 iOS 太陌生了。

【问题讨论】:

    标签: ios objective-c cordova uiviewcontroller cordova-plugins


    【解决方案1】:

    pluginInitialize 方法应该在 CDVPluginResult 和 commandDelegate 方法之间调用:

      - (void) process:(CDVInvokedUrlCommand*)command
        {
            CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"test"];
    
            [self pluginInitialize];
    
            [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        }
    

    https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithObjects/WorkingwithObjects.html

    【讨论】:

      【解决方案2】:

      成功解决了我从 Cordova 插件启动 ViewController 的问题,请参阅下面的帖子

      Plugin with UIViewController

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-30
        • 1970-01-01
        • 1970-01-01
        • 2023-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多