यदि आप NSURLCredential
का उपयोग NSUserDefaults और न ही किसी कीचेन आवरण के साथ परेशान करने के लिए जरूरत नहीं है। वास्तव में NSURLCredential
का उपयोग करने के लिए बहुत आसान है, क्योंकि यह आपको पर कोड के दो पंक्तियों में कीचेन दोनों उपयोगकर्ता नाम और पासवर्ड दोनों स्टोर करने की अनुमति देता है।
आपका कोड ऐसा ही कुछ एक बार उपयोगकर्ता के प्रवेश होगा:
NSURLCredential *credential;
credential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistencePermanent];
[[NSURLCredentialStorage sharedCredentialStorage] setCredential:credential forProtectionSpace:self.loginProtectionSpace];
फिर, हर बार एप्लिकेशन शुरू की है, आप जाँच कर सकते हैं अपने उपयोगकर्ता पहले से ही किसी भी क्रेडेंशियल के लिए खोज के द्वारा में लॉग इन किया गया था कि क्या आदेश स्वचालित रूप से आपके उपयोगकर्ता फिर से लॉग इन (यदि आवश्यकता हुई):
NSURLCredential *credential;
NSDictionary *credentials;
credentials = [[NSURLCredentialStorage sharedCredentialStorage] credentialsForProtectionSpace:self.loginProtectionSpace];
credential = [credentials.objectEnumerator nextObject];
NSLog(@"User %@ already connected with password %@", credential.user, credential.password);
तुम भी जब उपयोगकर्ता को लॉग आउट करना चाहता है क्रेडेंशियल साफ करने के लिए की जरूरत है:
NSURLCredential *credential;
NSDictionary *credentials;
credentials = [[NSURLCredentialStorage sharedCredentialStorage] credentialsForProtectionSpace:self.loginProtectionSpace];
credential = [credentials.objectEnumerator nextObject];
[[NSURLCredentialStorage sharedCredentialStorage] removeCredential:credential forProtectionSpace:self.loginProtectionSpace];
loginProtectionSpace
सभी के लिए एक बार बनाया गया है। कृपया, इस नमूना कोड को ध्यान दें कि इस स्थान में केवल एक प्रमाण पत्र है, जो आम तौर पर तब तक होता है जब तक आप कई खातों का प्रबंधन नहीं करते।
यहाँ कैसे आप एक NSURLProtectionSpace
बन जाएगा का एक उदाहरण है: जवाब देना के लिए
NSURL *url = [NSURL URLWithString:@"http://www.example.com"];
self.loginProtectionSpace = [[NSURLProtectionSpace alloc] initWithHost:url.host
port:[url.port integerValue]
protocol:url.scheme
realm:nil
authenticationMethod:NSURLAuthenticationMethodHTTPDigest];
धन्यवाद, हाँ यह है कि क्या मैं पुराने साख रखने के लिए करते हैं। मैं जो भी टालना चाहूंगा वह हर ऐप पर लॉगिन के लिए प्रारंभिक पोस्ट है, मैं बस सोच रहा था कि क्या AFNetworking द्वारा संचालित कुकी का जीवनकाल बढ़ाया जा सकता है। –
दरअसल, कृपया NSUserDefaults में उपयोगकर्ता नाम और पासवर्ड स्टोर न करें। इसके बजाय कीचेन का प्रयोग करें। ऐप्पल एक कीचेनवापर वितरित करता है जो मूल रूप से एक ही चीज़ करता है, लेकिन अधिक सुरक्षित रूप से। – eddieroger