加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

macos – 如何在swift中导入DER证书?

发布时间:2020-12-14 02:25:00 所属栏目:百科 来源:网络整理
导读:因此,似乎有一些方法已经改变,这些方法在我当前的代码库中已经破坏了.我目前收到以下错误: Cannot convert the expression’s type ‘(CFAllocator!,data: @lvalue NSData)’ to type ‘CFData!’ 这是相关的代码: let mainbun = NSBundle.pathForResource
因此,似乎有一些方法已经改变,这些方法在我当前的代码库中已经破坏了.我目前收到以下错误:

Cannot convert the expression’s type ‘(CFAllocator!,data: @lvalue NSData)’ to type ‘CFData!’

这是相关的代码:

let mainbun = NSBundle.pathForResource("mainkey",ofType: "der",inDirectory: "/myapppath")
var key: NSData = NSData(base64EncodedString: mainbun!,options: nil)!
var turntocert: SecCertificateRef = SecCertificateCreateWithData(kCFAllocatorDefault,data: key)

我有它使用桥接头,但我仍然希望能够直接在swift中创建证书引用.

更新:
这很有效

var bundle: NSBundle = NSBundle.mainBundle()
var mainbun = bundle.pathForResource("keyfile",ofType: "der")
var key: NSData = NSData(contentsOfFile: mainbun!)!
var turntocert: SecCertificateRef =
SecCertificateCreateWithData(kCFAllocatorDefault,key).takeRetainedValue()
在Swift中,SecCertificateCreateWithData返回Unmanaged类型.您需要使用takeRetainedValue()获取非托管引用的值.
let mainbun = NSBundle.pathForResource("mainkey",options: nil)!
var turntocert: SecCertificateRef = 
    SecCertificateCreateWithData(kCFAllocatorDefault,key).takeRetainedValue()

您遇到的核心问题是将CFData转换为NSData. See this question

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读