[英]iOS Networking Library Suggestion
对于为我的应用选择iOS网络库,我会提出一些建议。
我的需求是:
UIAlertView
来通知用户错误。 有没有人建议? (除了不再支持的ASIHTTPRequest),如果此lib有一些不错的文档,则更好。 我是iOS初学者。
预先感谢您的帮助。
我听说过RestKit的好东西https://github.com/RestKit/RestKit
这篇博文的底部有很多替代方案。 http://allseeing-i.com/[request_release];
我以前只使用过ASIHTTPRequest,尽管Ben Copsey不再开发它,但看起来他仍在合并请求和其他内容。 它被很多人使用,如果社区接手我也不会感到惊讶。 对于至少另一个版本的iOS,它可能是安全的。
您实际上并不需要一个库。
发送同步的GET请求:
//set up the GET URL and params
NSURL *getURL = [NSURL URLWithString:@"http://somesite.com/somepath?foo=bar"];
//create the request
NSURLRequest *request = [NSURLRequest requestWithURL:getURL];
//get the response
NSError *error = nil;
NSURLResponse *response = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
要发送同步POST请求:
//set up the POST URL and params
NSURL *postURL = [NSURL URLWithString:@"http://somesite.com/somepath"];
NSString *postParams = @"foo=bar&hello=world";
//create the request - this bit is the same for every post
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:postURL];
[request setHTTPMethod:@"POST"];
[request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSData *data = [postParams dataUsingEncoding:NSUTF8StringEncoding];
[self addValue:[NSString stringWithFormat:@"%i", [data length]] forHTTPHeaderField:@"Content-Length"];
[self setHTTPBody:data];
//get the response
NSError *error = nil;
NSURLResponse *response = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
无论哪种情况,如果responseData为nil或error不为nil,请使用以下命令显示警报:
[[[[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease] show];
如问题所述,他还正在寻找异步请求。
我建议使用AFNetworking或GCNetworkKit(最后一个是我自己的)。 这两个库都非常易于使用,但功能强大。
我不认为AFNetworking提供同步网络请求。 好吧,至少我的不是。 无论如何,您都不应该这样做。 这两个库都支持错误处理。 实现UIAlertView应该没有问题。
您可以在GitHub上找到它们。 只是搜索它们。 希望这可以帮助 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.