![](/img/trans.png)
[英]iOS: Why do I need to use sleep(1) after NSURLConnection sendAsynchronousRequest?
[英]NSURLConnection originalRequest throwing exception in iOS 4 what can I use instead?
在iOS 5 [NSURLConnection originalRequest]
工作正常,但在iOS 4中它抛出一个异常:
- [NSURLConnection originalRequest]:无法识别的选择器发送到实例0x65609a0
看起来iOS 4.3 SDK没有originalRequest
方法。
我可以用什么代替在iOS 4中获取NSURLConnection
的原始请求?
阅读NSURLConnection.h头文件 - 您将看到以下注释:
When created, an NSURLConnection performs a deep-copy of the
NSURLRequest. This copy is available through the
-originalRequest method. As the connection performs the load,
this request may change as a result of protocol
canonicalization or due to following redirects.
-currentRequest can be used to retrieve this value.
由于此方法是在iOS5中引入的,因此您无法在iOS4.3或更早版本中使用它。 因此,在这种情况下,您需要复制新方法的功能。
如果您使用的是非常简单的NSURLRequest对象,它可能就像在初始化NSURLConnection之前执行[请求复制]一样简单,否则您将需要研究如何实现深层复制。 NSURLRequest确实实现了NSCoding,因此如果您需要,可以存档和取消归档以进行深层复制。
我做的是这样的:我创建了一个NSString *origiOS4
,如果运行的操作系统是iOS4,那么我初始化字符串并给它urls值。 在connection:didReceiveData:
方法然后,我从origiOS4
读取值。
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version < 5) {
origiOS4 = [[NSString alloc] initWithString:response.URL.absoluteString];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.