有人能帮我从iPhone应用程序上获取NSURLRequest返回的字符串吗?我必须将一些用户凭据发送到URL,它将返回一个客户ID号。这就是我需要作为字符串引入的内容,我不习惯使用服务器或HTTP请求,所以任何帮助都会很棒。我已经阅读过关于它的Apple Docs,并且在这方面有点迷失。
答案 0 :(得分:6)
这是标准模式async下载器:
在.h文件中:
NSMutableData *responseData;
和.m文件:
-(void) execute {
NSString *urlString = @"http://www.google.com";
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[responseData setLength:0];
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[responseData appendData:data];
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection{
[connection release];
NSString *data = [[[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding] autorelease];
NSLog(@"%@", data);
[responseData release];
}
这将下载Google并打印内容。