Skip to content

Instantly share code, notes, and snippets.

@lidio601
Created May 8, 2015 22:35
Show Gist options
  • Save lidio601/759775aa961d81f30ccf to your computer and use it in GitHub Desktop.
Save lidio601/759775aa961d81f30ccf to your computer and use it in GitHub Desktop.
Objective-C: web view on a ios app. test of javascript injection
<html>
<head>
<script type="text/javascript">
function doWork() {
document.getElementById('result').value = genSeq();
}
</script>
</head>
<body>
<h1>it works!</h1><br />
<button onclick="doWork();">Run</button><br />
<textarea cols="30" rows="10" id="result"></textarea>
</body>
</html>
//
// ViewController.h
// testWebView
//
// Created by Fabio Cigliano on 21/02/13.
// Copyright (c) 2013 Fabio Cigliano. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (nonatomic,weak) IBOutlet UIWebView *webview;
@end
//
// TestWebviewVC.m
// testWebView
//
// Created by Fabio Cigliano on 21/02/13.
// Copyright (c) 2013 Fabio Cigliano. All rights reserved.
//
/*
http://stackoverflow.com/questions/8848311/how-to-inject-javascript-in-my-uiwebview
http://stackoverflow.com/questions/10010342/clicking-on-a-link-inside-a-webview-that-will-trigger-a-native-ios-screen-with
http://stackoverflow.com/questions/4645414/how-can-i-load-a-local-html-file-into-the-uiwebview
http://stackoverflow.com/questions/1662565/uiwebview-finished-loading-event
*/
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[self webview] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[[self webview] stringByEvaluatingJavaScriptFromString:@"var genSeq = function() { return new Array(0,0,0); };"];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment