UIWebView

作者: 无邪8 | 来源:发表于2018-05-21 11:18 被阅读13次

    一、简介

    <<UIWebView是iOS内置的浏览器控件,继承自UIView,遵循UIScrollViewDelegate协议,方便开发者监听滚动事件。

    <<继承关系:UIWebView-->UIView-->UIResponder-->NSObject

    格式为

    1-->初始化(作用)

    typedef NS_OPTIONS(NSUInteger, UIDataDetectorTypes) {

    UIDataDetectorTypePhoneNumber                                        = 1 << 0, //检测电话

    UIDataDetectorTypeLink                                              = 1 << 1, //检测网址和邮箱

    UIDataDetectorTypeAddress NS_ENUM_AVAILABLE_IOS(4_0)                = 1 << 2, // 检测地址

    UIDataDetectorTypeCalendarEvent NS_ENUM_AVAILABLE_IOS(4_0)          = 1 << 3, // 检测日历

    UIDataDetectorTypeShipmentTrackingNumber NS_ENUM_AVAILABLE_IOS(10_0) = 1 << 4, // 货物追踪号码检测

    UIDataDetectorTypeFlightNumber NS_ENUM_AVAILABLE_IOS(10_0)          = 1 << 5, // 班机号码检测

    UIDataDetectorTypeLookupSuggestion NS_ENUM_AVAILABLE_IOS(10_0)      = 1 << 6, //用户可能要查找的信息

    UIDataDetectorTypeNone          = 0,              // 禁用检测

    UIDataDetectorTypeAll          = NSUIntegerMax   // 检测所有类型链接

    } __TVOS_PROHIBITED;

    ;(如果属性有枚举类型的话,这里会有枚举类型说明

    self.webView.dataDetectorTypes =UIDataDetectorTypeAll;(这是具体的例子

    @property (nonatomic) UIDataDetectorTypes dataDetectorTypes NS_AVAILABLE_IOS(3_0);// 设置提示文字    (这是说明

    二、UIWebView的文本属性(属性的顺序与苹果API一致)

    1-->设置UIWebView Delegate代理

    webView.delegate = self;//设置UIWebView Delegate代理

    @property(nullable,nonatomic,weak) id<UIWebViewDelegate> delegate; //弱引用

    2-->内置的scrollView

    UIScrollView*scrollView = webView.scrollView;

    @property (nonatomic, readonly, strong) UIScrollView *scrollView NS_AVAILABLE_IOS(5_0);//与webview关联的滚动视图对象,通过该对象去处理webview的滚动行为

    3-->通过NSURLRequest去加载html界面

    NSURLRequest request =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.superqq.com"]];

    [self.view addSubview:webView];

    [webView loadRequest:request];

    - (void)loadRequest:(NSURLRequest *)request;

    4-->一般用来加载本地的html界面

    NSString *resourcePath = [[NSBundle mainBundle] resourcePath];

        NSString *filePath = [resourcePath stringByAppendingPathComponent:@"mobile.html"];

        NSString *htmlstring=[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

        [webView loadHTMLString:htmlstring baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];

    - (void)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL;//这个方法需要将httml文件读取为字符串,其中baseURL是我们自己设置的一个路径,用于寻找html文件中引用的图片等素材。

    5-->设置页面的内容、mimeType、encoding以及baseURL

    NSString *thePath = [[NSBundle mainBundle] pathForResource:@"iPhone_User_Guide" ofType:@"pdf"];

        if (thePath) {

            NSData *pdfData = [NSData dataWithContentsOfFile:thePath];

            [(UIWebView *)self.view loadData:pdfData MIMEType:@"application/pdf"                        textEncodingName:@"utf-8" baseURL:nil];

        }

    - (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL;//data是文件数据,MIMEType是文件类型,textEncodingName是编码类型,baseURL是素材资源路径。

    6-->当前webview加载内容位置的url请求

    NSURLRequest* request =webView.request;

    @property (nullable, nonatomic, readonly, strong) NSURLRequest *request;

    7-->重新加载当前页面

    [webView reload];

    - (void)reload;

    8-->停止加载任何主帧或子帧正在加载内容,如果没有内容加载,则什么也不做

    [webView stopLoading];

    - (void)stopLoading;

    9-->回退到history中的上个web页面

    [webView goBack];

    - (void)goBack;

    10-->前进到history中的下个页面

    [webView goForward];

    - (void)goForward;

    11-->是否可以回退

    Bool  isGoBack=[webView canGoBack];

    @property (nonatomic, readonly, getter=canGoBack) BOOL canGoBack;

    12-->是否可以前进

    Bool  canGoForward=[webView canGoForward];

    @property(nonatomic,readonly,getter=canGoForward)BOOL canGoForward;//能否跳转下一级

    13-->是否正在加载

    Bool  loading=[webView loading];

    @property (nonatomic, readonly, getter=isLoading) BOOL loading;

    14-->通过javaScript操作web数据

    NSString *docStr=[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.textContent"];

    - (nullable NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;

    15-->设置是否缩放到适合屏幕大小

    webView.scalespageToFit = YES;//自动对页面进行缩放以适应屏幕 

    @property (nonatomic) BOOL scalesPageToFit;

    16-->自动检测网页上的电话号码,单击可以拨打

    webView.detectsPhoneNumbers = YES;

    @property (nonatomic) BOOL detectsPhoneNumbers NS_DEPRECATED_IOS(2_0, 3_0);

    17-->设置某些数据变为链接形式,这个枚举可以设置如电话号,地址,邮箱等转化为链接。

    typedef NS_OPTIONS(NSUInteger, UIDataDetectorTypes) {

    UIDataDetectorTypePhoneNumber                                        = 1 << 0, //检测电话

    UIDataDetectorTypeLink                                              = 1 << 1, //检测网址和邮箱

    UIDataDetectorTypeAddress NS_ENUM_AVAILABLE_IOS(4_0)                = 1 << 2, // 检测地址

    UIDataDetectorTypeCalendarEvent NS_ENUM_AVAILABLE_IOS(4_0)          = 1 << 3, // 检测日历

    UIDataDetectorTypeShipmentTrackingNumber NS_ENUM_AVAILABLE_IOS(10_0) = 1 << 4, // 货物追踪号码检测

    UIDataDetectorTypeFlightNumber NS_ENUM_AVAILABLE_IOS(10_0)          = 1 << 5, // 班机号码检测

    UIDataDetectorTypeLookupSuggestion NS_ENUM_AVAILABLE_IOS(10_0)      = 1 << 6, //用户可能要查找的信息

    UIDataDetectorTypeNone          = 0,              // 禁用检测

    UIDataDetectorTypeAll          = NSUIntegerMax   // 检测所有类型链接

    } __TVOS_PROHIBITED;

    self.webView.dataDetectorTypes =UIDataDetectorTypeAll;

    @property (nonatomic) UIDataDetectorTypes dataDetectorTypes NS_AVAILABLE_IOS(3_0);

    18-->设置是否使用内联播放器播放视频

    webView.allowsInlineMediaPlayback=YES;

    @property (nonatomic) BOOL allowsInlineMediaPlayback NS_AVAILABLE_IOS(4_0); // iPhone Safari defaults to NO. iPad Safari defaults to YES

    19-->设置视频是否自动播放

    webView.mediaPlaybackRequiresUserAction=YES;

    @property (nonatomic) BOOL mediaPlaybackRequiresUserAction NS_AVAILABLE_IOS(4_0); // iPhone and iPad Safari both default to YES

    20-->设置音频播放是否支持ari play功能

    webView.mediaPlaybackAllowsAirPlay=YES;

    @property (nonatomic) BOOL mediaPlaybackAllowsAirPlay NS_AVAILABLE_IOS(5_0); // iPhone and iPad Safari both default to YES

    21-->设置是否将数据加载如内存后渲染界面

    webView.suppressesIncrementalRendering=YES;

    @property (nonatomic) BOOL suppressesIncrementalRendering NS_AVAILABLE_IOS(6_0); // iPhone and iPad Safari both default to NO4-->UITextView的一个类方法

    22-->设置用户交互模式

    webView.keyboardDisplayRequiresUserAction=YES;

    @property (nonatomic) BOOL keyboardDisplayRequiresUserAction NS_AVAILABLE_IOS(6_0); // default is YES

    23-->设置一种模式,当网页的大小超出view时,将网页以翻页的效果展示

    typedef NS_ENUM(NSInteger, UIWebPaginationMode) {

    UIWebPaginationModeUnpaginated,//不使用翻页效果

    UIWebPaginationModeLeftToRight,//将网页超出部分分页,从左向右进行翻页

    UIWebPaginationModeTopToBottom,//将网页超出部分分页,从上向下进行翻页

    UIWebPaginationModeBottomToTop,//将网页超出部分分页,从下向上进行翻页

    UIWebPaginationModeRightToLeft//将网页超出部分分页,从右向左进行翻页

    };

    webView.paginationMode=UIWebPaginationModeRightToLeft;

    @property (nonatomic) UIWebPaginationMode paginationMode NS_AVAILABLE_IOS(7_0);

    24-->决定了webView加载页面具有CSS属性时是使用页的样式还是以列的样式

    typedefNS_ENUM(NSInteger, UIWebPaginationBreakingMode) {

    UIWebPaginationBreakingModePage,//默认设置是这个属性,CSS属性以页样式。

    UIWebPaginationBreakingModeColumn//当UIWebPaginationBreakingMode设置这个属性的时候,这个页面内容CSS属性以column-break 代替page-breaking样式。

    };

    webView.paginationBreakingMode=UIWebPaginationBreakingModeColumn;

    @property (nonatomic) UIWebPaginationBreakingMode paginationBreakingMode NS_AVAILABLE_IOS(7_0);

    25-->分页的长度

    webView.pageLength=100;

    @property (nonatomic) CGFloat pageLength NS_AVAILABLE_IOS(7_0);

    26-->设置每一页的间距

    webView.gapBetweenPages=10;

    @property (nonatomic) CGFloat gapBetweenPages NS_AVAILABLE_IOS(7_0);

    27-->获取分页数

    NSUInteger  count=webView.pageCount;

    @property (nonatomic, readonly) NSUInteger pageCount NS_AVAILABLE_IOS(7_0);

    28-->分屏中是否播放

    webView.allowsPictureInPictureMediaPlayback=YES;

    @property (nonatomic) BOOL allowsPictureInPictureMediaPlayback NS_AVAILABLE_IOS(9_0);//如果你使用WebKit支持PiP功能,但要退出特定视频的画中画。设定关联web

    view 实例的allowsPictureInPictureMediaPlayback属性为NO

    29-->长按链接是否支持预览(支持3D Touch的设备)

    webView.allowsLinkPreview=YES;

    @property (nonatomic) BOOL allowsLinkPreview NS_AVAILABLE_IOS(9_0); // default is NO

    三、webView协议中的方法

    1-->UIWebView的代理方法,当webview开始加载请求的时候回去调用,可以拦截一些用户的行为是否加载。返回YES代表允许加载,返回NO代表停止加载

    typedefNS_ENUM(NSInteger,UIWebViewNavigationType) {UIWebViewNavigationTypeLinkClicked,//用户触发了一个链接UIWebViewNavigationTypeFormSubmitted,//用户提交了一个表单UIWebViewNavigationTypeBackForward,//用户触击前进前进或返回按钮UIWebViewNavigationTypeReload,//用户触击重新加载的按钮UIWebViewNavigationTypeFormResubmitted,//用户重复提交表单UIWebViewNavigationTypeOther//发生了其他行为

    };

    -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

    {

    if (navigationType==UIWebViewNavigationTypeLinkClicked) {

    return NO;

    }

    else {

    return YES;

    }

    }

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;

    2-->webView加载开始的时候调用

    - (void)webViewDidStartLoad:(UIWebView *)webView

    {

    // starting the load, show the activity indicator in the status bar

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    }

    - (void)webViewDidStartLoad:(UIWebView *)webView;

    3-->webView加载完毕的时候调用

    - (void)webViewDidFinishLoad:(UIWebView *)webView

    {

    // finished loading, hide the activity indicator in the status bar

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    }

    - (void)webViewDidFinishLoad:(UIWebView*)webView;

    4-->当在请求加载中发生错误时,得到通知。会提供一个NSSError对象,以标识所发生错误类型。

    - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

    {

    // load error, hide the activity indicator in the status bar

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    // report the error inside the webview

    NSString* errorString = [NSString stringWithFormat:

    @"An error occurred:

    %@",

    error.localizedDescription];

    [self.myWebView loadHTMLString:errorString baseURL:nil];

    }

    - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;

    参考链接

    IOS之UIWebView的使用

    iOS中UIWebView的使用详解

    UIWebView详解

    UIWebView全部API学习

    相关文章

      网友评论

        本文标题:UIWebView

        本文链接:https://www.haomeiwen.com/subject/cnnwdftx.html