以下是我用于 http 请求的代码行。 在运行模拟器时, 我只能得到“ 显示的连接” 。 有人能用正确的代码帮助我显示 Google 主页 。 谢谢 。
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http:www.google.com"] cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 15.0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest: request delegate:self];
if(connection){
//connect
label.text = @"connecting .....";
}else{
//error
}
}
-(void) connection: (NSURLConnection *)conenction didReceiveData:(NSData *)data{
response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection{
connection = nil;
}