Skip to content

Instantly share code, notes, and snippets.

@takuhou
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takuhou/19bfc59aac8264a98925 to your computer and use it in GitHub Desktop.
Save takuhou/19bfc59aac8264a98925 to your computer and use it in GitHub Desktop.
スマートフォンにおける広告配信用SDKの設計について考えてみた ref: http://qiita.com/takuhou/items/5969b7f2485b779044a3
<html>
<head></head>
<body>
<script>
var X ="1";
var Y ="2";
var Z ="3";
var scheme = "jp.co.takuhou";
var callNative = function(url){
location.href = url + "://callback";
}
callNative(scheme);
</script>
</body>
</html>
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UIWebViewDelegate>{
UIWebView *wv;
}
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
wv = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 0.0, 0.0, 0.0)];
wv.delegate = self;
[self.view addSubview:wv];
NSURL *url = [NSURL URLWithString:@"http://XXXXXX.co.jp/sample.html"];
[wv loadRequest:[NSURLRequest requestWithURL:url]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) callback{
NSString *X = [wv stringByEvaluatingJavaScriptFromString:@"getx();"];
NSString *Y = [wv stringByEvaluatingJavaScriptFromString:@"gety();"];
NSString *Z = [wv stringByEvaluatingJavaScriptFromString:@"getz();"];
NSLog(@"%@",X);
NSLog(@"%@",Y);
NSLog(@"%@",X);
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSString *requestString = [[request URL] absoluteString];
NSLog(@"%@",requestString);
if ([requestString rangeOfString:@"jp.co.takuhou://"].location == NSNotFound){
return YES;
}
NSError *
error = nil;
NSString *pattern = [NSString stringWithFormat:@"^jp.co.takuhou://(.+)$"];
NSRegularExpression *reg = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
if(error != nil){
NSLog(@"%@", [error description]);
return NO;
}
NSTextCheckingResult *match = [reg firstMatchInString:requestString options:0 range:NSMakeRange(0, requestString.length)];
if(match.numberOfRanges == 0){
NSLog(@"not match");
return NO;
}
NSString *method = [requestString substringWithRange:[match rangeAtIndex:1]];
if([method isEqualToString:@"callback"]){
[self callback];
}
return NO;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment