Tag: nsurlconnection

iOS开发:为什么我的NSURLConnection只有一些用户出现“错误的URL”错误?

我有一个iOS应用程序,它从Heroku上托pipe我的Rails 3应用程序请求JSON数据,并且在我的设备和许多其他用户(除了一个用户)上效果很好。 我有一个用户告诉我,我的应用程序无法检索JSON数据,所以我让她给我发送一些日志数据,日志显示NSURLConnection委托方法didFailWithError被调用,错误描述为“bad URL” 。 为什么会发生这个错误,为什么它只发生在一些设备而不是所有设备上? 这是我的代码, -(void)getTournamentInfoWithUsername:(NSString*)username { NSString *urlString = [NSString stringWithFormat:@"http://myapp-tourney.heroku.com/tournaments/next.json?username=%@", username]; NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30]; [self setUrlConnection:[[NSURLConnection alloc] initWithRequest:request delegate:self]]; } – (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error { [MyLog logError:[NSString stringWithFormat:@"%@ – %@ – %@ – %@", [error localizedDescription], [error localizedFailureReason], [error localizedRecoveryOptions], [error localizedRecoverySuggestion]]]; } […]

Object-c / iOS:如何使用asynchronous从URL获取数据?

我的朋友看到​​我的代码,一部分是从URL获取plist数据 他告诉我不要使用同步,使用asynchronous 但我不知道如何以简单的方式做asynchronous 这是我在程序中使用的代码 NSURL *theURL = [[NSURL alloc]initWithString:@"http://someurllink.php" ]; NSURLRequest *theRequest=[NSURLRequest requestWithURL:theURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; NSData *returnData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil]; NSString *listFile = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; self.plist = [listFile propertyList]; [self.tableView reloadData]; [listFile autorelease]; 如何更改我的代码使用asynchronous获取数据? 非常感谢所有答复和答案:)

使用NSURLConnection和NSURLProtectionSpace确定信任

