NSURLConnection网上有很多方法了,这里给出两个参考连接:
iPhone网络开发之如何使用NSURLConnection http://blog.csdn.net/a6472953/article/details/7617326
NSURLConnection和NSMutableURLRequest 实现同步、异步请求 http://blog.sina.com.cn/s/blog_7b9d64af01019p6q.html
我说一下细节,方便刚入门的。
1、首先要实现NSURLConnectionDelegate协议,就是在头文件定义中加入尖括号:
@interface ModalViewController : UIViewController <NSURLConnectionDelegate>
2、发送异步请求的两种方法是一样的,如下:
[NSURLConnection connectionWithRequest:request delegate:self]; //发送一个异步请求
或者:
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
3、监听回调:
加入一行指令:
#pragma mark - NSURLConnectionDelegate
后面就可以使用回调函数了
4、特别注意,不能在异步线程dispatch_async里面调用NSURLConnection,否则没有响应!具体我不清楚原因,呵呵