Descriptive Error Messages for the Security Framework

Have you ever wondered how to translate OSStatus codes like -9612 from e.g. SSLHandshake() to something user presentable using Cocoa?

Here is how!


NSString* OPStringForSecErrorCode(OSStatus status)
/*" Returns the error string from the SecErrorMessages.strings 
    file found in the Security.framework bundle for the error (status) 
    code given. "*/
{    
    NSBundle* secBundle   = [NSBundle bundleWithIdentifier: @"com.apple.security"];
    // Convert status to Int32 string representation, e.g. "-25924":
    NSString* keyString   = [NSString stringWithFormat: @"%d", status];     
    NSString* errorString = [secBundle localizedStringForKey: keyString 
                                                       value: keyString 
                                                       table: @"SecErrorMessages"];
    return errorString;
}

This code is also part of the BSD-licensed OPNetwork.framework (OPSSLSocket class).

Have fun!

Dirk

Print this page  PDF version

Leave a comment...

Your Name

Your Web site URL (optional)

Your e-mail address (optional)

Title

Your comment here please:
Note: You can use Wiki markup to format your comment. No HTML please.