iOS/xcode:didReceiveData当在设备上运行时为空,但在模拟器上罚款?
我正在开发一个使用API的iphone应用程序。
iOS/xcode:didReceiveData当在设备上运行时为空,但在模拟器上罚款?
在'模拟器'它工作正常。在'设备'api调用所有运行但响应委托都返回空。
LOG:
1)didReceiveData
返回(空NSData
即< 20>)下面设备:
append data <20>
但这对于模拟器:
append data <dfgdf89fgd.....>
2)该didReceiveResponse
我有以下返回设备下面返回(设置NSLog监视NSURLConnection对象)。困惑,为什么这是不完整的:
1 connection <NSURLConnection: 0x147a30>
但这对于模拟器:
1 connection <NSURLConnection: 0x4b76ea0, http://api.site.com/search?id=111>
3)的connectionDidFinishLoading
低于为模拟器
DONE. Received Bytes: 1400
但低于设备:
DONE. Received Bytes: 1
我的代码:
我有一个API文件中下面的代码:
- (void)fetch: (NSString *)id{
NSMutableData *receivedData = [[NSMutableData alloc]init];//initialised
self.receivedData = receivedData;//set to available globally
NSString *initialUrl = [NSString stringWithFormat: @"http://api.site.com/search?id=%@",id];
NSURL *url = [NSURL URLWithString:initialUrl];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"Token token=\"apitoken\"" forHTTPHeaderField:@"Authorization"];
[NSURLConnection connectionWithRequest:[request autorelease] delegate:self];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"1 connection %@",connection);
NSLog(@"didReceiveResponse");
[self.receivedData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"2 connection %@",connection);
NSLog(@"append data %@",data);
[self.receivedData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
//TODO error handling for connection
NSLog(@"Cannot Connect");
if ([_delegate respondsToSelector:@selector(apiFailed:)]) {
[_delegate apiFailed:[error localizedDescription]];
}
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"DONE. Received Bytes: %d", [self.receivedData length]);
NSLog(@"FULL DATA: %@",self.receivedData);
}
的模拟器和设备(我的iphone与provi sioned个人资料和工作)都运行在同一个Wifi(我的家庭路由器)。
的API:
的API返回JSON少量和我有一组不同的JSON和器件和模拟器返回完整JSON没有问题的测试,我只是不能让它工作与这个API。
我甚至在不同位置有相同的JSON(api返回),我可以做相同的请求(带有头文件和身份验证),它对设备没有任何问题。奇怪的是,虽然连接对象仍然不适合伪造的api服务器。在设备didReceiveResponse
有1 connection <NSURLConnection: 0x1af600>
仍然并没有与连接url(但运行在模拟器它有1 connection <NSURLConnection: 0x8193fd0, http://test-server.com:81/api-data.json>
)。这表明其设备和模拟器NSURLCONNECTION
对象之间的正常差异。
我觉得这可能是认证的方式获得,但API是完全开放的(假设你有令牌)并用模拟器加didFailWithError
是跑不了的工作没有问题..
我已经宣布委托方法在头文件里面。
不知道为什么连接对象和数据对象都会有问题。
备选:
我尝试下面的方法获取,而不是什么我已经内,但它也适用于模拟器但同样的事情发生在设备。
//NSURLCONN and NSDATA in response still return empty???
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL
URLWithString:@"http://api.site.com/search?id=111"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"Token token=\"mytoken\"" forHTTPHeaderField:@"Authorization"];
NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
我会很感激任何想法。
感谢
来进行测试:从请求
- 删除缓存(它可以做一些很有趣的事情)。
- 在
connection:didReceiveResponse:
中打印出HTTP状态码。确保你得到了200(你应该总是检查状态码!) - 在其他REST客户端(大多数浏览器的插件存在)中测试连接。
- 确保API不是“用户代理”敏感的。
- 如果使用“https”协议,请确保证书由设备已知的根授权机构签署。
- 使用调试器检查一切正常工作(例如
url
不是nil
,当创建请求时)。 - 您确定您使用的是正确的
Authorization
标题吗?价值Token token="..."
似乎很奇怪。
(3)在Firefox RESTCLIENT上完成并没有问题。没有https(5)。标题是相同的,并且可以在模拟器(4)中正常工作。现在尝试(2)。最好怎么做(1)? – 2013-04-21 16:19:53
@CraigTaub for 1 - 设置请求缓存策略为NSURLRequestReloadIgnoringLocalAndRemoteCacheData – 0x8badf00d 2013-04-21 16:22:40
谢谢...好主意检查状态码,它返回的'401(非授权)',这对于模拟器来说是非常奇怪的......(6)已经检查过,是的,它的正确和(7)同样的事情。 – 2013-04-21 16:23:41