1

My Code is quite simple as below:

@interface ViewController ()
{
    UIWebView *_webView;
    SContext *_jsContext;
}

@end

@implementation ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    _webView = [[UIWebView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:_webView];
    _webView.delegate = self;
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.google.com"]]];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, 100, 50)];
    button.backgroundColor = [UIColor redColor];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
}

- (void)click
{
    NSLog(@"documentView.webView.mainFrame.javaScriptContext!");
    [_jsContext evaluateScript:@"alert('Hello Word')"];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    _jsContext = [_webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
}

@end

However, When I click the button, the alert window does not appear. I also try to evaluateScript "console.log('oh my god')" by jsContext, but it does not work too.

WATER1350
  • 141
  • 1
  • 1
  • 6

0 Answers0