下面是我用的方法:
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