【问题标题】:iOS Cordova Application - shouldStartLoadWithRequestiOS Cordova 应用程序 - shouldStartLoadWithRequest
【发布时间】:2016-05-09 15:42:15
【问题描述】:

我已经构建了一个 Cordova 应用程序,我只是在 xcode 中添加了一些本机功能。我想从我的应用程序中截取 url,如下所示:

How to invoke Objective C method from Javascript and send back data to Javascript in iOS?

所以在我的 HTML 中我包含了一个链接

index.html

<a class="MyButton" id="id" href="req://ResultA">Item A</a>

然后我只有一个从 UIViewController 继承的非常基本的头文件

MyViewController.h

#import <UIKit/UIKit.h>
#import <Cordova/CDVViewController.h>

@interface MyViewController : UIViewController

@end

然后在我的MyViewController.m 文件中,我要做的就是从我的链接中解释 url。我想实现以下目标。

MyViewController.m

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    CDVViewController* StorySelectCDVViewController = [CDVViewController new];
    StorySelectCDVViewController.view.frame = self.view.frame;
    [self.view addSubview:StorySelectCDVViewController.view];
}

...

-(bool)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if ([[url scheme] isEqualToString:@"req"])
        //Store ResultA as a variable for later use.
}

但对于 CDVViewController。除此之外,我一直在阅读,尝试将 ViewController 用作 CDVViewController 的委托对于此方法是不好的做法,因为它会与 API 调用混淆?


或者,我也可以尝试复制 Cordova 应用程序提供的 MainViewController 并创建一个继承 CDVViewController 的 ViewController...

MyViewController.h

#import <Cordova/CDVViewController.h>
#import <Cordova/CDVCommandDelegateImpl.h>
#import <Cordova/CDVCommandQueue.h>

@interface MyViewController : CDVViewController

@end

@interface MyCommandDelegate : CDVCommandDelegateImpl
@end

@interface MyCommandQueue : CDVCommandQueue
@end

MyViewController.m

#import "MyViewController.h"

@implementation MyViewController

- (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Uncomment to override the CDVCommandDelegateImpl used
        // _commandDelegate = [[MyCommandDelegate alloc] initWithViewController:self];
        // Uncomment to override the CDVCommandQueue used
        // _commandQueue = [[MyCommandQueue alloc] initWithViewController:self];
    }
    return self;
}

- (id)init
{
    self = [super init];
    if (self) {
        // Uncomment to override the CDVCommandDelegateImpl used
        // _commandDelegate = [[MyCommandDelegate alloc] initWithViewController:self];
        // Uncomment to override the CDVCommandQueue used
        // _commandQueue = [[MyCommandQueue alloc] initWithViewController:self];
    }
    return self;
}


@end

@implementation MyCommandDelegate

#pragma mark CDVCommandDelegate implementation

- (id)getCommandInstance:(NSString*)className
{
    return [super getCommandInstance:className];
}

- (NSString*)pathForResource:(NSString*)resourcepath
{
    return [super pathForResource:resourcepath];
}

@end

@implementation MyCommandQueue

- (BOOL)execute:(CDVInvokedUrlCommand*)command
{
    return [super execute:command];
}

@end

但是我不确定如何适当地修改它以适当地利用 URL 命令。有人有什么想法吗?

【问题讨论】:

  • 当您必须从 HTML/javascript 与本机代码进行通信时,您可以使用这种方法,但您将不得不处理 Cordova 创建的本机项目并在 WebView 委托方法中创建条件块(不好)。我通常更喜欢为这些场景创建一个原生插件,f or me 更干净、更容易。你可以谷歌搜索 iOS 插件示例并查看 here
  • 非常感谢您的回复!这非常有帮助!不过,对于创建自己的插件的结构,我仍然有些困惑。我有一个新的 Cordova 项目。你能告诉我我需要哪些组件以及它们应该去哪里吗?我不确定using the plugin.xml file to inject this markup automatically 之类的事情。我知道我需要 xcodeproject 插件文件夹中的 CDVPlugin 文件,我可以在 ios 平台中配置 config.xml 文件,我只需要知道东西在哪里。谢谢!

标签: javascript ios cordova delegates


【解决方案1】:

这是创建 iOS 插件所需要的:

  • plugin.xml
  • src/ios/YourPluginName.h
  • src/ios/YourPluginName.m
  • www/YourPluginName.js

插件.xml

<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
           id="yourpluginid"
      version="1.0.0">
    <name>YourPluginName</name>
    <description>Your Plugin Description</description>
    <license>Apache 2.0</license>
    <js-module src="www/YourPluginName.js" name="YourPluginName">
        <clobbers target="YourPluginName" />
    </js-module>
    <platform name="ios">
        <config-file target="config.xml" parent="/*">
            <feature name="YourPluginName">
                <param name="ios-package" value="YourPluginName"/>
            </feature>
        </config-file>
        <header-file src="src/ios/YourPluginName.h" />
        <source-file src="src/ios/YourPluginName.m" />
    </platform>
</plugin>

src/ios/YourPluginName.h

#import <Foundation/Foundation.h>
#import <Cordova/CDV.h>

@interface YourPluginName : CDVPlugin

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

@end

src/ios/YourPluginName.m

#import "YourPluginName.h"

@implementation YourPluginName

- (void)pluginMethodName:(CDVInvokedUrlCommand*)command {
    CDVPluginResult* pluginResult = nil;
    //Get param
    NSString *param = [command.arguments objectAtIndex:0];
    //Do something
    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

www/YourPluginName.js

var exec = require('cordova/exec');

var YourPluginName = {
    pluginMethodName:function(param, successCallback, errorCallback) {
        exec(successCallback, errorCallback, "YourPluginName", "pluginMethodName", [param]);
    }
};

module.exports = YourPluginName;

将每个文件放在一个文件夹中,然后从您的 cordova 项目的根文件夹中键入:

cordova plugin add pluginfolderpath

然后从您的 javascript(在 onDeviceReady 事件之后)您可以执行以下操作:

YourPluginName.pluginMethodName("param", function(){}, function(){});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-29
    • 2018-11-23
    • 2016-08-31
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多