Skip to content

Instantly share code, notes, and snippets.

@ChrisRisner
Created November 9, 2012 18:04
Show Gist options
  • Save ChrisRisner/4047216 to your computer and use it in GitHub Desktop.
Save ChrisRisner/4047216 to your computer and use it in GitHub Desktop.
ios day 14
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"LocalPage" ofType:@"html" inDirectory:nil];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
//Append javascript
NSString *script = @"<script>alert(\"This is an alert!!\");</script>";
htmlString = [htmlString stringByAppendingString:script];
[self.webView loadHTMLString:htmlString baseURL:nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [[NSURL alloc] initWithString:@"http://chrisrisner.com"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[self.webView loadRequest:request];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"LocalPage" ofType:@"html" inDirectory:nil];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:htmlString baseURL:nil];
}
<html>
<body>
<h1>Hello iOS!</h1>
</body>
</html>
<html>
<body>
<h1>Hello iOS!</h1>
<input type='button' value="Do something in iOS" onclick='window.location="callmycode:cmd=showAlert&var=myVar"'/>
</body>
</html>
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [[NSURL alloc] initWithString:@"http://chrisrisner.com"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
self.webView.scalesPageToFit = YES;
[self.webView loadRequest:request];
}
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *URL = [request URL];
if ([[URL scheme] isEqualToString:@"callmycode"]) {
NSString *urlString = [[request URL] absoluteString];
NSArray *urlParts = [urlString componentsSeparatedByString:@":"];
//check to see if we just got the scheme
if ([urlParts count] > 1) {
NSArray *parameters = [[urlParts objectAtIndex:1] componentsSeparatedByString:@"&"];
NSString *methodName = [parameters objectAtIndex:0];
NSString *variableName = [parameters objectAtIndex:1];
NSString *message = [NSString stringWithFormat:@"Obj-c from js with methodname=%@ and variablename=%@", methodName, variableName];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Great" message:message delegate: self cancelButtonTitle: nil otherButtonTitles: @"OK",nil, nil];
[alert show];
}
}
return YES;
}
[self.webView stringByEvaluatingJavaScriptFromString:@"alert('Trigger the JS!');"];
@interface ViewController : UIViewController <UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@end
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"LocalPage" ofType:@"html" inDirectory:nil];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
//Append javascript
//NSString *script = @"<script>alert(\"This is an alert!!\");</script>";
//htmlString = [htmlString stringByAppendingString:script];
self.webView.delegate = self;
[self.webView loadHTMLString:htmlString baseURL:nil];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment