growing up with swift, the concept of pointer and low-level c stuff is unfamiliar to me and, at least for me, hard to understand. I have an Array<String>
, which i need to create a Pointer to (thats how I understand pointer). The pointer of type UnsafePointer<UnsafeMutablePointer<Int8>>
is required for the function AuthorizationExecuteWithPrivileges()
. I read the raywenderlich-guide on working with pointers in swift and used google, but I am still not able to get it to work.
Thats my code (which I borrowed mostly from here):
func runAsAdmin(command: String, arguments: [String]) {
var status = OSStatus()
var authorizationRef: AuthorizationRef?
status = AuthorizationCreate(nil, nil, [], &authorizationRef)
if status != errAuthorizationSuccess {
print("Error Creating Initial Authorization: \(status)")
}
let authFlags: AuthorizationFlags = [.interactionAllowed, .preAuthorize, .extendRights]
var rightItems = [AuthorizationItem(name: kAuthorizationRightExecute, valueLength: 0, value: nil, flags: 0)]
var rights = AuthorizationRights.init(count: UInt32(rightItems.count), items: &rightItems)
status = AuthorizationCopyRights(authorizationRef!, &rights, nil, authFlags, nil)
if status != errAuthorizationSuccess {
print("Copy Rights Unsuccessful: \(status)")
}
var pipe: FILE?
let tool = UnsafePointer<Int8>((command as NSString).utf8String!) //that seems good
var pToArgs = arguments.flatMap({($0 as NSString).utf8String}) //thats where the pain starts
var buffer = pre.withUnsafeMutableBufferPointer { // not sure about this approach, probably only returns the first value
return $0.baseAddress
}
status = AuthorizationExecuteWithPrivileges(authorizationRef!, tool, authFlags, buffer!, &pipe)
}
Any suggestions?