DAY thirteen – My Google
今天来建立一个 iPhone app 软件,用你的iPhone 直接打开谷歌网页。
纲要:
- 在程序显示前运行代码 -
- 关于iPhone的“UIWebView” 运用 -
首先运行以安装好的 xCode
选择: File->New Project.
从 "New Project" 窗口
选择 : iPhone OS ->Applications-> Utility Application
命名 : 我这里命名为 “Mygoogle”
(1) 在xCode打开 MainViewController.h 文件,加入下面代码
@interface MainViewController : UIViewController {
IBOutlet UIWebView *webView;
}
@end
(2) 在xCode打开 MainViewController.m 文件,加入下面代码
#import "MainViewController.h"
#import "MainView.h"
@implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
// set userInteractionEnabled to true so the user can click links
webView.userInteractionEnabled = true;
// navigate to google
[webView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:@"http://www.google.com"]]];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[super dealloc];
}
(3) 在xCode打开 FlipsdeViewController.h 文件,加入下面代码
#import <UIKit/UIKit.h>
@interface FlipsideViewController : UIViewController {
IBOutlet UIWebView *webView;
}
@end
(4) 在xCode打开 FlipsdeViewController.m 文件,加入下面代码
#import "FlipsideViewController.h"
@implementation FlipsideViewController
- (void)viewDidLoad {
webView.userInteractionEnabled = true;
// load our about page from the resource bundle
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"about" ofType:@"html"];
NSData* htmlData = [NSData dataWithContentsOfFile:filePath];
[webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@"http://www.mywebsite.com/about.html"]];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[super dealloc];
}
@end
(5) 导入下面html文件
下载下面html,放入 Mygoogle 文件夹内并命名为下面名称about.html
文件: about.html
在xCode下右键点击 Mygoogle->Add->Existing Files; 在 Mygoogle 文件夹内,选择下载好的图片,按 Add
(6) MainView 界面设置
双击文件: "mainView.xib" ;
然后 "Interface Builder" 会自动打开,在这里我们可以编辑改变界面
(7) 加入 UIWebView ; 显示:www.google.com
选择: Tools -> Library ; 从Library显示菜单中拖拉一个 Webview 到 Main View
在主视窗口或文件窗口;点击 UIWebView
选择: Tools -> Connection Inspector
移动鼠标在"New Referencing Outlets" 后面圆圈上; 圆圈变为(+); 拖向直线连接到"File's Owner";
放开鼠标选择键出现 "webView"; 选上它。
(8) FlipsideView 界面设置
双击文件: "mainView.xib" ;
然后 "Interface Builder" 会自动打开,在这里我们可以编辑改变界面
(9) 加入 UIWebView ; 显示:about.html
选择: Tools -> Library ; 从Library显示菜单中拖拉一个 Webview 到 Main View
在主视窗口或文件窗口;点击 UIWebView
选择: Tools -> Connection Inspector
移动鼠标在"New Referencing Outlets" 后面圆圈上; 圆圈变为(+); 拖向直线连接到"File's Owner";
放开鼠标选择键出现 "webView"; 选上它。
最后在 xCode 选择 Build->Build and Go; Save All.
下载今天教程文件: Mygoogle.zip
三十一天手把手 iPhone app 基础,来源于www.appsamuck.com,大家有空可以去看看原文;
下面文章本人根据appsamuck这个网站加以翻译,修改及测试,希望大家多多交流。一切版权归http://blog.sina.com.cn/iphonesdk 所有。
来源:http://blog.sina.com.cn/s/blog_5fae23350100eyrt.html