Skip to content
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
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ 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 = [
objc2 = "0.6"
objc2-foundation = { version = "0.3", default-features = false, features = [
"std",
"NSDictionary",
"NSString",
Expand Down
43 changes: 26 additions & 17 deletions src/ios.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
use crate::{Browser, BrowserOptions, Error, ErrorKind, Result, TargetType};
use block2::Block;
use objc2::rc::Id;
use objc2::runtime::Bool;
use objc2::{class, msg_send, msg_send_id};
use std::ffi::c_void;

use objc2::{class, msg_send, rc::Retained, Encode, Encoding, MainThreadMarker};
use objc2_foundation::{NSDictionary, NSObject, NSString, NSURL};

fn app() -> Option<Id<NSObject>> {
unsafe { msg_send_id![class!(UIApplication), sharedApplication] }
use crate::{Browser, BrowserOptions, Error, ErrorKind, Result, TargetType};

/// Returns `UIApplication`
#[allow(non_snake_case)]
fn sharedApplication(_mtm: MainThreadMarker) -> Retained<NSObject> {
unsafe { msg_send![class!(UIApplication), sharedApplication] }
}

/// Fake `block` to not have to depend on the `block2` crate just to set this to an empty/`None` block.
#[repr(transparent)]
struct FakeBlock(*const c_void);

// SAFETY: The type is `#[repr(transparent)]` over a pointer (same layout as `Option<&block::Block<...>>`).
unsafe impl Encode for FakeBlock {
const ENCODING: Encoding = Encoding::Block;
}

fn open_url(
app: &NSObject,
url: &NSURL,
options: &NSDictionary,
handler: Option<&Block<dyn Fn(Bool)>>,
) {
unsafe { msg_send![app, openURL: url, options: options, completionHandler: handler] }
#[doc(alias = "openURL_options_completionHandler")]
fn open_url(app: &NSObject, url: &NSURL, options: &NSDictionary) {
let fake_handler = FakeBlock(std::ptr::null());
unsafe { msg_send![app, openURL: url, options: options, completionHandler: fake_handler] }
}

/// Deal with opening of browsers on iOS/tvOS/visionOS.
Expand All @@ -34,10 +42,11 @@ pub(super) fn open_browser_internal(
return Ok(());
}

let app = app().ok_or(Error::new(
let mtm = MainThreadMarker::new().ok_or(Error::new(
ErrorKind::Other,
"UIApplication is null, can't open url",
"UIApplication must be retrieved on the main thread",
))?;
let app = sharedApplication(mtm);

// Create ns string class from our string
let url_string = NSString::from_str(url);
Expand All @@ -50,6 +59,6 @@ pub(super) fn open_browser_internal(
let options = NSDictionary::new();

// Open url
open_url(&app, &url_object, &options, None);
open_url(&app, &url_object, &options);
Ok(())
}