Ios NSURLConnection在iPad上超时,在iPhone上正常(Yelp AP/OAuth 2.0I)
我使用YelpSearchAPI基本上只是获取一个搜索查询的企业列表 这几乎是一个NSURLConnection is OAuth,但下面是初始化请求的代码:Ios NSURLConnection在iPad上超时,在iPhone上正常(Yelp AP/OAuth 2.0I),ios,objective-c,ipad,timeout,nsurlconnection,Ios,Objective C,Ipad,Timeout,Nsurlconnection,我使用YelpSearchAPI基本上只是获取一个搜索查询的企业列表 这几乎是一个NSURLConnection is OAuth,但下面是初始化请求的代码: NSURL *URL = [NSURL URLWithString:appDelegate.yelpAdvancedURLString]; OAConsumer *consumer = [[OAConsumer alloc] initWithKey:@"this-is-my-key" secret:@"this-is-my-secr
NSURL *URL = [NSURL URLWithString:appDelegate.yelpAdvancedURLString];
OAConsumer *consumer = [[OAConsumer alloc] initWithKey:@"this-is-my-key" secret:@"this-is-my-secret"];
OAToken *token = [[OAToken alloc] initWithKey:@"this-is-my-key" secret:@"this-is-my-secret"];
id<OASignatureProviding, NSObject> provider = [[OAHMAC_SHA1SignatureProvider alloc] init];
NSString *realm = nil;
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:URL
consumer:consumer
token:token
realm:realm
signatureProvider:provider];
[request prepare];
responseData = [[NSMutableData alloc] init];
yelpConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
当我在iPhone上运行时,一切都正常。然而,当我在iPad上运行时,连接会超时。下面是这一行的内容
NSLog(@"Error: %@, %@", [error localizedDescription], [error localizedFailureReason]);
Error: The request timed out., (null)
另外,如果我使用同步请求,它似乎可以通过以下方式工作:
NSData* result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSDictionary* JSON = [NSJSONSerialization
JSONObjectWithData:result
options:kNilOptions
error:&error];
但是,我想避免使用同步,因为它会冻结应用程序
这是特定于yelpapi的吗?还是我只是做错了什么?提前谢谢,如果有任何帮助,我将不胜感激
如果有帮助,它将在发送请求后大约10秒超时。创建此类型的NSMutableURLRequest:
NSMutableURLRequest*request=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:url]缓存策略:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:240.0] 我认为最好的方法是将init方法从
-(id)initWithURL:(NSURL*)aUrl
消费者:(OAConsumer*)消费者
代币:(奥托肯*)阿托肯
领域:(NSString*)aRealm
签名提供者:(id)一个提供者
{
if(self=[super initWithURL:aUrl
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:10.0])
{
...
}
}
到
-(id)initWithURL:(NSURL*)aUrl
cachePolicy:(NSURLRequestCachePolicy)cachePolicy
timeoutInterval:(NSTimeInterval)timeoutInterval
消费者:(OAConsumer*)消费者
代币:(奥托肯*)阿托肯
领域:(NSString*)aRealm
签名提供者:(id)一个提供者
{
if(self=[super initWithURL:aUrl
cachePolicy:cachePolicy
timeoutInterval:timeoutInterval])
{
...
}
然后再次检查,连接是否会遵守您指定的超时值。我不能使用initWithURL,因为我必须使用OAuth,所以我必须使用initWithRequest请求仍在超时。您能在语句
[请求准备]后检索超时吗;
?可能此方法正在覆盖您的超时设置。请尝试在可能修改请求的最后一条语句之后进行设置。查看OAMutableURLRequest的源代码可以发现这一点,这解释了您的问题:在NSMutableURLRequest子类的init
方法中:if(self=[super initWithURL:AurlCachePolicy:NSURLRequestReloadIgnoringCacheData TimeOutlineVal:10.0])
@CouchDeveloper是您要做的事情:yelpConnection=[[nSurConnection alloc]initWith Request:oarRequest delegate:self];[oarRequest prepare];
?因为它不工作。
NSData* result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSDictionary* JSON = [NSJSONSerialization
JSONObjectWithData:result
options:kNilOptions
error:&error];
- (id)initWithURL:(NSURL *)aUrl
consumer:(OAConsumer *)aConsumer
token:(OAToken *)aToken
realm:(NSString *)aRealm
signatureProvider:(id<OASignatureProviding, NSObject>)aProvider
{
if (self = [super initWithURL:aUrl
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:10.0])
{
...
}
}
- (id)initWithURL:(NSURL *)aUrl
cachePolicy:(NSURLRequestCachePolicy)cachePolicy
timeoutInterval:(NSTimeInterval)timeoutInterval
consumer:(OAConsumer *)aConsumer
token:(OAToken *)aToken
realm:(NSString *)aRealm
signatureProvider:(id<OASignatureProviding, NSObject>)aProvider
{
if (self = [super initWithURL:aUrl
cachePolicy:cachePolicy
timeoutInterval:timeoutInterval])
{
...
}