Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use callback functions? #118

Closed
yyon opened this issue Nov 18, 2022 · 2 comments
Closed

How to use callback functions? #118

yyon opened this issue Nov 18, 2022 · 2 comments

Comments

@yyon
Copy link

yyon commented Nov 18, 2022

Hello,
I'm trying to call a function in ObjC that takes a callback function, and I'm hoping to have that call back to a rust function. The function I'm trying to call is this one: https://developer.apple.com/documentation/webkit/wkwebview/1415017-evaluatejavascript?language=objc.

When I pass null to the callback function, it works:

            let callback_pointer = std::ptr::null::<*const std::ffi::c_void>(); // *const *const core:ffi::c_void
            let _: id = objc::msg_send![controller, evaluateJavaScript:javascript_string completionHandler:callback_pointer];
            

I tried this:

> struct NSError(id);
> extern fn javascript_callback_macos(value: id, error: NSError) {
>     println!("javascript_callback_macos");
> }
> ...
> let _: id = objc::msg_send![controller, evaluateJavaScript:javascript_string completionHandler:javascript_callback_macos as extern fn(id, NSError)];

but it crashes without an error message

@madsmtm
Copy link

madsmtm commented Nov 19, 2022

I've received a very similar question over here madsmtm/objc2#269, you can probably adapt the answer to your use-case (if not, please do say so, then I'd be happy to provide more details).

@yyon
Copy link
Author

yyon commented Nov 21, 2022

That worked, thanks so much for your help! I don't know about objective C, so I didn't realize they were blocks and not functions.

@yyon yyon closed this as completed Nov 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants