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 屏幕快照 2011-02-20 下午05.38.59](http://jordy.easymorse.com/wp-content/uploads/2011/02/2011-02-20-05.38.59_thumb.png)