Skip to content
Closed
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: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ jni = "0.21"
ndk-context = "0.1"

[target.'cfg(any(target_os = "ios", target_os = "tvos", target_os = "visionos"))'.dependencies]
block2 = "0.5.0"
objc2 = "0.5.1"
objc2-foundation = { version = "0.2.0", default-features = false, features = [
block2 = "0.6.0"
objc2 = "0.6.0"
objc2-foundation = { version = "0.3.0", default-features = false, features = [
"std",
"NSDictionary",
"NSString",
Expand Down
25 changes: 20 additions & 5 deletions src/ios.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
use crate::{Browser, BrowserOptions, Error, ErrorKind, Result, TargetType};
use block2::Block;
use objc2::rc::Id;
use objc2::rc::Retained;
use objc2::runtime::Bool;
use objc2::{class, msg_send, msg_send_id};
use objc2::{class, msg_send, MainThreadMarker};
use objc2_foundation::{NSDictionary, NSObject, NSString, NSURL};

fn app() -> Option<Id<NSObject>> {
unsafe { msg_send_id![class!(UIApplication), sharedApplication] }
fn app(mtm: MainThreadMarker) -> Option<Retained<NSObject>> {
let _ = mtm;
// SAFETY: The signature is correct, and we hold `MainThreadMarker`, so we
// know we're on the main thread where it's safe to access the shared
// UIApplication.
//
// NOTE: `sharedApplication` is declared as returning non-NULL, but it
// will only do so inside `UIApplicationMain`; if called outside, the
// shared application is NULL.
unsafe { msg_send![class!(UIApplication), sharedApplication] }
}

fn open_url(
Expand All @@ -29,12 +37,19 @@ pub(super) fn open_browser_internal(
// ensure we're opening only http/https urls, failing otherwise
let url = target.get_http_url()?;

let mtm = MainThreadMarker::new().ok_or_else(|| {
Error::new(
ErrorKind::Other,
"Cannot open URL from a thread that is not the main thread",
)
})?;

// always return true for a dry run
if options.dry_run {
return Ok(());
}

let app = app().ok_or(Error::new(
let app = app(mtm).ok_or(Error::new(
ErrorKind::Other,
"UIApplication is null, can't open url",
))?;
Expand Down
Loading