转自:http://blog.csdn.net/qq_34195670/article/details/53306559
在iOS开发中好多时候,原生的app会很大的限制,也会有好多难以实现的东西。那就需要与H5进行混合开发,那就使用到了webView。
一、首先简单介绍一下webView:
1、三种加载方式:
- (void)loadRequest:(NSURLRequest *)request;- (void)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL;- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL;
2、一些常用的属性和变量
@property (nonatomic,assign)id <UIWebViewDelegate> delegate;@property (nonatomic,readonly,retain)UIScrollView *scrollView;@property (nonatomic,readonly,retain)NSURLRequest *request;- (void)reload;- (void)stopLoading;- (void)goBack;- (void)goForward;@property (nonatomic,readonly,getter=canGoBack)BOOL canGoBack;@property (nonatomic,readonly,getter=canGoForward)BOOL canGoForward;@property (nonatomic,readonly,getter=isLoading)BOOL loading;- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;@property (nonatomic)BOOL scalesPageToFit;@property (nonatomic)UIDataDetectorTypes dataDetectorTypesNS_AVAILABLE_IOS(3_0);@property (nonatomic)BOOL allowsInlineMediaPlaybackNS_AVAILABLE_IOS(4_0);@property (nonatomic)BOOL mediaPlaybackRequiresUserActionNS_AVAILABLE_IOS(4_0);@property (nonatomic)BOOL mediaPlaybackAllowsAirPlayNS_AVAILABLE_IOS(5_0);@property (nonatomic)BOOL suppressesIncrementalRenderingNS_AVAILABLE_IOS(6_0);@property (nonatomic)BOOL keyboardDisplayRequiresUserActionNS_AVAILABLE_IOS(6_0);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
@property (nonatomic)UIWebPaginationMode paginationModeNS_AVAILABLE_IOS(7_0); typedef NS_ENUM(NSInteger, UIWebPaginationMode) { UIWebPaginationModeUnpaginated, UIWebPaginationModeLeftToRight, UIWebPaginationModeTopToBottom, UIWebPaginationModeBottomToTop, UIWebPaginationModeRightToLeft };@property (nonatomic)CGFloat pageLengthNS_AVAILABLE_IOS(7_0);@property (nonatomic)CGFloat gapBetweenPagesNS_AVAILABLE_IOS(7_0);@property (nonatomic,readonly)NSUInteger pageCountNS_AVAILABLE_IOS(7_0);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
3、webView协议中的方法
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;- (void)webViewDidStartLoad:(UIWebView *)webView;- (void)webViewDidFinishLoad:(UIWebView *)webView;- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;
以上就是webView的属性、方法以及代理方法的简单解释。
二、简单给出一个例子:(加载一个京东的界面),直接上代码:
self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)] self.webView.delegate = self [self.view addSubview:self.webView] NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.jd.com"]] [self.webView loadRequest:request]
这里webView的加载请求,也可以像平时做网络请求一样进行设置“GET”或“POST”请求,这需要根据后台的需求。例如一个是用post请求的例子:
注:这里的HTTP_AFNETWORKING_POST_URL
和 URL_Detail_H5([userManage key], _record_type, _record_id)
都是实际项目中的宏定义。
_webView.frame = CGRectMake(0, 0, SCREEN_W, 0); _webView.delegate = self; NSURL *url = [NSURL URLWithString: HTTP_AFNETWORKING_POST_URL]; NSString *body = URL_Detail_H5([userManage key], _record_type, _record_id); NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url]; [request setHTTPMethod: @"POST"]; [request setHTTPBody: [body dataUsingEncoding: NSUTF8StringEncoding]]; [self.webView loadRequest:request];
三、很多时候,webView加载了H5界面之后,如果H5界面过长,webView能进行滚动,但是有时需要进行让webView自适应,自适应的方法如下:
在网页加载完成的时候,使用stringByEvaluatingJavaScriptFromString实现UIWebView与JavaScript之间的交互,很方便的操作UIWebview中的页面元素。获取Html的高度。
虽然这个方法能使webView高度为显示的HTML实际高度,但效果并不是太好,当高度超出屏幕时,无法滚动下拉。但是这种方法与scrollview一起使用可以很好的控制滚动。
-(void)webViewDidFinishLoad:(UIWebView *)webView{ NSInteger height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] integerValue] self.webView.frame=CGRectMake(0, 0, self.view.frame.size.width,height)}
或者使用监听:
[_webView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil]; - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ if ([keyPath isEqualToString:@"contentSize"]) { NSInteger height = [[_webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] integerValue]; self.webView.frame=CGRectMake(0, 0, self.view.frame.size.width,height); [self setUILayout]; }}
四、最后说与网页的交互。
很多时候我们需要监听用户点击了网页上的内容我们进行相应的跳转到原生的界面上,这里就使用到了webView代理方法中的
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
这个方法。网页中的每一个请求都会被触发这个方法,返回NO代表不执行这个请求(常用于JS与iOS之间通讯)。
还是以京东首页为例子,首先load一个京东界面,然后监听京东界面的京东超市那个请求,当发出这个请求,然后跳转到我们自己的原生界面。
效果图:
直接上代码:
#import "ViewController.h"#import "aaa.h"@interface ViewController ()<UIWebViewDelegate>@property (strong, nonatomic) UIWebView *webView;@property (strong, nonatomic) UIActivityIndicatorView *activityIndicatorView;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; self.webView.delegate = self; [self.view addSubview:self.webView]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.jd.com"]]; [self.webView loadRequest:request]; self.activityIndicatorView = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 32, 32)]; self.activityIndicatorView.center = self.view.center; [self.activityIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge]; [self.view addSubview:self.activityIndicatorView];}#pragma mark - **************** delegate-(void)webViewDidStartLoad:(UIWebView *)webView{ NSLog(@"webViewDidStartLoad"); [self.activityIndicatorView startAnimating];}-(void)webViewDidFinishLoad:(UIWebView *)webView{ NSLog(@"webViewDidFinishLoad"); [self.activityIndicatorView stopAnimating];}-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{ if (error != nil) { NSLog(@"%@",[error description]); } [self.activityIndicatorView stopAnimating];}- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSLog(@"ssss%@",request); if ([request.URL.absoluteString isEqualToString:@"http://pro.m.jd.com/2hqsQcyM5bEUVSStkN3BwrBHqVLd/index.html"] ) { aaa *aa = [[aaa alloc]init]; [self presentViewController:aa animated:YES completion:nil]; return NO; } return YES;}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; }@end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
以上就是对webView的简单实用的总结。如有错误,请留言指出,将感激不尽。
0 0