我發現了使用APNS和可可的真棒解釋。 APNS Pusher 現在我不想每次選擇我的SecIdentityRef(因爲我很懶)我試着把SecIdentityRef放到NSData中並保存到默認值。下一次應用程序啓動時,我再次加載它,它總是得到exc_bad_access。這裏是我添加的代碼:將SecIdentityRef存儲到NSUserDefaults
// 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"];
非常感謝。但我如何正確存儲選定的身份? – thomasguenzel 2012-04-13 12:09:53
我不確定。我看到'SecIdentityCopyCertificate()'和'SecCertificateCopyData()',然後在另一個方向'SecCertificateCreateWithData()'和'SecIdentityCreateWithCertificate()'。但是,我對這些東西並不十分熟悉,我不確定是否會通過將證書數據保存爲用戶默認值來破壞安全性。 'SecIdentityCopyCertificate()'的文檔也表示可以將輸出轉換爲'SecKeychainItemRef'並且我看到'SecKeychainItemCreatePersistentReference()',這看起來很有前途。然後,反過來,'SecKeychainItemCopyFromPersistentReference()'。 – 2012-04-13 12:40:16