3

So I've added a CDVViewController to my UIViewController which renders the index page of my Cordova application. I've added a link (which I'm going to later try to interpret as objective-c functionality.) and a button. When I load the index I can click on the buttons and link and see a visual response. However when it is being rendered by a CDVViewController it doesn't. I've included my controller and the index.html code below but I also can't interact with the content using the Cordova-iOS MainViewController class. Does anybody know how I can fix this?

These were the references I was trying to use to achieve this.

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>
Community
  • 1
  • 1
Alec Gamble
  • 441
  • 3
  • 6
  • 16

1 Answers1

1

Alec, if what you are trying to achieve is to execute native iOS code from a button inside the webview the right approach should be to create a cordova plugin and expose the native code through a javascript interface. You can find the details of how to do this here:

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

Hope it helps!

Javier Abrego
  • 462
  • 3
  • 12