NSURLConnection IOS 3G issue

人走茶凉 提交于 2019-12-07 06:00:59

问题


I have used NSURLConnection (asynchronous) in my app which is working fine on wifi (iOS5 & iOS6). But its creating an issue with 3G. When I run my app 3G connection then I dont get any data in my didReceiveData method.

I have putted logs in my delegate methods, but while using 3G, The request is getting timed out. What can be the issue.

EDIT: On server side -> It shows that my request has been sent to server & server has also sent the response to the client.

EDIT 2:

The code which I have written is as follows.

NSURL *url = [NSURL URLWithString:@"someURL"];

NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:url     
 cachePolicy:NSURLRequestUseProtocolCachePolicy                                   
 timeoutInterval:40.0];

//[req setHTTPShouldUsePipelining:YES];

[req setValue:@"x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[req setValue:@"someValue" forHTTPHeaderField:@"Accept"];
[req setValue:@"myCrdentails" forHTTPHeaderField:@"Authorization"];
[req setHTTPMethod:@"GET"];

[[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];

The response headers are as below

< HTTP/1.1 200 OK
< X-Powered-By: Express
< Access-Control-Allow-Origin: *
< Content-Type: text/event-stream; charset=utf-8
< Connection: keep-alive
< Transfer-Encoding: chunked

回答1:


After struggling with this issue for hours I finally found a working solution. The NSMutableURLRequest object has a method to set whether cellular networks are allowed or not:

[req setAllowsCellularAccess:YES];

Even though the value seems to be YES by default for objects of type NSURLRequest, it solved the problem for me.

Hopefully this will help someone like me struggling with the issue and ending up on this question.




回答2:


try to use Reachability framework and check there if it returns false for 3G. If it does return true and you still get the timeout, try to go to sync model and see what happens. May be the connection works ok, but something intercepts your callback.




回答3:


Is this https? I wonder if there may be some SSL validation failure going on... Also have you tried querying this from curl over 3G? If the cellular carrier proxies and isn't giving you valid data, or if its IP related (black list, closed port, etc), then you might get a different response. I'd test with curl to be sure. Either from a jail broken phone, or while tethered.




回答4:


The problem with NSURLConnection over 3g is the timeout which is out of your hand basically ,,

If you are uploading or streaming a data to the server and that data handled there and needs sometime more than NSURLConnection time threshold or the connection is too slow the connection would be closed and returns NULL data .. that because the connection goes into IDLE state...

To overcome such issue you need to make the webservice sending an empty packets "spaces" You will see that the didReceiveData invoked on each packet arrival ..

The spaces wont affect the receivedData .. in my case the response is a JSON and I use JSonKit to parse it ,,...

hopefully it will help somebody ..



来源:https://stackoverflow.com/questions/13109874/nsurlconnection-ios-3g-issue

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