[iOS基礎控制項,ios基礎控制項
A.基本使用1.概念iOS內建的瀏覽器控制項Safari瀏覽器就是通過UIWebView實現的 2.用途:製作簡易瀏覽器(1)基本請求建立請求載入請求(2)代理監聽webView載入,例如提示正在載入(3)前進、後退使用NavigationController,在底部加上導覽列(勾選showToolBar),加上回退和前進按鈕(4)給頂部導覽列加上地址欄UISearchBar代理監聽“搜尋”按鈕,提取地址欄文本 1 // 2 // ViewController.m 3 // UIWebViewdDemo 4 // 5 // Created by hellovoidworld on 15/1/30. 6 // Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8 9 #import "ViewController.h"10 11 @interface ViewController () <UISearchBarDelegate, UIWebViewDelegate>12 13 @property (weak, nonatomic) IBOutlet UIWebView *webView;14 15 @property (weak, nonatomic) IBOutlet UIBarButtonItem *backButton;16 @property (weak, nonatomic) IBOutlet UIBarButtonItem *forwardButton;17 - (IBAction)back:(UIBarButtonItem *)sender;18 - (IBAction)forward:(UIBarButtonItem *)sender;19 20 @end21 22 @implementation ViewController23 24 - (void)viewDidLoad {25 [super viewDidLoad];26 // Do any additional setup after loading the view, typically from a nib.27 28 // 建立一個搜尋欄29 UISearchBar *searchBar = [[UISearchBar alloc] init];30 searchBar.frame = CGRectMake(0, 0, 300, 40);31 searchBar.delegate = self;32 33 self.navigationItem.titleView = searchBar;34 35 self.webView.delegate = self;36 }37 38 39 40 - (IBAction)back:(UIBarButtonItem *)sender {41 NSLog(@"後退");42 [self.webView goBack];43 }44 45 - (IBAction)forward:(UIBarButtonItem *)sender {46 NSLog(@"前進");47 [self.webView goForward];48 }49 50 #pragma mark - UIWebViewDelegate51 - (void)webViewDidStartLoad:(UIWebView *)webView {52 NSLog(@"開始載入");53 }54 55 - (void)webViewDidFinishLoad:(UIWebView *)webView {56 NSLog(@"完成載入");57 58 // 設定後退、前進按鈕59 if ([self.webView canGoBack]) {60 self.backButton.enabled = YES;61 } else {62 self.backButton.enabled = NO;63 }64 65 if ([self.webView canGoForward]) {66 self.forwardButton.enabled = YES;67 } else {68 self.forwardButton.enabled = NO;69 }70 }71 72 #pragma mark - UISearchBarDelegate73 /** 點擊搜尋按鈕(虛擬鍵盤上) */74 - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {75 NSString *str = searchBar.text;76 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", str]];77 NSURLRequest *request = [NSURLRequest requestWithURL:url];78 79 [self.webView loadRequest:request];80 }81 82 @end 3.載入本地資源html、txt、doc、ppt、MP4、pdf直接請求即可 4.自動識別電話、郵箱、地址 // 識別所有類型的資料 self.webView.dataDetectorTypes = UIDataDetectorTypeAll; B.其他用途1.OC執行javascript1 /** 運行js代碼 */2 - (IBAction)runJs {3 NSString *str = @"alert('hello, world!');";4 [self.webView stringByEvaluatingJavaScriptFromString:str];5 } 2.javascript調用OC在javascript中發送網頁請求,使用OC截取,根據內容判斷調用相應方法 這裡使用js代碼呼出相簿選擇: 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>運行OC代碼</title> 8 </head> 9 <body>10 11 <script type="text/javascript">12 function runOC() {13 window.location.href="ios://openAlbum";14 }15 </script>16 17 <input type="button" value="運行OC代碼" onclick="runOC();" />18 19 </body>20 </html> OC代碼:
1 /** webView發送請求之前都會調用,可以攔截請求,選擇載入(返回YES)還是不載入(返回NO)*/ 2 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { 3 4 // 攔截請求 5 NSString *urlStr = request.URL.absoluteString; 6 NSRange range = [urlStr rangeOfString:@"ios://"]; 7 8 // 如果是ios請求 9 if (range.length != 0) {10 // 方法名11 NSString *method = [urlStr substringFromIndex:range.location + range.length];12 13 // 封裝SEL14 SEL selector = NSSelectorFromString(method);15 16 // 執行方法17 [self performSelector:selector withObject:nil];18 }19 20 return YES;21 }22 23 /** 開啟相簿 */24 - (void) openAlbum {25 // 建立一個相片選擇控制器26 UIImagePickerController *imagePC = [[UIImagePickerController alloc] init];27 [imagePC setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; // 使用相簿來源28 29 [self presentViewController:imagePC animated:YES completion:^{30 NSLog(@"呼出相簿");31 }];32 }