UIWebView的基础使用
CGRect bouds = [[UIScreen manScreen]applicationFrame];
UIWebView* webView = [[UIWebView alloc]initWithFrame:bounds];
属性:
@property (nonatomic) BOOL scalesPageToFit; // 是否根据页面进行缩放展示 @property (nonatomic) BOOL detectsPhoneNumbers NS_DEPRECATED_IOS(2_0, 3_0); // 已经过时 @property (nonatomic) UIDataDetectorTypes dataDetectorTypes NS_AVAILABLE_IOS(3_0); // 页面展示,可以识别页面上的数字/地址
typedef NS_OPTIONS(NSUInteger, UIDataDetectorTypes) { UIDataDetectorTypePhoneNumber = 1 << 0, // Phone number detection UIDataDetectorTypeLink = 1 << 1, // URL detection UIDataDetectorTypeAddress NS_ENUM_AVAILABLE_IOS(4_0) = 1 << 2, // Street address detection UIDataDetectorTypeCalendarEvent NS_ENUM_AVAILABLE_IOS(4_0) = 1 << 3, // Event detection UIDataDetectorTypeShipmentTrackingNumber NS_ENUM_AVAILABLE_IOS(10_0) = 1 << 4, // Shipment tracking number detection UIDataDetectorTypeFlightNumber NS_ENUM_AVAILABLE_IOS(10_0) = 1 << 5, // Flight number detection UIDataDetectorTypeLookupSuggestion NS_ENUM_AVAILABLE_IOS(10_0) = 1 << 6, // Information users may want to look up UIDataDetectorTypeNone = 0, // Disable detection UIDataDetectorTypeAll = NSUIntegerMax // Enable all types, including types that may be added later }
UIWebView代理:
@protocol UIWebViewDelegate <NSObject> @optional - (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; // 页面加载失败 @end
如果页面上的链接需要进行拦截处理可以在一下方法中处理,通过返回值控制是否允许加载链接页面内容,YES:记载页面内容,NO:不允许;
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
/** webView代理 拦截webView请求 @param webView @param request @param navigationType @return */ - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ NSURL *requestURL =[request URL]; if (navigationType == UIWebViewNavigationTypeLinkClicked) { NSString absoluteStr = requestURL.absoluteString; // 根据自己的业务处理相关业务逻辑 } if ((navigationType == UIWebViewNavigationTypeLinkClicked )&&([[requestURL scheme] isEqualToString: @"http"] || [[requestURL scheme ] isEqualToString: @"https" ]
|| [[ requestURL scheme ] isEqualToString:@"mailto"])) { // Your code return ![[UIApplication sharedApplication ] openURL:requestURL]; } return YES; }
UIWebViewNavigationType枚举值
typedef NS_ENUM(NSInteger, UIWebViewNavigationType) { UIWebViewNavigationTypeLinkClicked, // 链接点击 UIWebViewNavigationTypeFormSubmitted, // 表单提交 UIWebViewNavigationTypeBackForward, // 触发回退或前进 UIWebViewNavigationTypeReload, // 重新加载按钮触发 UIWebViewNavigationTypeFormResubmitted, // 重复提交 UIWebViewNavigationTypeOther // 其他行为 } __TVOS_PROHIBITED;
让UIWebView更加接近Native
某些情况下,我们既想要UIWebView加载web页面,又想使得所加载的页面的外观和操作行为更加接近native感觉。这时需要使用一些CSS样式来达到这些效果,这些CSS只适用于IOS中的Safari。
- -webkit-touch-callout
禁用长按触控对象弹出的菜单。IOS中,当你长按一个触控对象时,如链接,safari会弹出包含链接信息的菜单。禁用此行为CSS代码
.disable-callout{ -webkit-touch-callout:none ; }
或在webViewDidFinisheLoad中使用
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
- -webkit-user-select
控制用户是否可以选择页面元素内容。IOS中,在页面元素中进行长按操作,safari会弹出菜单,来允许进行选择行为。禁用此行为代码
.disable-select{ -webkit-user-select:none; }
或在webViewDidFinisheLoad中使用
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
- -webkit-tap-highlight-color
覆盖当用户tap链接或clickable元素时默认产生的高亮颜色(灰色)。如
.no-highlight{ -webkit-tap-highlight-color:rgba(0,0,0,0); }
禁止UIWebView弹出alert对话框
方式是新建UIWebView的分类,然后重写三个方法,方法实现都为空,runJavaScriptConfirmPanelWithMessage返回YES.
#import "UIWebView+HJ.h" @implementation UIWebView (HJ) -(void)webView:(UIWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(id)frame { } - (BOOL)webView:(UIWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(id)frame { return YES; } - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { } @end