Asynchronous POST to server

∥☆過路亽.° 提交于 2019-12-04 19:16:35

@ott is on the right track, I'll try to clarify.

  1. You don't need start as he says. It's benign as the connection will start automatically.

  2. initWithRequest:delegate and connectionWithRequest:delegate: are equivalent except for the retain state of the new connection object.

  3. The real problem is b/c you are using connectionWithRequest:delegate the returned connection is autoreleased at the end of the run loop and you are not retaining it in a property. Therefore, the connection never starts.

The solution is to add a property @property (nonatomic, retain) NSURLConnection *connection to your class and set this property to the connection returned from connection:withRequest:

You then release the connection in the completion methods connection:didFinishLoading and connection:didFailWithError:.

The start is wrong here. Simply use

self.connection = [NSURLConnection connectionWithRequest:request delegate:self];

with NSURLConnection *connection; defined. See the class reference for connectionWithRequest. start is to be used with initWithRequest:delegate:.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!