iOS - 无法与 Cordova webview 内容交互

iOS - can't interact with Cordova webview content

所以我在我的 UIViewController 中添加了一个 CDVViewController,它呈现我的 Cordova 应用程序的索引页面。我添加了一个 link(稍后我将尝试将其解释为 objective-c 功能。)和一个按钮。当我加载索引时,我可以单击按钮和 link 并查看视觉响应。但是,当它由 CDVViewController 呈现时,它不会。我在下面包含了我的控制器和 index.html 代码,但我也无法使用 Cordova-iOS MainViewController class 与内容进行交互。有人知道我该如何解决这个问题吗?

这些是我试图用来实现这一目标的参考资料。

https://cordova.apache.org/docs/en/2.2.0/guide/cordova-webview/ios.html

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

MyViewController.h

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

@interface MyViewController : UIViewController{

}
@property (strong, nonatomic) IBOutlet UIView *MyPlaceholderView;

@end

MyViewController.m

#import "MyViewController.h"

@interface MyViewController ()

@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


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

    CDVViewController* viewController = [CDVViewController new];
    viewController.view.frame = _MyPlaceholderView.frame;
    [self.view addSubview:viewController.view];
}
@end

index.html

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <title>Hello World</title>
    </head>
    <body>
            <a class="MyButton" href="req://LoadNativeContent">Load Native Content</a>
            <button id="button" style="width:100%; height:100px;"> Button 1 </button>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
    </body>
</html>

Alec,如果你想要实现的是从 webview 内的按钮执行本机 iOS 代码,正确的方法应该是创建一个 cordova 插件并通过 [=18] 公开本机代码=] 界面。 您可以在此处找到有关如何执行此操作的详细信息:

https://cordova.apache.org/docs/en/latest/guide/hybrid/plugins/index.html

希望对您有所帮助!