如何在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#.NETNetworkCredential
中,凭据是.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
}
}
希望这有帮助