我发现了将 APNS 与可可一起使用的绝佳解释。APNS Pusher 现在我不想每次都选择我的 SecIdentityRef(因为我很懒),我尝试将 SecIdentityRef 放入 NSData 并将其保存为默认值。下次应用程序启动时,我会再次加载它,它总是会得到 exc_bad_access。这是我添加的代码:
// For saving
NSData *secRefData = [NSData dataWithBytes:[SFChooseIdentityPanel sharedChooseIdentityPanel].identity length:sizeof([SFChooseIdentityPanel sharedChooseIdentityPanel].identity)];
[[NSUserDefaults standardUserDefaults] setValue:secRefData forKey:@"identity"];
//For loading
NSData *secRefData = [[NSUserDefaults standardUserDefaults] valueForKey:@"identity"];
if([secRefData length] != 0) {
[[APNS sharedAPNS] setIdentity:(SecIdentityRef)CFRetain([secRefData bytes])];
}
我怎样才能让它工作?还有另一种我应该存储身份的方法吗?
编辑
因此,我通过保存身份名称找到了解决方案,并且在启动应用程序时,它会查看哪些可用身份具有此名称并使用具有正确名称的身份。这是代码:
//For loading
NSString *lastIdentityName = [[NSUserDefaults standardUserDefaults] valueForKey:@"identityName"];
if([lastIdentityName length] != 0) {
NSArray *allIdentities = [self identities];
for (id object in allIdentities) {
NSString *theName = [[[X509Certificate extractCertDictFromIdentity:(SecIdentityRef)object] valueForKey:@"Subject"] objectForKey:@"CommonName"];
if([theName isEqualToString:lastIdentityName]) {
[[APNS sharedAPNS] setIdentity:(SecIdentityRef)CFRetain((__bridge_retained SecIdentityRef)object)];
[[NSUserDefaults standardUserDefaults] setValue:[self identityName] forKey:@"identityName"];
// KVO trigger
[self willChangeValueForKey:@"identityName"];
[self didChangeValueForKey:@"identityName"];
}
}
}
//For saving
[[NSUserDefaults standardUserDefaults] setValue:[self identityName] forKey:@"identityName"];