iOS界面编程-UIWebView

一、介绍

      使用UIWebView类在你的应用中嵌入web内容,只需要简单的三个步骤,第一步创建一个UIWebView对象,第二步把UIWebView与一个window联系起来,第三步,发送一个请求去加载web内容。你也能使用UIWebView向前、向后加载历史web页面。你甚至可以通过编程方式设置一些Web内容的属性。

     使用loadRequest:方法开始加载网页内容,stopLoading方法去停止加载,loading属性去查看一个webview 是否在加载中。

     如果你允许用户前进和回退历史浏览页面,这时你可以为按钮动作使用goBack和goForward方法去操作。也可以设置canGoBack和canGoForward 布尔属性去控制用户是否能前进否则回退操作。

    默认的,web view自动把电话号码成一个电话链接的形式。当一个电话连接被点击后,手机应用就会启动然后拨打号码。可以把detectsPhoneNumbers属性设置成NO去关掉这个默认的行为。

    你可以使用scalesPageToFit属性去设程序化置网页内容的缩放,设置后用户可以使用手势去改变缩放。

    如果你想跟踪网页加载的内容,可以给对象去设置委托属性,去实现UIWebViewDelegate协议,


注意:

   你不应该在UIScrollView对象中嵌入UIWebView或者UITableView对象。如果你这么做,可能会导致一些不可预期的行为,这是因为两个对象的混合和错误处理。

   UIWebView类不应该被子类化。   


二、相关属性和方法

@property(nonatomic,assign)id<UIWebViewDelegate>delegate 

当网页内容加载时,会给委托消息发送对象。在你在指定了UIWebView委托对象中释放UIWebView时候,你必须先把委托属性设置为nil。例如这可以在你的dealloc方法中去实现。

- (void)loadData:(NSData * nonnull)data   MIMEType:(NSString * nonnull)MIMEType  textEncodingName:(NSString * nonnull)encodingName
         baseURL:(NSURL * nonnull)baseURL

     设置主页的内容,文本格式,内容编码以及基础URL

- (void)loadHTMLString:(NSString * nonnull)string    baseURL:(NSURL * nullable)baseURL

    设置主页内容和基本的URL

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

    通过初始化一个异步客户端请求去连接一个给定的URL地址

@property(nonatomic,readonly, strong)NSURLRequest *request  一个只读属性,URL请求属性,标记加载网页的位置。

@property(nonatomic,readonly, getter=isLoading)BOOL loading  指示接收器是否正在加载页面内容

- (void)stopLoading  停止加载

- (void)reload 重新加载当前页面

@property(nonatomic,readonly, getter=canGoBack)BOOL canGoBack  布尔只读变量,判断是否能后退

@property(nonatomic,readonly, getter=canGoForward)BOOL canGoForward  布尔只读变量,判断是否能前进

- (void)goBack后退页面

- (void)goForward 前进页面

@property(nonatomic)BOOL scalesPageToFit   布尔值决定是否网页内容缩放去适配视图,用户可以改变这个缩放

@property(nonatomic,readonly, strong)UIScrollView *scrollView 只读属性,获取与UIWebView相关的scrollview

@property(nonatomic)BOOL suppressesIncrementalRendering 一个布尔值指示web view是否抑制内容渲染直到被充分加载到内存中。

@property(nonatomic)BOOL keyboardDisplayRequiresUserAction 布尔值指示键盘显示是否需要用户点击激活

- (NSString * nullable)stringByEvaluatingJavaScriptFromString:(NSString * nonnull)script  返回一个运行script的结果


检测数据类型

@property(nonatomic)UIDataDetectorTypes dataDetectorTypes  把web view的内容数据类型转换成可点击的URL

@property(nonatomic)BOOL allowsInlineMediaPlayback

@property(nonatomic)BOOL mediaPlaybackRequiresUserAction

@property(nonatomic)BOOL mediaPlaybackAllowsAirPlay

@property(nonatomic)CGFloat gapBetweenPages

@property(nonatomic,readonly) NSUInteger pageCount

@property(nonatomic)CGFloat pageLength

enum {

   UIWebViewNavigationTypeLinkClicked,

   UIWebViewNavigationTypeFormSubmitted,

   UIWebViewNavigationTypeBackForward,

   UIWebViewNavigationTypeReload,

   UIWebViewNavigationTypeFormResubmitted,

   UIWebViewNavigationTypeOther

};

typedef NSUIntegerUIWebViewNavigationType;

三、实际例子


  
    webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, self.view.bounds.size.height-10)];
  
    NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.sina.com"]];
    [self.view addSubview: webView];
    [webView loadRequest:request];
    webView.delegate = self;
    
    activityView = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(150, 200, 20, 20)];
    activityView.activityIndicatorViewStyle =UIActivityIndicatorViewStyleWhiteLarge;
    activityView.hidesWhenStopped= YES;
    [self.view addSubview:activityView];
    UIToolbar  *bar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 60, self.view.bounds.size.width, 44)];
    [self.view addSubview:bar];
    
    bar.barStyle = UIBarStyleBlack;
    
    UIBarButtonItem *tabBarItem1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:@selector(backClicked:)];
    tabBarItem1.tag = URL_BACK;
    
    UIBarButtonItem *tabBarItem2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:@selector(backClicked:)];
    tabBarItem2.tag = URL_FORWARD;
    
     UIBarButtonItem *tabBarItem3 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(backClicked:)];
      tabBarItem3.tag = URL_REFRESH;
    
    UIBarButtonItem *tabBarItem4 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:@selector(backClicked:)];
    
    
    UIBarButtonItem *flexibleButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    NSArray *tabBarItemArray = [[NSArray alloc] initWithObjects: tabBarItem1, tabBarItem4, tabBarItem2,flexibleButton,tabBarItem3 ,nil];
    [bar setItems: tabBarItemArray];


-(void)backClicked:(UIBarButtonItem *)sender{
    switch (sender.tag) {
        case URL_BACK:
            if (webView.canGoBack) {
                [webView goBack];
            }
            break;
        case URL_FORWARD:
            if (webView.canGoForward) {
                [webView goForward];
            }
            break;
        default:
            [webView reload];
            break;
    }
    
}
#pragma mark -UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    return  YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
    [activityView startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [activityView stopAnimating];

}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error{
    [activityView stopAnimating];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

house.zhang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值