cocoa - Getting bundle identifier in Swift -
i'm trying bundleid using app's directory , i'm getting error: exc_bad_access(code=1, address=0xd8)
application.directory! string
let startcstring = (application.directory! nsstring).utf8string //type: unsafepointer<int8> let convertedcstring = unsafepointer<uint8>(startcstring) //cfurlcreatefromfilerepresentation needs <uint8> pointer let length = application.directory!.lengthofbytesusingencoding(nsutf8stringencoding) let dir = cfurlcreatefromfilesystemrepresentation(kcfallocatordefault, convertedcstring, length, false) let bundle = cfbundlecreate(kcfallocatordefault, dir) let result = cfbundlegetidentifier(bundle)
and error on result line.
what doing wrong here?
one potential problem code pointer obtained in
let startcstring = (application.directory! nsstring).utf8string //type: unsafepointer<int8>
is valid long temporary nsstring
exists. conversion c string can done "automatically" compiler (compare string value unsafepointer<uint8> function parameter behavior), working version should be
let dir = cfurlcreatefromfilesystemrepresentation(kcfallocatordefault, path, int(strlen(path)), false) let bundle = cfbundlecreate(kcfallocatordefault, dir) let result = cfbundlegetidentifier(bundle)
but can create nsbundle
given path , obtain identifier:
let ident = nsbundle(path: path)!.bundleidentifier!
full example added error checking:
let path = "/applications/textedit.app" if let bundle = nsbundle(path: path) { if let ident = bundle.bundleidentifier { print(ident) // com.apple.textedit } else { print("bundle has no identifier") } } else { print("bundle not found") }
Comments
Post a Comment