macos – Swift中的Cocoa授权

macos – Swift中的Cocoa授权,第1张

概述这是我第一次写 Swift,Cocoa(有Cocoa Touch的经验)和使用授权,所以老实说我不知道​​我是否在正确的轨道上.我试图修改hosts文件,这需要用户身份验证,但AuthorizationCreate和AuthorizationExecuteWithPrivileges方法都给出了错误. var authorizationRef:AuthorizationRefvar status 这是我第一次写 Swift,Cocoa(有Cocoa touch的经验)和使用授权,所以老实说我不知道​​我是否在正确的轨道上.我试图修改hosts文件,这需要用户身份验证,但AuthorizationCreate和AuthorizationExecuteWithPrivileges方法都给出了错误.

var authorizationRef:AuthorizationRefvar status:Osstatusstatus = AuthorizationCreate(nil,environment:kAuthorizationEmptyEnvironment,flags:kAuthorizationFlagDefaults,authorization:&authorizationRef)let overwrite_hosts = "echo \(hostsContents) > /private/etc/hosts"let args = [overwrite_hosts.cStringUsingEnCoding(NSUTF8StringEnCoding)]status = AuthorizationExecuteWithPrivileges(authorizationRef,pathToTool:"/bin/sh",options:kAuthorizationFlagDefaults,arguments:args,communicationsPipe:nil)

我调用AuthorizationCreate抛出“Type”()’不符合协议’AuthorizationRef’“并且我对AuthorizationExecuteWithPrivileges的调用正在抛出”无法找到接受所提供参数的’__conversion’的重载“

有任何想法吗?我接近这个错误吗?

谢谢你的帮助!

解决方法 我能够通过AppleScript弄清楚如何做到这一点,但你应该能够使用我之前尝试过的授权方法来做到这一点,因此请保持这个问题的开放性.任何人都在寻找快速解决方案(没有实施错误检查)你可以使用我在下面写的内容:

func doScriptWithadmin(inScript:String) -> String{    let script = "do shell script \"\(inScript)\" with administrator privileges"    var appleScript = NSAppleScript(source: script)    var eventResult = appleScript.executeAndReturnError(nil)    if !eventResult {        return "ERROR"    }else{        return eventResult.stringValue    }}
总结

以上是内存溢出为你收集整理的macos – Swift中的Cocoa授权全部内容,希望文章能够帮你解决macos – Swift中的Cocoa授权所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1009319.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存