问题描述
我使用SMCopyAllJobDictionaries
和SMJobCopyDictionary
检查应用程序在Mac中是否为Login Item,但在10.10中已弃用.那么他们官方推荐的替代品是什么?
I use SMCopyAllJobDictionaries
and SMJobCopyDictionary
to check whether app is Login Item in Mac, but in 10.10, they are deprecated. So what are their official recommended replacement?
推荐答案
您不需要替换.只需使用SMLoginItemSetEnabled
,如果成功,则将值存储在NSUserDefaults
中.每次启动应用程序时,使用NSUserDefaults
中的存储值调用SMLoginItemSetEnabled
,以检查是否有任何更改.如果返回false
,请相应地设置用户默认值,否则状态仍然与您先前在NSUserDefaults
中记住的状态相同.
You don't need a replacement. Just use SMLoginItemSetEnabled
, if it succeeds, store the value in NSUserDefaults
. Every time the app launches, call SMLoginItemSetEnabled
with the stored value from your NSUserDefaults
to check if something changed. If it returns false
, set your user defaults value accordingly, otherwise the status is still the same one you previously remembered in your NSUserDefaults
.
示例:
import Foundation
import ServiceManagement
final class LoginItem {
let identifier: String
private let nc = NSUserDefaults.standardUserDefaults()
init(identifier: String) {
self.identifier = identifier
}
var enabled: Bool {
return nc.boolForKey(defaultKey)
}
func setEnabled(enabled: Bool) -> Bool {
if SMLoginItemSetEnabled(identifier, enabled) {
nc.setBool(enabled, forKey: defaultKey)
return true
}
return false
}
func validate() -> Bool {
if setEnabled(enabled) {
return true
}
nc.removeObjectForKey(defaultKey)
return false
}
private var defaultKey: String {
return "SMLoginItem-" + identifier
}
}
只需在启动时调用validate
即可验证打开/关闭状态.
Just call validate
on startup to validate the on/off state.
这篇关于不建议使用SMCopyAllJobDictionaries和SMJobCopyDictionary,那么它们的替代品是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!