Tag: nsurlconnection

iOScaching策略

我正在做一个连接到NSURL,我需要创build一个请求绕过所有的caching策略。 我见过的例子有: NSURLRequest *request = [NSURLRequest requestWithURL:baseURL cachePolicy:0 timeoutInterval:10]; self.urlConnection = [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease]; 请问高速caching策略0是指什么? 已经尝试search数字代表什么,但我没有得到任何我需要的答案。 谢谢! 🙂

请求NSURLRequest

我试图做一个使用数据库的应用程序(实际上在我的本地主机),我尝试使用ASIHTTPRequest,但有这么多麻烦与iOS 5(我学会了如何使用ASIHTTPRequestforms: http ://www.raywenderlich.com / 2965 /如何到写的-IOS-APP-该用途-A-Web服务 现在我想用苹果提供的API:NSURLRequest / NSURLConnection等,… 我阅读了苹果在线指南,并创build了第一个代码: – (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8888/testNSURL/index.php"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; [request setValue:@"Hello world !" forKey:@"myVariable"]; NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self]; if (theConnection) { receiveData = [NSMutableData data]; } […]

将大型NSData上传到Web

我目前正在开发一个应用程序,它需要将大文件(主要是电影/video)上传到networking上。 看完我所能做的之后,我将电影转换为NSData,然后将其作为NSURLConnection's HTTPBody 。 但是,在将电影(最初是一个ALAsset )转换为NSData ,我收到内存警告,然后再发生崩溃。 我不知道如何去上传这些types的大文件,如果这些数据只是造成即时崩溃。 我正在考虑的一个解决scheme是写入文件系统,然后直接从那里上传文件,但是我一直没能find任何信息来完成这个任务。 这是我使用的相关代码。 如果有什么我在这里做错了,我很想知道。 ALAssetRepresentation *representation = [asset defaultRepresentation]; Byte *buffer = (Byte *)malloc([representation size]); NSUInteger buffered = [representation getBytes:buffer fromOffset:0.0 length:[representation size] error:nil]; uploadData = [NSData dataWithBytes:buffer length:buffered]; free(buffer);

用信号量实现NSURLConnection阻塞包装器

对于我最近的项目,我偶然发现需要: 以阻塞的方式下载数据(在后台线程中启动) 而且会逐渐处理收到的数据(因为下载的数据可能很容易达到100M,所以将其全部存储在一个大的NSData *中效率不高) 因此,我需要使用一个asynchronous的NSURLConnection对象(为了能够逐步接收数据),但将其包装在一个容器中,该容器将阻止两个连续的connection:didReceiveData:之间的调用线程connection:didReceiveData:委托调用,直到connectionDidFinishLoading:或connection:didFailWithError:被调用。 我以为我会分享我的解决scheme,因为它花了我几个小时把这里和那里find的正确的代码(在StackOverflow和其他论坛)。 代码基本上在后台线程( dispatch_get_global_queue )上启动一个新的NSURLConnection ,将运行循环设置为能够接收委托调用,并使用dispatch_semaphores以“交替”方式阻止调用线程和后台线程。 dispatch_semaphores代码很好地包装在一个自定义的ProducerConsumerLock类中。 BlockingConnection.m #import "BlockingConnection.h" #import "ProducerConsumerLock.h" @interface BlockingConnection() @property (nonatomic, strong) ProducerConsumerLock* lock; @end @implementation BlockingConnection – (id)initWithURL:(NSURL*) url callback:(void(^)(NSData* data)) callback { if (self = [super init]) { self.lock = [ProducerConsumerLock new]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10]; [NSURLConnection connectionWithRequest:request […]

在iOS中使用NSURLConnection的表单数据请求

我想在iOS中使用NSURLConnection使http表单发布。 我在HTML表单中有两个表单域和一个file upload选项。 当我使用NSURLConnection做同样的事情时,我没有得到回应。 NSString *urlString = @"http://url/test.php"; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:urlString]]; [request setHTTPMethod:@"POST"]; NSString *contentType = [NSString stringWithFormat:@"multipart/form-data"]; [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; NSMutableData *body = [NSMutableData data]; [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"file\"; filename=\"myphoto.png\"rn"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-streamrnrn"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:filedata]; [body appendData:[[NSString stringWithFormat:@"&s=YL4e6ouKirNDgCk0xV2HKixt&hw=141246514ytdjadh"] dataUsingEncoding:NSUTF8StringEncoding]]; [request setHTTPBody:body]; NSData *returnData […]

如何unit testing一个NSURLConnection委托?

我怎样才能unit testing我的NSURLConnection委托? 我做了一个ConnectionDelegate类,它符合不同的协议,以便将来自Web的数据提供给不同的ViewController。 在我太远之前,我想开始写我的unit testing。 但是我不知道如何testing他们作为一个单位没有互联网连接。 我也想我应该做什么来处理asynchronouscallback。

为什么我更喜欢NSURLConnection的ASIHTTPRequest从网上下载文件?

我见过几次使用ASIHTTPRequest来下载文件的人。 现在我想知道为什么? NSURLConnection的核心优势是什么?

iOS,NSURLConnection:委托不同的线程callback?

我怎样才能让NSURLConnection调用它的委托方法从一个不同的线程,而不是主线程。 我试图搞乱scheduleInRunLoop:forMode:但似乎并没有做我想要的。 我必须下载一个大文件,并频繁地中断主线程,以至于一些正在发生的渲染开始变得波涛汹涌。 NSURLRequest * request = [NSURLRequest requestWithURL:url]; NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO]; NSRunLoop * loop = [NSRunLoop currentRunLoop]; NSLog(@"loop mode: %@",[loop currentMode]); [connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; [connection start]; 另外一件我没有看到的东西是“模式”只有两种模式被logging下来,所以没有太多真正的testing。 有任何想法吗? 谢谢

从NSURLResponse完成块中获取数据

看起来我还没有完全得到块的概念… 在我的代码中,我必须从asychronous block获取JSON数据,并从“ outer ”方法返回。 我searchvariable with __block ,发现如果variable with __block定义一个variable with __block这个variables的variables被扩展到这个block 。 但由于某种原因返回的JSON对象是零。我想知道为什么? – (NSMutableDictionary *)executeRequestUrlString:(NSString *)urlString { __block NSMutableDictionary *json = nil; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPShouldHandleCookies:YES]; [request setHTTPMethod:@"GET"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"]; NSString *cookieString = [self.userDefaults objectForKey:SAVED_COOKIE]; [request addValue:cookieString forHTTPHeaderField:@"Cookie"]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse […]

具有自签名证书的NSURLSession +服务器

我有一个生产的应用程序,以及具有自签名证书的开发服务器。 – (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler我试图testingNSURLSession和后台下载,但似乎无法通过- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler 当我使用NSURLConnection我可以绕过它使用: – (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { NSLog(@"canAuthenticateAgainstProtectionSpace %@", [protectionSpace authenticationMethod]); return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]; } – (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { NSLog(@"didReceiveAuthenticationChallenge %@ %zd", [[challenge protectionSpace] authenticationMethod], (ssize_t) [challenge previousFailureCount]); [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge]; } […]