Swift实现APP链接跳转及视频分享带跳转链接方案咨询
Hey there! Let’s tackle your three questions step by step to get your app’s deep link sharing up and running alongside your video content.
1. How to Generate the App Jump Link
You have two reliable options for deep linking in iOS: Custom URL Schemes and Universal Links. Here’s how to set up both:
Option 1: Custom URL Scheme (Quick Setup)
- Head to your Xcode project settings →
Infotab → expandURL Types - Add a new URL type: set
Identifierto something likecom.yourcompany.yourapp, andURL Schemesto a unique value (e.g.,myapp) - Your dynamic jump link will look like:
myapp://post?postId=1980(replace1980with your actual post ID)
Option 2: Universal Links (More Secure & Recommended)
This uses standard HTTPS URLs that open your app directly if installed, or fall back to a web page if not:
- Enable
Associated Domainsin your Xcode project capabilities (requires an Apple Developer account) - Add a domain entry like
applinks:yourapp.com - Host an
apple-app-site-associationfile on your server athttps://yourapp.com/.well-known/apple-app-site-associationwith content like:{ "applinks": { "apps": [], "details": [ { "appID": "YOUR_TEAM_ID.com.yourcompany.yourapp", "paths": ["/post/*"] } ] } } - Your jump link will be a web URL like:
https://yourapp.com/post/1980
2. Should You Add the URL to the Share Items Array?
Absolutely! To share both the video and your app link together, just append the URL (as a URL object) to your existing items array. Most social platforms (like Facebook) will handle both assets, displaying the video alongside a link preview or attachment.
Modify your existing code to include the link:
func launchShareActivityController(with items: [Any], of player: AVPlayer, postID: Int, canDownload: Int) { // Generate your deep link URL (use either Custom Scheme or Universal Link) guard let appLinkURL = URL(string: "myapp://post?postId=\(postID)") else { print("Failed to create app link URL") return } // Combine original items with the app link let shareItems: [Any] = items + [appLinkURL] let activityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil) activityViewController.popoverPresentationController?.sourceView = self.view activityViewController.completionWithItemsHandler = { activity, success, items, error in player.play() if success { print("successfully shared") self.postShare(postID: postID, completion: { (isSuccess, results) in if results.count > 0 { // Add post-share logic here if needed } }) } } }
Note: Some platforms (like Instagram) may only support sharing a single media item. For these, you can add the link as plain text (e.g., "Check out this post in MyApp: myapp://post?postId=1980") instead of a URL object.
3. How to Detect Link Clicks & Route to the Specified Post
For Custom URL Schemes
Add this method to your AppDelegate (or SceneDelegate if using Scenes):
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { // Parse the URL to extract the post ID if url.scheme == "myapp", url.host == "post" { let components = URLComponents(url: url, resolvingAgainstBaseURL: false) if let postIdString = components?.queryItems?.first(where: { $0.name == "postId" })?.value, let postId = Int(postIdString) { // Navigate to your post detail view controller let postVC = PostDetailViewController(postID: postId) if let window = app.windows.first, let rootVC = window.rootViewController { if let navVC = rootVC as? UINavigationController { navVC.pushViewController(postVC, animated: true) } else { rootVC.present(UINavigationController(rootViewController: postVC), animated: true) } } return true } } return false }
For Universal Links
Add this method to your AppDelegate or SceneDelegate:
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool { if userActivity.activityType == NSUserActivityTypeBrowsingWeb, let url = userActivity.webpageURL { // Parse the URL path to get the post ID (e.g., https://yourapp.com/post/1980) let pathComponents = url.pathComponents if pathComponents.count >= 3, pathComponents[1] == "post", let postId = Int(pathComponents[2]) { // Navigate to post detail view controller let postVC = PostDetailViewController(postID: postId) // Same navigation logic as above... return true } } return false }
内容的提问来源于stack exchange,提问作者Aqib Javed

