UIWebView-->iOS进行混合开发的桥梁,在iOS开发中常用于加载网页和html文件
现在对于混合式(传统模式与PC网页端相结合)移动端开发(开发成本低、速度快)越来越流行。在Android开发中有WebView作为混合模式开发的桥梁,在iOS中也同样有一个 UIWebView 组件来作为混合模式开发的桥梁。
1.初始化[继承自UIView]
继承自UIView,所以初始化方法类似UIView
eg:
UIWebView *webView = [[UIWebView alloc]initWithFrame:self.view.frame];
[self.view addSubview:webView];
2.加载数据[三种]
第一种:
- (void)loadRequest:(NSURLRequest *)request;
[webView loadRequest:[NSURLRequest requestWithURL:[NSUR URLWithString:@"http://www.baidu.com"]]];
这是加载网页最常用的方式,通过一个网页的url来进行加载,这个url可以是远程的,也可以是本地的。第二种:
- (void)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL;
第一个参数是读取html文件得到的字符串,第二个参数是获取html文件中引用图片等资源的路径第三种:
- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL;
用的比较少,但是自由度最高。参数依次是:文件数据、文件类型、编码类型、资源路径(同第二种)。
3.协议方法[UIWebViewDelegate]
//准备加载时调用(通过返回值判断是否调用)
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
//开始加载时调用
- (void)webViewDidStartLoad:(UIWebView *)webView;
//结束加载时调用
- (void)webViewDidFinishLoad:(UIWebView *)webView;
//加载失败时调用
- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error;
4.属性
@property (nullable, nonatomic, assign) id <UIWebViewDelegate> delegate;//代理
@property (nonatomic, readonly, strong) UIScrollView *scrollView NS_AVAILABLE_IOS(5_0);//内置的scrollView
@property (nullable, nonatomic, readonly, strong) NSURLRequest *request;//URL请求
@property (nonatomic, readonly, getter=canGoBack) BOOL canGoBack;//能否返回上一级
@property (nonatomic, readonly, getter=canGoForward) BOOL canGoForward;//能否进入下一级
@property (nonatomic, readonly, getter=isLoading) BOOL loading;//是否正在加载
@property (nonatomic) BOOL scalesPageToFit;//是否适应屏幕大小
@property (nonatomic) UIDataDetectorTypes dataDetectorTypes NS_AVAILABLE_IOS(3_0);//转链接,比较重要
@property (nonatomic) BOOL allowsInlineMediaPlayback NS_AVAILABLE_IOS(4_0);//是否使用内联播放器播放视频
@property (nonatomic) BOOL mediaPlaybackRequiresUserAction NS_AVAILABLE_IOS(4_0);//是否自动播放
@property (nonatomic) BOOL mediaPlaybackAllowsAirPlay NS_AVAILABLE_IOS(5_0);//是否支持air play
@property (nonatomic) BOOL suppressesIncrementalRendering NS_AVAILABLE_IOS(6_0);//是否加载数据后渲染界面
@property (nonatomic) BOOL keyboardDisplayRequiresUserAction NS_AVAILABLE_IOS(6_0);//直接翻译
@property (nonatomic) UIWebPaginationMode paginationMode NS_AVAILABLE_IOS(7_0);//网页翻页模式
@property (nonatomic) UIWebPaginationBreakingMode paginationBreakingMode NS_AVAILABLE_IOS(7_0);//
@property (nonatomic) CGFloat pageLength NS_AVAILABLE_IOS(7_0);//页面长度
@property (nonatomic) CGFloat gapBetweenPages NS_AVAILABLE_IOS(7_0);//间距
@property (nonatomic, readonly) NSUInteger pageCount NS_AVAILABLE_IOS(7_0);//页数
//下面这两好像与分屏有关,有朋友了解的给我留个言,感激不尽
@property (nonatomic) BOOL allowsPictureInPictureMediaPlayback NS_AVAILABLE_IOS(9_0);
@property (nonatomic) BOOL allowsLinkPreview NS_AVAILABLE_IOS(9_0);