SFAuthorizationView-迅捷

时间:2018-12-10 09:59:59

标签: swift macos cocoa authorization

如何在快速项目中使用SFAuthorizationView。我已经尝试过通过创建桥接标头并导入SecurityInterface / SFAuthorizationView.h来进行尝试。我的代码如下。

  

我的参考资料-https://github.com/dominikhofacker/SFAuthorizationView

“授权视图和锁定”图标出现在我的输出屏幕上。但是当我单击锁定按钮时,什么也没发生。

import Cocoa

class ViewController: NSViewController {

@IBOutlet weak var auth: SFAuthorizationView!

@IBOutlet weak var button: NSButton!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    var items = [ AuthorizationItem(name: kAuthorizationRuleIsAdmin, valueLength: 0, value: nil, flags: 0), ]
    var rights = AuthorizationRights(count: UInt32(items.count), items: &items)
            auth.setAuthorizationRights(&rights)
    auth.setDelegate(self)
    auth.updateStatus(nil)
    button.isEnabled = isUnlocked()

}

override var representedObject: Any? {
    didSet {
    // Update the view, if already loaded.
    }
}

func isUnlocked() -> Bool {
    return auth.authorizationState() == SFAuthorizationViewUnlockedState;
}


override func authorizationViewDidAuthorize(_ view: SFAuthorizationView!) {
    button.isEnabled = isUnlocked()
}
override func authorizationViewDidDeauthorize(_ view: SFAuthorizationView!) {
    button.isEnabled = isUnlocked()
}
@IBAction func button(_ sender: Any) {
    print("hihi")
}

}

1 个答案:

答案 0 :(得分:0)

似乎您只是忘记了在项目 target

功能选项卡上禁用sandbox mode