在iOS9.0以後須要在ifo.plist文件中添加一個配置文件,否則只能識別https開頭的網址,http開頭的不識別web
主要有三個步驟ide
1 // 1.獲取URL 2 NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"]; 3 4 5 // 2.發送請求 6 NSURLRequest *request = [NSURLRequest requestWithURL:url]; 7 8 9 // 3.加載內容 10 [self.webView loadRequest:request];
還有一些代理方法:atom
1 #import "RootViewController.h" 2 #import "RootView.h" 3 4 @interface RootViewController () <UIWebViewDelegate> 5 @property (nonatomic, strong) RootView *rootView; 6 7 @end 8 9 @implementation RootViewController 10 11 - (void)loadView { 12 self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds]; 13 self.view = self.rootView; 14 } 15 16 - (void)viewDidLoad { 17 [super viewDidLoad]; 18 19 // 設置代理 20 self.rootView.webView.delegate = self; 21 22 // 添加按鈕事件 23 [self.rootView.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; 24 25 [self.rootView.button1 addTarget:self action:@selector(click1:) forControlEvents:UIControlEventTouchUpInside]; 26 } 27 28 29 #pragma mark - 代理方法 30 - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { 31 32 NSLog(@"加載失敗"); 33 } 34 35 36 - (void)webViewDidFinishLoad:(UIWebView *)webView { 37 38 NSLog(@"加載完成"); 39 40 // 獲取輸入的內容 41 NSString *str = [webView stringByEvaluatingJavaScriptFromString:@"document.title"]; 42 NSLog(@"title = %@", str); 43 } 44 45 46 - (void)webViewDidStartLoad:(UIWebView *)webView { 47 48 NSLog(@"開始加載"); 49 } 50 51 52 #pragma mark - 實現按鈕點擊事件(返回上一頁,前進到下一頁) 53 - (void)click:(UIButton *)sender { 54 55 // 返回上一頁 56 [self.rootView.webView goBack]; 57 } 58 59 - (void)click1:(UIButton *)sender { 60 61 // 返回上一頁 62 [self.rootView.webView goForward]; 63 } 64 @end