You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

UITextView点击同应用Branch.io universal deep link跳转Safari问题求助

Hey there! I’ve dealt with this exact scenario before—Branch links working perfectly from external apps like WhatsApp but detouring through Safari when tapped inside your own app’s UITextView. The issue boils down to how UITextView handles links by default: it hands them off to the system, which opens Safari instead of letting your app’s Branch handler take over. Here’s how to fix it:

The key is to take control of link interactions in your UITextView using its delegate method. This lets you decide whether to let the system handle the link or process it internally with Branch.

First, set your view controller as the delegate of your UITextView (either in code or Interface Builder):

override func viewDidLoad() {
    super.viewDidLoad()
    messageTextView.delegate = self
}

Then implement the shouldInteractWith URL delegate method to intercept Branch links:

extension YourMessageViewController: UITextViewDelegate {
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        // Replace "your-app.branch.app" with your actual Branch domain (e.g., "yourapp.link")
        guard URL.host?.contains("branch.app") ?? false || URL.host == "yourapp.link" else {
            // Let the system handle non-Branch links normally (like external websites)
            return true
        }
        
        // Manually trigger Branch's deep link processing
        Branch.getInstance().handleDeepLink(URL)
        
        // Return false to prevent the system from opening Safari
        return false
    }
}

2. Verify Your Branch Initialization & Handler Logic

Make sure your Branch setup is complete so that when you call handleDeepLink(_:), it triggers your product page navigation. Double-check these spots:

In AppDelegate (for iOS 12 and below)

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    Branch.getInstance().initSession(launchOptions: launchOptions) { [weak self] params, error in
        guard let self = self, let params = params, !params.isEmpty else { return }
        // Your existing logic to navigate to the product page using Branch params
        self.navigateToProductPage(with: params)
    }
    return true
}

// Don't forget these methods to handle links from external apps
func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    return Branch.getInstance().application(application, open: url, options: options)
}

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    return Branch.getInstance().continue(userActivity)
}

In SceneDelegate (for iOS 13+)

If you’re using scenes, add Branch initialization and handling there too:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    if let userActivity = connectionOptions.userActivities.first {
        Branch.getInstance().continue(userActivity)
    }
    if let urlContext = connectionOptions.urlContexts.first {
        Branch.getInstance().application(UIApplication.shared, open: urlContext.url, options: [:])
    }
}

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    guard let url = URLContexts.first?.url else { return }
    Branch.getInstance().application(UIApplication.shared, open: url, options: [:])
}

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
    Branch.getInstance().continue(userActivity)
}

3. Test Edge Cases

  • If you’re using shortened Branch links (e.g., custom short domains), make sure your URL.host check covers those domains.
  • If you have links that redirect to Branch, you might need to resolve the final URL first before checking—though Branch’s own short links should resolve directly to your Branch domain.
  • Ensure that when handleDeepLink(_:) is called, your existing navigation logic (the same one that works for external app taps) fires correctly.

By intercepting the link tap and manually handing it off to Branch, you skip the Safari detour entirely and go straight to your product page—just like when tapping from external apps!

内容的提问来源于stack exchange,提问作者Arik Segal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:56:39