UIWebView是iOS sdk中一個(gè)最常用的控件。是內(nèi)置的瀏覽器控件乡范,我們可以用它來瀏覽網(wǎng)頁、打開文檔等等啤咽,UIWebView能夠加載html/htm晋辆、pdf、docx宇整、txt等格式的文件
UIWebView:->UIView同時(shí)遵守了滾動(dòng)的協(xié)議
loadRequest:加載請(qǐng)求
NSURLRequest:請(qǐng)求類用于設(shè)置網(wǎng)絡(luò)請(qǐng)求相關(guān)信息如:網(wǎng)址瓶佳、傳的內(nèi)容、請(qǐng)求頭...
NSURL:路徑每個(gè)內(nèi)容都有一個(gè)在萬維網(wǎng)中唯一的路徑
掛代理:<UIWebViewDelegate>
1.在viewDidLoad里面
self.edgesForExtendedLayout=UIRectEdgeNone;
myWebView= [[UIWebViewalloc]initWithFrame:[UIScreenmainScreen].bounds];
myWebView.delegate=self;
myWebView.scalesPageToFit=YES;
myWebView.allowsInlineMediaPlayback=YES;
[self.viewaddSubview:myWebView];
self.navigationItem.leftBarButtonItem= [[UIBarButtonItemalloc]initWithTitle:@"返回"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(gotoBack)];
self.navigationItem.rightBarButtonItem= [[UIBarButtonItemalloc]initWithTitle:@"下一頁"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(next)];
indicatorView= [[UIActivityIndicatorViewalloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
indicatorView.center=self.view.center;
[self.viewaddSubview:indicatorView];
NSString*path = [[NSBundlemainBundle]pathForResource:@"123"ofType:@"pdf"];
[selfloadWithURL:[NSURLURLWithString:@"http://www.chinacaipu.com/menu/dianxinshipin/118481.html"]];
- (void)loadWithURL:(NSURL*)url{
NSURLRequest*request = [NSURLRequestrequestWithURL:url];
[myWebViewloadRequest:request];?}
- (void)gotoBack{
[myWebViewgoBack];?}
- (void)next{
[myWebViewgoForward];?}
開始加載請(qǐng)求
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType{
[indicatorViewstartAnimating];?returnYES;?}
加載完成
- (void)webViewDidFinishLoad:(UIWebView*)webView{
[indicatorViewstopAnimating];?}
加載失敗
- (void)webView:(UIWebView*)webView didFailLoadWithError:(nullableNSError*)error{
if([errorisEqual:[NSNullnull]] !=YES) {
UIAlertAction*reloadAction = [UIAlertActionactionWithTitle:@"重新加載"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {
[selfloadWithURL:[NSURLURLWithString:@"https://www.baidu.com"]];?}];
UIAlertAction*cancelAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction*_Nonnullaction) {?}];
NSString*errorMessage =@"";
if(error.code== -1009) {
errorMessage =@"網(wǎng)絡(luò)連接錯(cuò)誤";?}
UIAlertController*alert = [UIAlertControlleralertControllerWithTitle:@"加載錯(cuò)誤"message:errorMessagepreferredStyle:UIAlertControllerStyleAlert];
[alertaddAction:reloadAction];
[alertaddAction:cancelAction];
[selfpresentViewController:alertanimated:YEScompletion:nil];
NSLog(@"%@",error.userInfo);?}}
前往下一頁:goForward
self.navigationItem.rightBarButtonItem= [[UIBarButtonItemalloc]initWithTitle:@"下一頁"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(next)];
返回上一頁:goBack?
self.navigationItem.leftBarButtonItem= [[UIBarButtonItemalloc]initWithTitle:@"返回"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(gotoBack)];
scalesPageToFit讓加載的內(nèi)容自適應(yīng)設(shè)備
webView可以通過代理方法檢測(cè)加載內(nèi)容的狀態(tài)
// 是否允許播放內(nèi)鏈視頻 在手機(jī)上默認(rèn)是NO
myWebView.allowsInlineMediaPlayback=YES;
代理方法
網(wǎng)頁開始加載的時(shí)候調(diào)用
-(void)webViewDidStartLoad:(UIWebView *)webView
網(wǎng)頁加載完成的時(shí)候調(diào)用
-(void)webViewDidFinishLoad:(UIWebView *)webView
網(wǎng)頁加載出錯(cuò)的時(shí)候調(diào)用
-(void)webView:(UIWebView )webView didFailLoadWithError:(NSError )error
網(wǎng)頁中的每一個(gè)請(qǐng)求都會(huì)被觸發(fā)這個(gè)方法鳞青,返回NO代表不執(zhí)行這個(gè)請(qǐng)求(常用于JS與iOS之間通訊)
-(BOOL)webView:(UIWebView )webView shouldStartLoadWithRequest:(NSURLRequest )request navigationType:(UIWebViewNavigationType)navigationType