Skip to content

Commit

Permalink
Merge pull request #22970 from brave/ios/bugfix/activity-action-external
Browse files Browse the repository at this point in the history
  • Loading branch information
soner-yuksel authored Apr 11, 2024
2 parents 4b0ecda + 010a19b commit e4ecdb5
Showing 1 changed file with 64 additions and 45 deletions.
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

0 comments on commit e4ecdb5

Please sign in to comment.