ios 使用UIWebView创建web窗口

http://jordy.easymorse.com/?p=529

在应用程序开发经常会需要浏览网页,或者使用内嵌浏览器的方式访问网站,ios使用UIWebView控件可以实现这一需求,具体做法:

1、新建一个xcode项目,命名为Simple UIWebView

在Simple_UIWebViewViewController.h文件中定义一个UIWebView的输出口,代码:

#import <UIKit/UIKit.h>

@interface Simple_UIWebViewViewController : UIViewController {

    IBOutlet UIWebView *webView;

}

@property (nonatomic, retain) UIWebView *webView;
@end

2、打开Simple_UIWebViewViewController.xib文件,从library中拖拽一个Web View到视图中,并且按command+1,

在Web Attributes中的scaling勾选scales page to fit, 点击File’s Ower图标到Web View并且选择webView输出口,保存文件并退出。

3、打开Simple_UIWebViewViewController.h文件,代码:

#import "Simple_UIWebViewViewController.h"

@implementation Simple_UIWebViewViewController
@synthesize webView;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    NSURL *url = [NSURL URLWithString:@"http://www.apple.com"];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    [webView loadRequest:req];

}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}
- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)dealloc {
    [super dealloc];
}

@end
在viewOnLoad方法中添加要访问的url链接地址,最后使用loadRequest读取req对象的web视窗。
运行程序,效果如图:
 
屏幕快照 2011-02-20 下午05.38.59

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值