我希望我的应用程序对设备进行身份验证,即通过 https 传递用户名和密码。
我看了几个例子,基本上构建了以下内容:
-(void)authentication
{
//setting the string of the url taking from appliance IP.
NSString *urlString =[NSString
stringWithFormat:
@"http://%@",appliance_IP.text];
//define the url with the string
NSURL *url = [NSURL URLWithString:urlString];
//creating request
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//setting credential taking from the username and password field
NSURLCredential *credential = [NSURLCredential credentialWithUser:username.text password:password.text persistence:NSURLCredentialPersistenceForSession];
NSURLProtectionSpace *protectionSpace=
[[NSURLProtectionSpace alloc]initWithHost:urlString port:443 protocol:@"https" realm:nil authenticationMethod:nil ];
[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential forProtectionSpace:protectionSpace];
[ NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL];
}
由于我使用了我需要更多理解的示例,上面示例中的 NSURLConnection 不包含用户名和密码,我该如何添加它?因此请求还将包含用户名和密码,或者最好传递该信息。当前请求仅包含 url 字符串。
谢谢急诊室