我正在初始视图控制器的viewDidAppear中对服务器进行URL调用。如果服务器调用成功(服务器发送响应代码1),则只有用户可以访问该应用程序,否则会要求他重试。即使互联网已连接,第一个网址调用失败的次数约为2次,响应为nil(已将 timeoutInterval设置为7秒)。 我使用Chrome Postman插件测试了相同的网址调用。但它正在响应每一个请求。可能是什么原因?它发生在某个时间,所以它也不可重现.Below是我的AppDelegate代码
//Setting up NSURL Cache
NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024
diskCapacity:20 * 1024 * 1024
diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];
//Setting up AWS Thing
AWSStaticCredentialsProvider *credentialsProvider=[[AWSStaticCredentialsProvider alloc] initWithAccessKey:k_AmazonKeyID secretKey:k_AmazonPassword];
AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSWest2 credentialsProvider:credentialsProvider];
[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;
//Setting up for RateApp thing
[Appirater setAppId:@"********"];
[Appirater setDebug:YES];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
return YES;
以下代码是viewDidAppear中的方法,我在这里调用URL
+(void)callURLWithString:(NSString*)urlStr withBody:(NSDictionary*)dict andIsMethodPOST:(BOOL)isPOST withBlock:(void(^)(NSData *data,NSError *error))block
{
NSString *body=[HelperMethods_SuperdocPatientsAPI makeParamtersString:dwithEncoding:NSUTF8StringEncoding];
NSURL *url;
NSMutableURLRequest *request;
NSURLRequestCachePolicy policy;
if (isPOST) {
//Post method
url=[NSURL URLWithString:urlStr];
request=[NSMutableURLRequest requestWithURL:url cachePolicy:poltimeoutInterval:k_URLTimeoutSeconds];
[request setHTTPMethod:@"POST"];
[request addValue:@"text/plain" forHTTPHeaderField:@"Content-type"];
[request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
}
else
{
//Get method
url= [NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",urlStr,body]];
request=[NSMutableURLRequest requestWithURL:url cachePolicy:poltimeoutInterval:k_URLTimeoutSeconds];
[request setHTTPMethod:@"GET"];
[request addValue:@"text/plain" forHTTPHeaderField:@"Content-type"];
}
NSHTTPURLResponse *response = nil;
NSError *error;
NSData *outputReceived=[NSURLConnection sendSynchronousRequest:requreturningResponse:&response error:&error];
if (response == nil) {
dispatch_async(dispatch_get_main_queue(), ^{
[[[UIAlertView alloc] initWithTitle:@"Oops!" message:@"Please Check YInternet Connection and Try Again" delegate:self cancelButtonTitle:@"otherButtonTitles:nil] show];
});
NSLog("%@",error.description);
block(nil,error);
}
else
{
block(outputReceived,nil);
}
}
使用AFNetworking Library会解决我的问题吗?