当使用完成处理程序样式请求,如下所示:
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.mysite.com/"]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response,NSData *data,NSError *error) { //Do Stuff }];
处理身份验证的正确方法是什么?我需要分配和初始化NSURLConnection并设置代理,而不是使用此类方法样式?我想我知道如何使用委托函数正确地进行身份验证,但是我想知道我是否可以将它包含在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]; }}解决方法 我认为completionHandler方法是基本请求.也许你可以考虑使用 AFNetworking,因为我使用块方法和身份验证.
编辑….
您是否尝试将身份验证标头添加到NSURLRequest?
创建一个NSMutableURLRequest:
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.mysite.com/"]];
并添加如下认证头:
Nsstring *basicAuthCredentials = [Nsstring stringWithFormat:@"%@:%@",username,password];Nsstring *authValue = [Nsstring stringWithFormat:@"Basic %@",AFBase64EncodedStringFromString(basicAuthCredentials)];[urlRequest setValue:authValue forhttpheaderFIEld:@"Authorization"];
AFBase64EncodedStringFromString函数是这样的:
static Nsstring * AFBase64EncodedStringFromString(Nsstring *string) { NSData *data = [NSData dataWithBytes:[string UTF8String] length:[string lengthOfBytesUsingEnCoding:NSUTF8StringEnCoding]]; NSUInteger length = [data length]; NSMutableData *mutableData = [NSMutableData dataWithLength:((length + 2) / 3) * 4]; uint8_t *input = (uint8_t *)[data bytes]; uint8_t *output = (uint8_t *)[mutableData mutableBytes]; for (NSUInteger i = 0; i < length; i += 3) { NSUInteger value = 0; for (NSUInteger j = i; j < (i + 3); j++) { value <<= 8; if (j < length) { value |= (0xFF & input[j]); } } static uint8_t const kAFBase64EnCodingtable[] = "ABCDEFGHIJKLMnopQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; NSUInteger IDx = (i / 3) * 4; output[IDx + 0] = kAFBase64EnCodingtable[(value >> 18) & 0x3F]; output[IDx + 1] = kAFBase64EnCodingtable[(value >> 12) & 0x3F]; output[IDx + 2] = (i + 1) < length ? kAFBase64EnCodingtable[(value >> 6) & 0x3F] : '='; output[IDx + 3] = (i + 2) < length ? kAFBase64EnCodingtable[(value >> 0) & 0x3F] : '='; } return [[Nsstring alloc] initWithData:mutableData enCoding:NSASCIIStringEnCoding];}
然后调用您之前调用的函数,但使用您的新NSURLRequest:
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response,NSError *error) { //Do Stuff }];总结
以上是内存溢出为你收集整理的ios – 使用NSURLConnection进行身份验证sendAsynchronousRequest与完成处理程序全部内容,希望文章能够帮你解决ios – 使用NSURLConnection进行身份验证sendAsynchronousRequest与完成处理程序所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)