diff --git a/.changes/objc2-log.md b/.changes/objc2-log.md new file mode 100644 index 00000000..01c2f20b --- /dev/null +++ b/.changes/objc2-log.md @@ -0,0 +1,6 @@ +--- +"log": patch +"log-js": patch +--- + +Use `objc2` instead of `objc`. diff --git a/Cargo.lock b/Cargo.lock index ec0b3f4f..41543309 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6645,10 +6645,10 @@ version = "2.2.0" dependencies = [ "android_logger", "byte-unit", - "cocoa", "fern", "log", - "objc", + "objc2", + "objc2-foundation", "serde", "serde_json", "serde_repr", diff --git a/plugins/log/Cargo.toml b/plugins/log/Cargo.toml index 895ab886..21f03b81 100644 --- a/plugins/log/Cargo.toml +++ b/plugins/log/Cargo.toml @@ -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"] diff --git a/plugins/log/src/lib.rs b/plugins/log/src/lib.rs index 6b07c9d2..e7848ff4 100644 --- a/plugins/log/src/lib.rs +++ b/plugins/log/src/lib.rs @@ -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 )); @@ -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 _, ); } }),