我想问一个以前提出的问题的后续问题 。 我有创build一个NSURLRequest /连接的代码,运行它,并具有调用callback方法的身份validation。 以下是特定的代码: – (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust] || [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodDefault]; } -(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { if ([challenge previousFailureCount] > 0) { [[challenge sender] cancelAuthenticationChallenge:challenge]; NSLog(@"Bad Username Or Password"); badUsernameAndPassword = YES; finished = YES; return; } if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) { if (appDelegate._allowInvalidCert) { // Go ahead…trust me! […]

带有完成处理程序的NSURLConnection sendAsynchronousRequestvalidation

一般来说,我喜欢用NSURL的sendAsynchronousRequest类方法使用完成处理程序块来“消灭和忘记”,但是在需要身份validation时,似乎可能不是一个选项。 当使用这样的完成处理程序样式请求时: [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.mysite.com/"]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { //Do Stuff }]; 什么是处理身份validation的正确方法? 我需要分配和初始化NSURLConnection并设置一个委托,而不是使用这种类方法风格? 我想我明白如何使用委托函数正确进行身份validation,但我试图弄清楚是否可以将它包含在completionHandler块中,或者是否有更好的方法来执行此操作。 – (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { if ([challenge previousFailureCount] > 0) { NSLog(@"Authentication Failure"); [connection cancel]; } else { NSURLCredential *credential = [NSURLCredential credentialWithUser:self.username password:self.password persistence:NSURLCredentialPersistenceForSession]; [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; } }

NSURLConnection在iOS9中已弃用

我想用NSURLRequest下载一个文件并保存,但是与 NSData * data = …发生错误。 NSURL *Urlstring = [NSURL URLWithString:@"http://yourdomain.com/yourfile.pdf"]; NSURLRequest *request = [NSURLRequest requestWithURL: Urlstring]; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; documentsURL = [documentsURL URLByAppendingPathComponent:@"localFile.pdf"]; [data writeToURL:documentsURL atomically:YES]; 警告消息是我应该使用NSURLSession dataTaskwithrequest “,因为sendSynchronousRequest在iOS 9中被弃用,但是这不起作用,我希望有人可以帮助我

使用证书创buildSSL连接

我有作为文件提供给我的ssl证书(.cer)。 我添加它捆绑,并希望使用它与服务器通信。 我用苹果提供的代码: – (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{ DLog(@"didReceiveAuthenticationChallenge : %@",challenge); if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) { NSString *filePath = [[NSBundle mainBundle] pathForResource:@"certificate" ofType:@"cer"]; NSData *certData = [NSData dataWithContentsOfFile:filePath]; CFDataRef myCertData = (__bridge CFDataRef)certData; SecCertificateRef myCert = SecCertificateCreateWithData(NULL, myCertData); SecPolicyRef myPolicy = SecPolicyCreateBasicX509(); // 3 SecCertificateRef certArray[1] = { myCert }; CFArrayRef myCerts = CFArrayCreate(NULL, (void *)certArray, […]

与NSURLSessionasynchronous上传将无法正常工作,但同步NSURLConnection不会

编辑:我需要从iPhoneasynchronous上传文件到Python服务器端进程。 我想asynchronous地执行这个请求,这样我可以在工作时显示一个忙碌的animation。 该请求需要包含用户名,密码和文件“multipart / form-data”。 我可以使用NSURLConnection来同步工作,代码如下所示: -(void) uploadDatabase{ Database *databasePath = [[Database alloc] init]; NSString *targetPath = [databasePath getPathToDatabaseInDirectory]; NSData *dbData = [NSData dataWithContentsOfFile:targetPath]; NSString *url = @"http://mydomain.com/api/upload/"; //NSString *username = [[NSUserDefaults standardUserDefaults] stringForKey:USERNAME]; NSString *username = @"user"; NSString *password = @"pass"; NSMutableURLRequest *request = [self createRequestForUrl:url withUsername:username andPassword:password andData:dbData]; NSURLResponse *response; NSError *error; NSData […]

在UIProgressView上添加NSURLConnection加载过程

我创build了一个UIProgressView 。 但是我用NSTimer来UIProgressView's过程。 现在我需要整合UIProgressView过程,当URL加载。 UIProgressView's大小将取决于NSURLConnection's数据。 我用下面的代码来NSURLConnection 。 -(void)load { NSURL *myURL = [NSURL URLWithString:@"http://feeds.epicurious.com/newrecipes"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60]; [[NSURLConnection alloc] initWithRequest:request delegate:self]; } -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { responseData = [[NSMutableData alloc] init]; } -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [responseData appendData:data]; } -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [connection release]; UIAlertView *alert = […]

我如何通过NSURLConnection sendAsynchronousRequest取消asynchronous调用?

我有一个Web服务调用实时validation用户input。 我想在validation(在iOS 5中引入[NSURLConnection sendAsynchronousRequest]上使用[NSURLConnection sendAsynchronousRequest] ,但如果用户同时更改input字段内容,则取消它。 什么是取消当前请求的最佳方法?

带有NSOperation的asynchronousNSURLConnection

我想在后台模式下做NSURLConnection ,因为它的响应是有很多数据的didEnterBackground告诉使用Apple的有限长度编码在didEnterBackground使用。 但我想避免它。取而代之,我使用下面的代码通过与NSInvocation NSOperation作为,但它不工作。 connectToServer有NSURLConnection operation.any帮助吗? didReceiveData , didReceiveResponse委托方法不被调用? NSOperationQueue *queue = [NSOperationQueue new]; NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(connectToServer) object:nil]; [queue addOperation:operation]; [operation release]; [queue autorelease]; -(void)connectToServer { NSURL *url = [NSURL URLWithString:@"http://www.google.com"]; NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; NSURLConnection *theConnection = [[[NSURLConnection alloc] initWithRequest:theRequest delegate:self] autorelease]; if( theConnection ) { webData = [[NSMutableData […]