下面是我用的方法:
NSArray *paths = [[NSBundle mainBundle] pathsForResourcesOfType:@"p12" inDirectory:nil];
NSMutableArray *idents = [NSMutableArray array];
for (NSString *certPath in paths) {
CFDataRef certData = (CFDataRef)[[NSData alloc] initWithContentsOfFile:certPath];
const void *keys[] = {kSecImportExportPassphrase};
const void *values[] = {(CFStringRef)kPassword}; // kPassword should be your password
CFDictionaryRef optsDict = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL);
OSStatus status = -1;
CFArrayRef items = NULL;
status = SecPKCS12Import(certData, optsDict, &items);
if (status == 0) { // noErr or errSecSuccess
CFDictionaryRef item = CFArrayGetValueAtIndex(items, 0);
SecIdentityRef bundleIdent = (SecIdentityRef)CFDictionaryGetValue(item, kSecImportItemIdentity);
[idents addObject:(id)bundleIdent];
}
if (optsDict) CFRelease(optsDict);
if (items) CFRelease(items);
if (certData) CFRelease(certData);
}
,瞧 - 你都SecIdentityRefs
在idents
陣列在您的處置。
編輯: 這裏的Apple document介紹如何做的正是你想要的。
您好,感謝看起來不錯。但我得到一個例外' - [__ NSCFDictionary長]:無法識別的選擇發送到實例0x2cab00'在線路設置密碼後。 – 2012-04-16 22:39:27
和@all,它根本不能在模擬器中工作。我花了整整一個小時才發現模擬器無法解碼。 – 2012-04-16 22:46:37
此代碼旨在獲得p12證書,並且它可以工作。它失敗的原因可能是從錯誤的文件類型到加密問題。順便說一句,「模擬器無法解碼」是什麼意思?作爲一個參考,你可以檢查https://developer.apple.com/library/mac/#documentation/security/conceptual/CertKeyTrustProgGuide/iPhone_Tasks/iPhone_Tasks.html – mit3z 2012-04-17 05:46:35