我的应用程序在用户登录或登录时存储用户的凭据。当应用程序启动时,我会检查didFinishLaunchingWithOptions
我们是否存储了凭据。当通过点击 App Icon 或从 Xcode 启动它来启动应用程序时,这可以正常工作。
但是,当应用程序在后台被杀死并由于位置更改更新而被系统重新启动时,返回的凭据defaultCredentialForProtectionSpace
为 nil。当我再次正常重新启动应用程序时,凭据又回来了。
所以当[launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]
为真时,NSURLCredential
返回的NSURLCredentialStorage
是零;当它为假时,我们得到预期的凭证。
这是一些代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Other init things happen here, including setting up the NSURLProtectionSpace
NSURLCredentialStorage *credentialStorage = [NSURLCredentialStorage sharedCredentialStorage];
NSURLCredential *credential = [credentialStorage defaultCredentialForProtectionSpace:self.protectionSpace];
if (credential) {
// do something - XXX this does not happen when app is launched in background
}
}