iOS web與js的簡單互動

來源:互聯網
上載者:User

標籤:

我們在封裝網頁的時候經常會遇到需要往網頁裡面的控制項添加資料,但是怎麼添加又成了痛點。本人最近在開發的時候就遇到這樣的事,解決之後,來和大家分享一下。

 

//以必應網站為例

[web loadRequest:

[NSURLRequestrequestWithURL:

[NSURL URLWithString:@"http://www.bing.com/?FORM=Z9FD1"]]];

 

以下就是主要代碼,只有幾句

 

//擷取當前頁面的title

NSString *title =

[webView

stringByEvaluatingJavaScriptFromString:

@"document.title"];

NSLog(@"title====%@",title);

//擷取當前URL

NSString *URL =

[webView

stringByEvaluatingJavaScriptFromString: @"document.location.href"];

NSLog(@"URL===%@",URL);

//得到網頁代碼

NSString *html =

[webView

stringByEvaluatingJavaScriptFromString:

@"document.documentElement.innerHTML" ];

NSLog(@"html====%@",html);

//拼接字串 根據網頁name找到控價並賦值

NSString *str = @"隨_的簡書";

NSString *JSStr =

[NSString stringWithFormat:

@"document.getElementsByName(‘q‘)[0].value = (‘%@‘);",str];

[webView stringByEvaluatingJavaScriptFromString: JSStr];

  以下是全部代碼,commit+c commit+v可以直接運行

 

@interface ViewController ()

@property (weak, nonatomic) UIWebView *web;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

UIWebView *web =

[[UIWebView alloc] initWithFrame:

[UIScreen mainScreen].bounds];

UIButton *back =

[[UIButton alloc] initWithFrame:CGRectMake

(0, 0, self.view.frame.size.width,self.view.frame.size.width / 7.5)];

[back setBackgroundColor: [UIColor orangeColor]];

[back setTitle:@"back" forState:UIControlStateNormal];

[back addTarget:self

action: @selector(back)

forControlEvents:UIControlEventTouchUpInside];

//以必應為例

[web loadRequest:

[NSURLRequest requestWithURL:

[NSURL URLWithString:

@"http://www.bing.com/?FORM=Z9FD1"]]];

web.delegate = self;

web.scalesPageToFit =YES;

web.scrollView.delegate = self;

self.web = web;

[self.view addSubview:web];

[self.view addSubview:back];

}

#pragma mark ---Delegate

-(void) webViewDidStartLoad:

(UIWebView *)webView{

NSLog(@"開始載入---") ;

}

- (void) webViewDidFinishLoad:

(UIWebView *)webView {

NSLog(@"載入完成---");

//擷取當前頁面的title

NSString *title =

[webView

stringByEvaluatingJavaScriptFromString:

@"document.title"];

NSLog(@"title====%@",title);

//擷取當前URL

NSString *URL =

[webView

stringByEvaluatingJavaScriptFromString:

@"document.location.href"];

NSLog(@"URL===%@",URL);

//得到網頁代碼

NSString *html =

[webView

stringByEvaluatingJavaScriptFromString:

@"document.documentElement.innerHTML" ];

NSLog(@"html====%@",html);

//拼接字串 根據網頁name找到控價並賦值

NSString *str = @"隨_的簡書";

NSString *JSStr =

[NSString stringWithFormat:

@"document.getElementsByName(‘q‘)[0].value = (‘%@‘);",str];

[webView stringByEvaluatingJavaScriptFromString:JSStr];

}

- (void) webView:

(UIWebView *)webView

didFailLoadWithError:(NSError *)error {

NSLog(@"載入失敗===%@",error);

}

//當網頁位置為頂部 不允許繼續下拉

- (void) scrollViewDidScroll:

(UIScrollView *)scrollView {

if (self.web.frame.origin.y == 0) {

self.web.scrollView.bounces = NO;

return;

}

}

//webView的每次頁面跳轉都會執行,在這裡可以得到想要的資料

- (BOOL)webView:

(UIWebView *)webView

shouldStartLoadWithRequest:(NSURLRequest *)request

navigationType:(UIWebViewNavigationType)navigationType {

NSLog(@"頁面跳轉");

return YES;

}

//返回

- (void) back {

[self.web goBack];

}

 

iOS web與js的簡單互動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.