zoukankan      html  css  js  c++  java
  • [IOS]UIWebView 请求网络页面或者加载本地资源页面

    UIWebView是一个能够显示网页的IOS视图控件,我们可以用它来访问一个网站。下面是具体的实例:

    操作步骤:

    1.首先在xib文件中拖放一个UIWebView控件到view中

    2.将下载的页面以及页面资源加载到项目中,但必须选择Create folder references for any added folders,然后知道文件在项目中是蓝色显示,而不是黄色显示

    3.将webView的Delegate拖到File's Owner,继承UIWebView的Delegate协议,并且实现他的协议


    ViewController.h:

    #import <UIKit/UIKit.h>
    
    @interface DXWViewController : UIViewController<UIWebViewDelegate>
    @property (retain, nonatomic) IBOutlet UIWebView *webview;
    @property(nonatomic,retain) UIAlertView *alert;
    @end

    ViewController.m:

    #import "DXWViewController.h"
    
    @interface DXWViewController ()
    
    @end
    
    @implementation DXWViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    	NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        //[self.webview loadRequest:request];
        
        //加载本地资源,html页面
        NSString *str = [[NSBundle mainBundle] pathForResource:@"百度图片—全球最大中文图片库" ofType:@"html"];
        
        str = [NSString stringWithContentsOfFile:str encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"%@",str);
        [self.webview loadHTMLString:str baseURL:[[NSBundle mainBundle] bundleURL]];
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    - (void)dealloc {
        [_webview release];
        [_alert release];
        [super dealloc];
    }
    
    -(void)webViewDidFinishLoad:(UIWebView *)webView
    {
        [self.alert dismissWithClickedButtonIndex:0 animated:YES];
    }
    
    -(void)webViewDidStartLoad:(UIWebView *)webView
    {
        self.alert = [[UIAlertView alloc] initWithTitle:@"Loading..." message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
        [self.alert show];
        
        UIActivityIndicatorView *aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        aiv.center = CGPointMake(self.alert.bounds.size.width/2, self.alert.bounds.size.height/2);
        [aiv startAnimating];
        [self.alert addSubview:aiv];
    }
    
    @end


  • 相关阅读:
    Java并发实现一(并发的实现之Thread和Runnable的区别)
    Java中的enum
    Eclipse+Maven创建webapp项目
    手机上最简洁的"云笔记"软件
    工具与艺术的结合:浅谈博客的排版规范与样式设计
    页面定制CSS代码初探(四):cnblogs使用Github引用样式
    脑图工具MindNode"附属节点"是什么意思 图解
    页面定制CSS代码初探(三):设置正文最小高度
    Sublime 是自动检测而非自动设置缩进
    苹果操作系统名称演变史 新名称macOS
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3285607.html
Copyright © 2011-2022 走看看