Skip to content

Shortcuts and User Intents not working when app is not in background #22970

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

Merged
merged 1 commit into from
Apr 11, 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
109 changes: 64 additions & 45 deletions ios/brave-ios/App/iOS/Delegates/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
isPrivateBrowsing: browserViewController.privateBrowsingManager.isPrivateBrowsing
)
PrivacyReportsManager.scheduleVPNAlertsTask()

// Handle Custom Activity and Intents
if let currentActivity = connectionOptions.userActivities.first {
handleCustomUserActivityActions(scene, userActivity: currentActivity)
}
}

private func present(
Expand Down Expand Up @@ -321,23 +326,42 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
}

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
guard let scene = scene as? UIWindowScene else {
return
handleCustomUserActivityActions(scene, userActivity: userActivity)
}

func windowScene(
_ windowScene: UIWindowScene,
performActionFor shortcutItem: UIApplicationShortcutItem,
completionHandler: @escaping (Bool) -> Void
) {
if let browserViewController = windowScene.browserViewController {
QuickActions.sharedInstance.handleShortCutItem(
shortcutItem,
withBrowserViewController: browserViewController
)
completionHandler(true)
} else {
completionHandler(false)
}
}

if let url = userActivity.webpageURL {
switch UniversalLinkManager.universalLinkType(for: url, checkPath: false) {
case .buyVPN:
scene.browserViewController?.presentCorrespondingVPNViewController()
return
case .none:
break
}
func stateRestorationActivity(for scene: UIScene) -> NSUserActivity? {
return scene.userActivity
}
}

scene.browserViewController?.switchToTabForURLOrOpen(url, isPrivileged: true)
extension SceneDelegate {

private func handleCustomUserActivityActions(_ scene: UIScene, userActivity: NSUserActivity) {
guard let scene = scene as? UIWindowScene else {
return
}

handleCustomUserActivityTypes(scene, userActivity: userActivity)
handleCustomUserIntents(scene, userActivity: userActivity)
}

private func handleCustomUserActivityTypes(_ scene: UIWindowScene, userActivity: NSUserActivity) {
switch userActivity.activityType {
case CSSearchableItemActionType:
// Otherwise, check if the `NSUserActivity` is a CoreSpotlight item and switch to its tab or
Expand Down Expand Up @@ -432,59 +456,54 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
break
}

func switchToTabForIntentURL(intentURL: String?) {
if let browserViewController = scene.browserViewController {
guard let siteURL = intentURL, let url = URL(string: siteURL) else {
browserViewController.openBlankNewTab(
attemptLocationFieldFocus: false,
isPrivate: Preferences.Privacy.privateBrowsingOnly.value
)
return
}

browserViewController.switchToTabForURLOrOpen(
url,
isPrivate: Preferences.Privacy.privateBrowsingOnly.value,
isPrivileged: false
)
if let url = userActivity.webpageURL {
switch UniversalLinkManager.universalLinkType(for: url, checkPath: false) {
case .buyVPN:
scene.browserViewController?.presentCorrespondingVPNViewController()
return
case .none:
break
}

scene.browserViewController?.switchToTabForURLOrOpen(url, isPrivileged: true)
return
}
}

private func handleCustomUserIntents(_ scene: UIWindowScene, userActivity: NSUserActivity) {
if let intent = userActivity.interaction?.intent as? OpenWebsiteIntent {
switchToTabForIntentURL(intentURL: intent.websiteURL)
switchToTabForIntentURL(scene, intentURL: intent.websiteURL)
return
}

if let intent = userActivity.interaction?.intent as? OpenHistoryWebsiteIntent {
switchToTabForIntentURL(intentURL: intent.websiteURL)
switchToTabForIntentURL(scene, intentURL: intent.websiteURL)
return
}

if let intent = userActivity.interaction?.intent as? OpenBookmarkWebsiteIntent {
switchToTabForIntentURL(intentURL: intent.websiteURL)
switchToTabForIntentURL(scene, intentURL: intent.websiteURL)
return
}
}

func windowScene(
_ windowScene: UIWindowScene,
performActionFor shortcutItem: UIApplicationShortcutItem,
completionHandler: @escaping (Bool) -> Void
) {
if let browserViewController = windowScene.browserViewController {
QuickActions.sharedInstance.handleShortCutItem(
shortcutItem,
withBrowserViewController: browserViewController
private func switchToTabForIntentURL(_ scene: UIWindowScene, intentURL: String?) {
if let browserViewController = scene.browserViewController {
guard let siteURL = intentURL, let url = URL(string: siteURL) else {
browserViewController.openBlankNewTab(
attemptLocationFieldFocus: false,
isPrivate: Preferences.Privacy.privateBrowsingOnly.value
)
return
}

browserViewController.switchToTabForURLOrOpen(
url,
isPrivate: Preferences.Privacy.privateBrowsingOnly.value,
isPrivileged: false
)
completionHandler(true)
} else {
completionHandler(false)
}
}

func stateRestorationActivity(for scene: UIScene) -> NSUserActivity? {
return scene.userActivity
return
}
}

Expand Down
Loading