objective-cmacosrust

Calling NSApplication from Rust using objc


I am trying to learn by doing a project of allowing AppKit certain functions , related with App and Window creation to be called from Rust using objc.

extern crate objc;
use objc::runtime::{Class,Object};
use objc::{msg_send, sel, sel_impl};

pub fn create_app(){
    unsafe {
        let app_class =Class::get("NSApplication").unwrap();
        let app:&Object = msg_send![app_class,sharedApplication];
        let _:&Object = msg_send![app_class,run];
    }
}

However if i try to run this an main.rs I get the following:

thread 'main' panicked at src/lib.rs:7:52:
called `Option::unwrap()` on a `None` value

I guess the objective-c is unable to find the NSApplication Class of AppKit but why and how can i resolve such an issue?

I am running all of this from a M3 MacOs just to give insight on my machine.


Solution

  • I dont have the full knowledge to explain as to why the below solution was correct , if anyone could explain it better you are more then welcome. Seems like running it using the following command helps it succesfully find the AppKit classes using the objc2 crate.

    RUSTFLAGS="-C link-arg=-lobjc -C link-arg=-framework -C link-arg=AppKit" cargo run --release