Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在iPhone应用程序中向web服务传递用户名和密码?_Iphone_Objective C_Web Services_Macos - Fatal编程技术网

如何在iPhone应用程序中向web服务传递用户名和密码?

如何在iPhone应用程序中向web服务传递用户名和密码?,iphone,objective-c,web-services,macos,Iphone,Objective C,Web Services,Macos,请查看此代码段:- CommonService-objcommonService NetworkCredential myCredentials=新的NetworkCredential(“144552”)F@mous123456"); objcommonService.Credentials=myCredentials 这是一个C#.net代码。在此代码段中,“objcommonService”是webservice的对象“CommonService”。此Web服务根据其提供的凭据对用户进行预验

请查看此代码段:-

CommonService-objcommonService
NetworkCredential myCredentials=新的NetworkCredential(“144552”)F@mous123456");
objcommonService.Credentials=myCredentials

这是一个C#.net代码。在此代码段中,“
objcommonService
”是webservice的对象“
CommonService
”。此Web服务根据其提供的凭据对用户进行预验证。在C#.NET
NetworkCredential
中,凭据是.NET的标准类

如果我尝试在iPhone应用程序中访问需要这些凭据的Web服务,我应该如何通过?

有没有办法让我的应用程序的用户在使用我的应用程序时不会遇到诸如“身份验证失败!!”之类的困难。
webservice对用户进行预身份验证。我需要在我的应用程序调用该服务时传递我的凭据。

我通过在URLConnection中使用以下委托方法来实现身份验证

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{

if ([challenge previousFailureCount] == 0) {
    NSURLCredential *newCredential;
    newCredential=[NSURLCredential credentialWithUser:username
                                             password:password
                                          persistence:NSURLCredentialPersistenceNone];
    [[challenge sender] useCredential:newCredential
           forAuthenticationChallenge:challenge];

} else {

    [[challenge sender] cancelAuthenticationChallenge:challenge];
    // inform the user that the user name and password
    // in the preferences are incorrect
}
}


希望这有助于

我在URLConnection中使用以下委托方法来实现auth

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{

if ([challenge previousFailureCount] == 0) {
    NSURLCredential *newCredential;
    newCredential=[NSURLCredential credentialWithUser:username
                                             password:password
                                          persistence:NSURLCredentialPersistenceNone];
    [[challenge sender] useCredential:newCredential
           forAuthenticationChallenge:challenge];

} else {

    [[challenge sender] cancelAuthenticationChallenge:challenge];
    // inform the user that the user name and password
    // in the preferences are incorrect
}
}

希望这有帮助