Cocoa获取Keychain证书列表
通过Security.framework
1 - (IBAction)loadCerList1:(id)sender { 2 NSDictionary *options = @{(__bridge id)kSecClass: (__bridge id)kSecClassCertificate, 3 (__bridge id)kSecMatchLimit: (__bridge id)kSecMatchLimitAll}; 4 CFArrayRef certs = NULL; 5 __unused OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)options, (CFTypeRef *)&certs); 6 NSArray *certificates = CFBridgingRelease(certs); 7 8 [self.onePopUpButton removeAllItems]; 9 10 for (int i=0;i<[certificates count];i++) { 11 SecCertificateRef certificate = (__bridge SecCertificateRef)([certificates objectAtIndex:i]); 12 NSString *name = CFBridgingRelease(SecCertificateCopySubjectSummary(certificate)); 13 [self.onePopUpButton addItemWithTitle:name]; 14 15 } 16 }
通过NSTask执行security find-identity
终端命令
1 security find-identity -v -p codesigning
Cocoa
1 (IBAction)loadCerList2:(id)sender { 2 [self.twoPopUpButton removeAllItems]; 3 4 NSTask *certTask = [[NSTask alloc] init]; 5 [certTask setLaunchPath:@"/usr/bin/security"]; 6 [certTask setArguments:[NSArray arrayWithObjects:@"find-identity", @"-v", @"-p", @"codesigning", nil]]; 7 8 NSPipe *pipe = [NSPipe pipe]; 9 [certTask setStandardOutput:pipe]; 10 [certTask setStandardError:pipe]; 11 NSFileHandle *handle=[pipe fileHandleForReading]; 12 [certTask launch]; 13 [NSThread detachNewThreadSelector:@selector(watchGetCerts:) toTarget:self withObject:handle]; 14 } 15 - (void)watchGetCerts:(NSFileHandle*)streamHandle { 16 @autoreleasepool { 17 18 NSString *securityResult = [[NSString alloc] initWithData:[streamHandle readDataToEndOfFile] encoding:NSASCIIStringEncoding]; 19 // Verify the security result 20 if (securityResult == nil || securityResult.length < 1) { 21 // Nothing in the result, return 22 return; 23 } 24 NSArray *rawResult = [securityResult componentsSeparatedByString:@"\""]; 25 NSMutableArray *tempGetCertsResult = [NSMutableArray arrayWithCapacity:20]; 26 for (int i = 0; i <= [rawResult count] - 2; i+=2) { 27 NSLog(@"i:%d", i+1); 28 if (rawResult.count - 1 < i + 1) { 29 // Invalid array, don't add an object to that position 30 } else { 31 // Valid object 32 [tempGetCertsResult addObject:[rawResult objectAtIndex:i+1]]; 33 [self.twoPopUpButton addItemWithTitle:[rawResult objectAtIndex:i+1]]; 34 } 35 } 36 } 37 }

浙公网安备 33010602011771号