从macOS中的授权插件机制读取任何文件
下面的代码在控制台应用程序中运行良好,但在macOS授权插件的机制中调用时无法读取任何文件(返回false)从macOS中的授权插件机制读取任何文件,macos,security,plugins,authorization,bundle,Macos,Security,Plugins,Authorization,Bundle,下面的代码在控制台应用程序中运行良好,但在macOS授权插件的机制中调用时无法读取任何文件(返回false) func readConfigFile() -> Bool { globalLogger("readConfigFile: Start") //this is the file. we will write to and read from it let file = "/Users/Shared/macLA/mlaConfig
func readConfigFile() -> Bool {
globalLogger("readConfigFile: Start")
//this is the file. we will write to and read from it
let file = "/Users/Shared/macLA/mlaConfig.txt"
let text = "some text1" //just a text
let fileURL = URL(fileURLWithPath:file)
globalLogger("readConfigFile: fileURL= \(fileURL)")
//writing
do {
globalLogger("readConfigFile: writing")
try text.write(to: fileURL, atomically: false, encoding: .utf8)
}
catch {
/* error handling here */
globalLogger("readConfigFile: error writing")
return false
}
//reading
do {
let text2 = try String(contentsOf: fileURL, encoding: .utf8)
globalLogger("file content = \(text2)")
}
catch {
/* error handling here */
globalLogger("readConfigFile: error reading")
return false
}
globalLogger("readConfigFile: End1")
return true
}
我得到的日志是:
mechanism -1 will invoke
invoke
readConfigFile: Start
readConfigFile: fileURL= file:///Users/Shared/macLA/mlaConfig.txt
readConfigFile: writing
readConfigFile: error writing
使用Xcode 12.0.1和swift 5.0
我已经用管理员和普通权限进行了测试。
我必须从文件中读取一些配置文本,这样任何用户都可以更改配置,而无需重新构建授权插件包。那么,还有其他选择吗
另一次尝试是在捆绑包中添加文件。它确实奏效了。我使用的代码复制到下面
func readConfigFile() -> Bool {
globalLogger("readConfigFile: Start")
//this is the file. we will write to and read from it
// let file = "/Users/Shared/macLA/mlaConfig.txt"
let file = "/Library/Security/SecurityAgentPlugins/LoginUIAuthPlugin.bundle/Contents/Resources/mlaConfig.txt"
// let text = "some text1" //just a text
let fileURL = URL(fileURLWithPath:file)
globalLogger("readConfigFile: fileURL= \(fileURL)")
/* commented this out since we cant write to a file within bundle
//writing
do {
globalLogger("readConfigFile: writing")
try text.write(to: fileURL, atomically: false, encoding: .utf8)
}
catch {
/* error handling here */
globalLogger("readConfigFile: error writing")
return false
}
*/
//reading
do {
let text2 = try String(contentsOf: fileURL, encoding: .utf8)
globalLogger("file content = \(text2)")
}
catch {
/* error handling here */
globalLogger("readConfigFile: error reading")
return false
}
globalLogger("readConfigFile: End1")
return true
}
我将制作另一个基于macOS GUI的普通应用程序,让用户在登录mac后配置一切
那么,除了在捆绑包中添加文件之外,还有其他选择吗?这是配置设置的最佳方法吗