xcode 4.6 使用NSURLConnection 获取网页内容(iOS6.1,纯手工编码,无xib,无storyboard)
来源:互联网 发布:乐谱排版软件 编辑:程序博客网 时间:2024/05/20 08:22
环境 iOS 6.1, xcode 4.6
一、创建新项目
1、打开 xcode,File --> New --> Project... -->Empty Application
2、项目名称 NSURLConnectionDemo,下面所有选项全部不选,完成创建。
二、创建视图控制器
3、File-->New-->File-->Objective-C class
4、创建UIViewController的子类,命名 TestViewController
三、创建视图控制器实例
5、在 AppDelegate.m中引入
#import "TestViewController.h"
6、添加TestViewController到窗体
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
TestViewController *rootController = [[TestViewController alloc]init];
self.window.rootViewController = rootController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[rootController release];
return YES;
}
7、在TestViewController.h 中添加两个变量
@interface TestViewController : UIViewController{
NSMutableData * pageData ;
UIWebView *webView;
}
8、在TestViewController.m 中添加按钮和 UIWebView
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.view addSubview:button];
[button setTitle:@"start" forState:UIControlStateNormal];
[button setFrame:CGRectMake(10,20, 300, 40)];
[button addTarget:self action:@selector(openUrl) forControlEvents:UIControlEventTouchUpInside];
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 50, 320, 400)];
[webView setUserInteractionEnabled:NO];
[webView setBackgroundColor:[UIColor clearColor]];
[webView setOpaque:NO];
[self.view addSubview:webView];
}
9、实现按钮点击的事件
-(void)openUrl
{
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc ]initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
pageData = [[NSMutableData alloc]init];
[request setHTTPMethod:@"GET"];
[request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
[request release];
[conn release];
}
10、实现 NSURLConnection 代理的两个方法
- (void)connection:(NSURLConnection *)aConn didReceiveData:(NSData *)data {
[pageData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)aConn {
NSString *results = [[NSString alloc] initWithData:pageData encoding:NSUTF8StringEncoding];
[webView loadHTMLString:results baseURL:nil];
}
<END>
- xcode 4.6 使用NSURLConnection 获取网页内容(iOS6.1,纯手工编码,无xib,无storyboard)
- iOS UICollectionView 纯代码,无xib
- xcode xib,storyboard降级
- Xcode6.1创建仅xib文件无storyboard的hello world应用
- IOS swift3.1 创建Swift空工程(无storyBoard)纯代码方式编写APP
- 网页使用无缓存
- 纯网页省市二级联动(无数据库)
- Xcode 8上 storyboard xib 使用imageView不显示问题
- Xcode中不用Storyboard,用纯xib创建TabBar模式视图
- iOS纯代码实现界面建立、跳转、导航栏(无storyboard、无nib)(Objective-C)
- iOS纯代码实现界面建立、跳转、导航栏(无storyboard、无nib)(Objective-C)
- storyboard 修改 无反应
- Asp 使用 Microsoft.XMLHTTP 抓取网页内容无乱码处理,并过滤需要的内容
- 纯代码 vs XIB vs Storyboard
- xib/纯代码/storyboard之争
- xcode中获取xib或者storyboard中控件 的两种方式
- 无javascript,纯CSS制作的网页下拉菜单
- 无javascript,纯CSS制作的网页下拉菜单
- 实验6 多态性
- 创建一个app流程
- WPF DataTrigger的一个简单实例
- django的views基本代码
- java.lang.NoSuchMethodError: javax.servlet.ServletContext.getContextPath()Ljava/lang/String;
- xcode 4.6 使用NSURLConnection 获取网页内容(iOS6.1,纯手工编码,无xib,无storyboard)
- hdoj 1176免费馅饼(dp)
- ZOJ 2682 People like People(DFS)
- Ubuntu 10.10开启SSH服务
- 进程的状态转换
- 从海量数据中找出重复次数最多的一个
- 驾考那点事儿(经验分享)
- 2013金山西山居创意游戏程序挑战赛——初赛(1)题解
- django 的基本函数