NSURLConnection IOS 3G问题 [英] NSURLConnection IOS 3G issue

查看:56
本文介绍了NSURLConnection IOS 3G问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中使用了NSURLConnection(异步),该应用在wifi(iOS5和iOS6)上运行良好.但是,这给3G带来了问题.当我运行应用程序3G连接时,我的didReceiveData方法中没有任何数据.

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.

我已经将日志放入委托方法中,但是在使用3G时,请求超时.可能是什么问题.

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

在服务器端->显示我的请求已发送到服务器&服务器也已将响应发送给客户端.

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

我写的代码如下.

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];

响应头如下

< 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

推荐答案

在为这个问题苦苦挣扎了数小时之后,我终于找到了一个可行的解决方案. NSMutableURLRequest 对象具有一种设置是否允许蜂窝网络的方法:

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];

即使类型为 NSURLRequest 的对象的默认值似乎是 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.

这篇关于NSURLConnection IOS 3G问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