Adding Safari auto-fill password

How do I use this function in Xojo?

Like this? It doesn’t work.
Sub Opening() Handles Opening
// Xojo Declare for SecAddSharedWebCredential function
Declare Sub SecAddSharedWebCredential Lib “Security” (fqdn As CFStringRef, account As CFStringRef, password As CFStringRef, completionHandler As Ptr)

// Example usage
Dim fqdn As CFStringRef = “example.com
Dim account As CFStringRef = “user@example.com
Dim password As CFStringRef = “password123”

// Create a delegate for the completion handler
Dim completionHandlerDelegate As Ptr = AddressOf SharedWebCredentialCompletionHandler

// Call the SecAddSharedWebCredential function
SecAddSharedWebCredential(fqdn, account, password, completionHandlerDelegate)

End Sub
Public Sub SharedWebCredentialCompletionHandler(error As Ptr)
If error <> Nil Then
// Error occurred
Dim errorMessage As String = GetErrorMessage(error)

// Handle the error here or display an error message
MsgBox "Error occurred: " + errorMessage

Else
// Success
MsgBox “Shared web credential added successfully!”
End If
End Sub
Public Function GetErrorMessage(error As Ptr) As String
If error = Nil Then Return “”

Declare Function CFErrorCopyDescription Lib “CoreFoundation” (error As Ptr) As CFStringRef
Return CFErrorCopyDescription(error)
End Function

If you read that whole page, there are potentially two issues:

First, you’ve got this line wrong:

Dim completionHandlerDelegate As Ptr = AddressOf SharedWebCredentialCompletionHandler

That needs to be:

dim block as new ObjCBlock(AddressOf SharedWebCredentialCompletionHandler)

And then use block.handle instead of completionHandlerDelegate in your call.

Second (and more importantly) there’s this note:

Because a request involving shared web credentials may potentially require user interaction or other verification to be approved, this function is dispatched asynchronously; your code provides a completion handler that is called as soon as the results (if any) are available.

That means that the code may run preemptively (not on the main thread) and therefore the callback would also which is not supported in Xojo in any way, shape or form. Running any code in the completion handler will cause your app to crash if it’s not running on the main thread.

That fixed that part, but now it crashes when showing MsgBox, although if you break, the stirng appears properly:

Public Sub SharedWebCredentialCompletionHandler(error As Ptr)
If error <> Nil Then
// Error occurred
Dim errorMessage As String = GetErrorMessage(error)

// Handle the error here or display an error message
MsgBox errorMessage

Else
// Success
MsgBox “Shared web credential added successfully!”
End If
End Sub

Right. As I said above, that method probably runs on a preemptive thread. Running any code that requires the Xojo framework will crash your app.

Try passing Nil.