对UIWebView的学习

建工程,建一个类WebViewController 继承于UIViewController

WebViewController设置为根视图控制器

WebViewController遵守UIWebViewDelegate协议以便网页视图加载或停止加载,加载出错时或执行什么方法,发生什么事件

WebViewController.m中代码

#import "WebViewController.h"

@interface WebViewController ()
{
    UIWebView *_webView;
    UITextField *_textField;
    UIActivityIndicatorView *_activityIndicatorView;
}
@end

@implementation WebViewController
-(void)dealloc
{
    [_webView release];
    [_textField release];
    [_activityIndicatorView release];
    [super dealloc];
}
//webView获取url地址
- (void)loadWebPageWithString:(NSString *)urlstring
{
    NSURL *url = [NSURL URLWithString:urlstring];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];
}
//点击按钮,网页加载
- (void)buttonPress:(id)sender
{
    [_textField resignFirstResponder];
    [self loadWebPageWithString:_textField.text];
}
//代理中的方法
-(void)webViewDidStartLoad:(UIWebView *)webView
{
    //网页加载时  进度轮开始启动
    [_activityIndicatorView startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    //网页加载完成  进度轮停止
    [_activityIndicatorView stopAnimating];
}
//请求页面出现错误:
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网址输入错误或网络断开" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alertView show];
    [alertView release];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 20, 270, 30)];
    _textField.borderStyle = UITextBorderStyleRoundedRect;
    //输入框默认显示http://www.baidu.com
    _textField.text = @"http://www.baidu.com";
    //点击button进入网页
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(280, 20, 30, 30);
    [button setTitle:@"GO" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
    //网页显示
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 50, 320, 425)];
    _webView.delegate = self;
    [self.view addSubview:_webView];
    [self.view addSubview:_textField];
    [self.view addSubview:button];
    //设置进度轮
    _activityIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
    //进度轮中心位置
    [_activityIndicatorView setCenter:self.view.center];
    //进度轮显示类型
    [_activityIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
    [self.view addSubview:_activityIndicatorView];
    //没有点击按钮时,执行方法进入默认显示网页
    [self buttonPress:nil];

    // Do any additional setup after loading the view.
}

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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

实例代码:LiSWebView.zip

时间: 2024-08-14 01:45:54

对UIWebView的学习的相关文章

《iOS Human Interface Guidelines》——Web View

Web视图 web视图是一个可以显示丰富的HTML内容的区域(如下iPhone上邮件app中导航栏和工具栏中间所显示的). API NOTE 查看UIWebView来学习更多关于在你的代码中定义web视图的内容. 一个web视图: 显示web内容 执行一些对web内容的自动处理,比如将一个电话号码转化成拨打电话 如果你有一个网页或者web app,你可能决定使用web视图来实现一个简单的为你的网页和web app提供外皮的iOS app.如果你计划使用web视图来链接你控制的web内容,一定要阅

ios网络学习------4 UIWebView的加载本地数据的三种方式

UIWebView是IOS内置的浏览器,可以浏览网页,打开文档  html/htm  pdf   docx  txt等格式的文件.  safari浏览器就是通过UIWebView做的. 服务器将MIME的标识符等放入传送的数据中告诉浏览器使用那种插件读取相关文件. uiwebview加载各种本地文件(通过loadData方法): - (void)viewDidLoad { [super viewDidLoad]; [self setupUI]; NSString *path = [[NSBund

iOS之 UIWebView的简单学习

UIWebView的简单学习 #import "ViewController.h" @interface ViewController ()<UIWebViewDelegate> { UIWebView *WebView; UIView *view; UIActivityIndicatorView *activityIndicator; } @end @implementation ViewController - (void)viewDidLoad { [super vi

关于学习是UIWebView的一些思考

前几天因为数据中加载有html语言的数据,关于html语言和UIWebView,有一些纠结,经过几天的研究,也有了一些自己的简单的见解. 我有两个页面需要加载html语言(注意,这里面的html不是从json解析出来的一段html语言,而是整个网站,然后用谷歌的开发者工具可接看到网站的源码),这段html语言显示的内容包括排版还是比较好的,所以我想直接把这个网站加载到我的程序上,但是由于是别的的数据,概览上面有一段我不想要的数据,滚动视图的详情页面有我很多不想要的数据,所以我就想到了把这段不想要

IOS开发-UI学习-UIWebView,简单浏览器的制作

制作一个简单的浏览器,包含网址输入框,Search按钮,前进.回退按钮,UIWebView就这几个简单的控件. UITextField:用来输入网址: UIbuttom:实现前进,后退,搜索等功能: UIWebView:实现网页展示. 准备工作: 右键Info.plist并Open As       Source Code,打开之后添加以下代码段: 1 <key>NSAppTransportSecurity</key> 2 <dict> 3 <key>NSA

UIWebView全部API学习。

最生僻的API做了下划线以及粗体的标注.百度上查了全是拷贝的同一份代码,而且只有代码没有解释,很是鄙视. //1. UIWebViewNavigationType枚举定义了页面中用户行为的分类 typedef NS_ENUM(NSInteger, UIWebViewNavigationType) { UIWebViewNavigationTypeLinkClicked,//用户触发了一个链接 UIWebViewNavigationTypeFormSubmitted,//用户提交了一个表单 UIW

Objective-C学习—UIWebView的使用

1.使用UIWebView加载网页 运行XCode 4.3,新建一个Single View Application,命名为WebViewDemo. 2.加载WebView 在ViewController.h添加WebView成员变量和在ViewController.m添加实现 [cpp] view plaincopy #import <UIKit/UIKit.h> @interface ViewController : UIViewController { UIWebView *webView

一步一步学习Swift之(四)玩转UIWebView

实现原理: 1.通过UIWebView的stringByEvaluatingJavaScriptFromString方法来触发脚本 2.通过自定义连接来触发oc代码 实现过程 @IBOutlet weak var webView: UIWebView! override func viewDidLoad() { super.viewDidLoad() webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://passport.cnbl

UIWebView v.s. WKWebView

从初学Objective-C到现在学习iOS开发已经快要一个半月了.最近刚刚学完基本的UIView控件部分,想着先做一个简单的浏览器app练练手. 为什么我要做一款浏览器app呢?我平时特喜欢用手机浏览器app上网,但使用主流浏览器发现有两个缺点: 现在的浏览器可视面积不够大.平时用手机app浏览网页时总觉得上边的状态栏和下边的工具栏太碍眼.尤其是横屏浏览时,本来就高度有限,这俩霸王又占去一大块地儿,最后就只留下中间窄窄的一个横条区域,更加不够用. 现在的浏览器没有针对大屏幕手机的操控进行优化.