Skip to content

Commit 44834dc

Browse files
committed
[log] Use objc2
This makes it easier to correctly do the NSString construction.
1 parent e76272b commit 44834dc

File tree

4 files changed

+18
-30
lines changed

4 files changed

+18
-30
lines changed

.changes/objc2-log.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-plugin-log": patch
3+
---
4+
5+
Use `objc2` instead of `objc`.

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/log/Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ android_logger = "0.14"
3939

4040
[target."cfg(target_os = \"ios\")".dependencies]
4141
swift-rs = "1"
42-
objc = "0.2"
43-
cocoa = "0.26"
42+
objc2 = "0.5"
43+
objc2-foundation = { version = "0.2", default-features = false, features = [
44+
"std",
45+
"NSString",
46+
] }
4447

4548
[features]
4649
colored = ["fern/colored"]

plugins/log/src/lib.rs

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,6 @@ pub const WEBVIEW_TARGET: &str = "webview";
3535

3636
#[cfg(target_os = "ios")]
3737
mod ios {
38-
use cocoa::base::id;
39-
use objc::*;
40-
41-
const UTF8_ENCODING: usize = 4;
42-
pub struct NSString(pub id);
43-
44-
impl NSString {
45-
pub fn new(s: &str) -> Self {
46-
// Safety: objc runtime calls are unsafe
47-
NSString(unsafe {
48-
let ns_string: id = msg_send![class!(NSString), alloc];
49-
let ns_string: id = msg_send![ns_string,
50-
initWithBytes:s.as_ptr()
51-
length:s.len()
52-
encoding:UTF8_ENCODING];
53-
54-
// The thing is allocated in rust, the thing must be set to autorelease in rust to relinquish control
55-
// or it can not be released correctly in OC runtime
56-
let _: () = msg_send![ns_string, autorelease];
57-
58-
ns_string
59-
})
60-
}
61-
}
62-
6338
swift_rs::swift!(pub fn tauri_log(
6439
level: u8, message: *const std::ffi::c_void
6540
));
@@ -429,7 +404,12 @@ impl Builder {
429404
log::Level::Info => 2,
430405
log::Level::Warn | log::Level::Error => 3,
431406
},
432-
ios::NSString::new(message.as_str()).0 as _,
407+
// The string is allocated in rust, so we must
408+
// autorelease it rust to give it to the Swift
409+
// runtime.
410+
objc2::rc::Retained::autorelease_ptr(
411+
objc2_foundation::NSString::from_str(message.as_str()),
412+
) as _,
433413
);
434414
}
435415
}),

0 commit comments

Comments
 (0)