iOS - 无法与Cordova webview内容互动

时间:2016-05-05 14:43:10

标签: ios xcode cordova webview viewcontroller

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

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

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>

1 个答案:

答案 0 :(得分:1)

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

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

希望它有所帮助!