iOS使用UIWebview與Javascript整合的範例

1. 先將UI透過Builder拉好

2. 將控制項都對應到CODE(按住CTRL直到CODE中)

3. 輸入相對應的CODE。


#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 initView];
}

- (void) initView {
//Field init

//Behavior Binding
self.webView.delegate = self;

//Other init logic
NSString * filePath = [[NSBundle mainBundle] pathForResource:@"home" ofType:@"html"];

NSURL * url = [NSURL fileURLWithPath:filePath];
NSURLRequest * req = [NSURLRequest requestWithURL:url];

[self.webView loadRequest:req];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)dealloc {
[_webView release];
[super dealloc];
}
@end

4. 此時需要調整Build Phase的一些設定,把Jquery.js從Compile Sources移到下方的Copy Bundle Resources

5. 這時JavaScript就可以Works了

6. 最後再處理下方的BUTTON對應Js的CODE(剛好都有overview的div)

(home.html 相對應的HTML)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *