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

feat(iOS): added custom URL schemes handling in the AppDelegate class #969

Merged
merged 2 commits into from
Sep 3, 2024
Merged
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: 5 additions & 0 deletions .changes/ios-custom-url-schemes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": patch
---

On iOS, implement `application:openURL:options:` to handle custom URL schemes.
51 changes: 39 additions & 12 deletions src/platform_impl/ios/view.rs
Original file line number Diff line number Diff line change
@@ -555,7 +555,40 @@ pub fn create_delegate_class() {
YES
}

fn handle_deep_link(url: id) {
unsafe {
let absolute_url: id = msg_send![url, absoluteString];
let bytes = {
let bytes: *const c_char = msg_send![absolute_url, UTF8String];
bytes as *const u8
};

// 4 represents utf8 encoding
let len = msg_send![absolute_url, lengthOfBytesUsingEncoding: 4];
let bytes = std::slice::from_raw_parts(bytes, len);

let url = url::Url::parse(std::str::from_utf8(bytes).unwrap()).unwrap();

app_state::handle_nonuser_event(EventWrapper::StaticEvent(Event::Opened { urls: vec![url] }));
}
}

// custom URL schemes
// https://developer.apple.com/documentation/xcode/defining-a-custom-url-scheme-for-your-app
extern "C" fn application_open_url(
_self: &mut Object,
_cmd: Sel,
_app: id,
url: id,
_options: id,
) -> BOOL {
handle_deep_link(url);

YES
}

// universal links
// https://developer.apple.com/documentation/xcode/supporting-universal-links-in-your-app
extern "C" fn application_continue(
_: &mut Object,
_: Sel,
@@ -568,19 +601,8 @@ pub fn create_delegate_class() {
if webpage_url == nil {
return NO;
}
let absolute_url: id = msg_send![webpage_url, absoluteString];
let bytes = {
let bytes: *const c_char = msg_send![absolute_url, UTF8String];
bytes as *const u8
};

// 4 represents utf8 encoding
let len = msg_send![absolute_url, lengthOfBytesUsingEncoding: 4];
let bytes = std::slice::from_raw_parts(bytes, len);

let url = url::Url::parse(std::str::from_utf8(bytes).unwrap()).unwrap();

app_state::handle_nonuser_event(EventWrapper::StaticEvent(Event::Opened { urls: vec![url] }));
handle_deep_link(webpage_url);

YES
}
@@ -631,6 +653,11 @@ pub fn create_delegate_class() {
did_finish_launching as extern "C" fn(&mut Object, Sel, id, id) -> BOOL,
);

decl.add_method(
sel!(application:openURL:options:),
application_open_url as extern "C" fn(&mut Object, Sel, id, id, id) -> BOOL,
);

decl.add_method(
sel!(application:continueUserActivity:restorationHandler:),
application_continue as extern "C" fn(&mut Object, Sel, id, id, id) -> BOOL,
Loading