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

[log] Use objc2 #2344

Merged
merged 3 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/objc2-log.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"log": patch
"log-js": patch
---

Use `objc2` instead of `objc`.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions plugins/log/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ android_logger = "0.14"

[target."cfg(target_os = \"ios\")".dependencies]
swift-rs = "1"
objc = "0.2"
cocoa = "0.26"
objc2 = "0.5"
objc2-foundation = { version = "0.2", default-features = false, features = [
"std",
"NSString",
] }

[features]
colored = ["fern/colored"]
32 changes: 6 additions & 26 deletions plugins/log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,6 @@ pub const WEBVIEW_TARGET: &str = "webview";

#[cfg(target_os = "ios")]
mod ios {
use cocoa::base::id;
use objc::*;

const UTF8_ENCODING: usize = 4;
pub struct NSString(pub id);

impl NSString {
pub fn new(s: &str) -> Self {
// Safety: objc runtime calls are unsafe
NSString(unsafe {
let ns_string: id = msg_send![class!(NSString), alloc];
let ns_string: id = msg_send![ns_string,
initWithBytes:s.as_ptr()
length:s.len()
encoding:UTF8_ENCODING];

// The thing is allocated in rust, the thing must be set to autorelease in rust to relinquish control
// or it can not be released correctly in OC runtime
let _: () = msg_send![ns_string, autorelease];

ns_string
})
}
}

swift_rs::swift!(pub fn tauri_log(
level: u8, message: *const std::ffi::c_void
));
Expand Down Expand Up @@ -429,7 +404,12 @@ impl Builder {
log::Level::Info => 2,
log::Level::Warn | log::Level::Error => 3,
},
ios::NSString::new(message.as_str()).0 as _,
// The string is allocated in rust, so we must
// autorelease it rust to give it to the Swift
// runtime.
objc2::rc::Retained::autorelease_ptr(
objc2_foundation::NSString::from_str(message.as_str()),
) as _,
);
}
}),
Expand Down
Loading