Skip to content

Commit ad652e5

Browse files
CampioneDevJason Tsai
and
Jason Tsai
authored
feat(iOS): added custom URL schemes handling in the AppDelegate class (#969)
* feat(iOS): added custom URL schemes handling in the `AppDelegate` class Until now, only ["associated domains"](https://developer.apple.com/documentation/xcode/supporting-associated-domains) were handled, using the `application_continue` function, that implements [this Swift method from the `UIApplicationDelegate` class](https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623072-application). For [custom URL schemes](https://developer.apple.com/documentation/xcode/defining-a-custom-url-scheme-for-your-app), I added a new `application_open_url` function that matches the signature of [this other Swift method](https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623112-application). Most of the code of the pre-existing `application_continue` has been moved into a separate `handle_deep_link` function so the new `application_open_url` can call it as well. I believe using the same `Event::Opened` event is appropriate in both situations. Since the scheme is part of the URL, a listener can differentiate between them if needed. **Tauri:** since we are emitting the same `Event::Opened` event, this change works automatically with the ["Deep Linking" plugin](https://v2.tauri.app/plugin/deep-linking/) without further modifications. Custom URL schemes in mobile apps are essential, for example, when dealing with OAuth redirect URLs. * Update .changes/ios-custom-url-schemes.md Co-authored-by: Jason Tsai <[email protected]> --------- Co-authored-by: Jason Tsai <[email protected]>
1 parent b6adbac commit ad652e5

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

.changes/ios-custom-url-schemes.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tao": patch
3+
---
4+
5+
On iOS, implement `application:openURL:options:` to handle custom URL schemes.

src/platform_impl/ios/view.rs

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,40 @@ pub fn create_delegate_class() {
555555
YES
556556
}
557557

558+
fn handle_deep_link(url: id) {
559+
unsafe {
560+
let absolute_url: id = msg_send![url, absoluteString];
561+
let bytes = {
562+
let bytes: *const c_char = msg_send![absolute_url, UTF8String];
563+
bytes as *const u8
564+
};
565+
566+
// 4 represents utf8 encoding
567+
let len = msg_send![absolute_url, lengthOfBytesUsingEncoding: 4];
568+
let bytes = std::slice::from_raw_parts(bytes, len);
569+
570+
let url = url::Url::parse(std::str::from_utf8(bytes).unwrap()).unwrap();
571+
572+
app_state::handle_nonuser_event(EventWrapper::StaticEvent(Event::Opened { urls: vec![url] }));
573+
}
574+
}
575+
576+
// custom URL schemes
577+
// https://developer.apple.com/documentation/xcode/defining-a-custom-url-scheme-for-your-app
578+
extern "C" fn application_open_url(
579+
_self: &mut Object,
580+
_cmd: Sel,
581+
_app: id,
582+
url: id,
583+
_options: id,
584+
) -> BOOL {
585+
handle_deep_link(url);
586+
587+
YES
588+
}
589+
558590
// universal links
591+
// https://developer.apple.com/documentation/xcode/supporting-universal-links-in-your-app
559592
extern "C" fn application_continue(
560593
_: &mut Object,
561594
_: Sel,
@@ -568,19 +601,8 @@ pub fn create_delegate_class() {
568601
if webpage_url == nil {
569602
return NO;
570603
}
571-
let absolute_url: id = msg_send![webpage_url, absoluteString];
572-
let bytes = {
573-
let bytes: *const c_char = msg_send![absolute_url, UTF8String];
574-
bytes as *const u8
575-
};
576604

577-
// 4 represents utf8 encoding
578-
let len = msg_send![absolute_url, lengthOfBytesUsingEncoding: 4];
579-
let bytes = std::slice::from_raw_parts(bytes, len);
580-
581-
let url = url::Url::parse(std::str::from_utf8(bytes).unwrap()).unwrap();
582-
583-
app_state::handle_nonuser_event(EventWrapper::StaticEvent(Event::Opened { urls: vec![url] }));
605+
handle_deep_link(webpage_url);
584606

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

656+
decl.add_method(
657+
sel!(application:openURL:options:),
658+
application_open_url as extern "C" fn(&mut Object, Sel, id, id, id) -> BOOL,
659+
);
660+
634661
decl.add_method(
635662
sel!(application:continueUserActivity:restorationHandler:),
636663
application_continue as extern "C" fn(&mut Object, Sel, id, id, id) -> BOOL,

0 commit comments

Comments
 (0)