iphone - nsurlconnection 服务器需要特殊设置吗?

标签 iphone ios nsurl

我想知道服务器是否应该有特殊设置来使用 nsurlconnections?当我尝试创建连接和身份验证时。我没有收到任何错误或警告,但它没有连接。

有人可以向我解释一下这一点吗?

谢谢

编辑:这是我当前使用的代码

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"loginURl"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
    [webView1 scalesPageToFit]; 
    if (theConnection) { 
        receivedData = [[NSMutableData data] retain]; 
    } else { 
        NSLog(@"Connection Failed"); 
    } 
}

最佳答案

不,您的服务器不需要任何特殊配置。 NSURLConnection 仅使用简单的 HTTP 请求。

对于身份验证:您需要自己实现身份验证所需的委托(delegate)方法。另请检查您是否通过 SSL 连接并为此实现所需的委托(delegate)方法。

编辑:这是我的一个使用 HTTP 身份验证的应用程序的一部分。我发现您还需要在 canAuthenticateAgainstProtectionSpace: 方法中为 NSURLAuthenticationMethodHTTPBasic 返回 YES,否则它将无法工作。

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust] || [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic];
}

关于iphone - nsurlconnection 服务器需要特殊设置吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5948903/

相关文章:

iphone - 实现防震防抖算法的基本概念是什么?

iphone - 单点触控 : How can I set the selected item(s) in a UIPickerView?

iphone - 将数组保存到 NSUserDefaults

iphone - 将数据保存在 UITableView 中

iphone - 当 iOS 设备旋转时,如何仅更改一个元素的方向?

ios - 如何检查 Swift/Xcode 中的线程/队列层次结构?

ios - Swift DispatchQueue 全局和主变量

objective-c - 单击主页按钮后 AppDelegate 值不会更改

objective-c - 先在/Application文件夹中搜索应用程序

ios - 代码将在 Playground 上运行,但在应用程序模拟器中执行时失败