【问题标题】:iOS - can't interact with Cordova webview contentiOS - 无法与 Cordova webview 内容交互
【发布时间】:2016-08-31 09:56:36
【问题描述】:

所以我在 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>

【问题讨论】:

    标签: ios xcode cordova webview viewcontroller


    【解决方案1】:

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

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

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-13
      • 2012-05-10
      • 1970-01-01
      • 2020-08-09
      • 2017-10-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多