iOS应用外部链接唤起及URL处理方案咨询(Swift/iOS9)
Hey there! Let's tackle your questions step by step—since you're working with Swift and targeting iOS 9.0, I'll focus on solutions that fit that environment perfectly.
First, you need to set up deep linking so that tapping your shared links opens your app (if installed) and routes users to the right content. Here's how:
Step 1: Configure Custom URL Scheme (or Universal Links)
For iOS 9, start with a Custom URL Scheme (we’ll cover alternatives later). Add this to your Info.plist:
<key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLSchemes</key> <array> <string>your-app-unique-scheme</string> <!-- e.g., "myawesomeapp" --> </array> </dict> </array>
Step 2: Share the Link via UIActivityViewController
When sharing your web content, construct a link that uses your scheme (or Universal Link) so social platforms can pass it to users. For example:
let webPageURL = URL(string: "https://your-content-url.com")! let encodedWebURL = webPageURL.absoluteString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)! let deepLinkURL = URL(string: "your-app-unique-scheme://open?weburl=\(encodedWebURL)")! let activityVC = UIActivityViewController(activityItems: [deepLinkURL], applicationActivities: nil) present(activityVC, animated: true)
Note: If using Universal Links, replace the scheme URL with your HTTPS domain link (e.g., https://yourapp.com/article/123).
Step 3: Handle the URL in AppDelegate
In iOS 9, use this method to catch incoming links and route users:
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { // Verify the URL belongs to your app's scheme guard url.scheme == "your-app-unique-scheme" else { return false } // Parse the URL to retrieve the target web content if let webURLString = url.queryParameters?["weburl"], let targetWebURL = URL(string: webURLString) { // Navigate to your in-app web view with the target URL let webVC = WebViewController(url: targetWebURL) window?.rootViewController?.pushViewController(webVC, animated: true) } return true } // Helper extension to parse query parameters (add outside AppDelegate) extension URL { var queryParameters: [String: String]? { guard let components = URLComponents(url: self, resolvingAgainstBaseURL: true), let queryItems = components.queryItems else { return nil } return queryItems.reduce(into: [String: String]()) { result, item in result[item.name] = item.value } } }
Besides Universal Links, these methods work reliably for iOS 9.0+:
Custom URL Schemes: The method we covered above. It’s simple to set up, but has two caveats:
- Scheme collisions are possible (if another app uses the same unique scheme).
- When opening from Safari, iOS shows a user confirmation dialog before launching your app.
Web View Fallback Handling: If you’re sharing a regular HTTPS link, add a JavaScript snippet to your web page that tries to open your app’s scheme first. If that fails (app not installed), it redirects to your App Store page or the web content. Example JS:
window.addEventListener('load', function() { setTimeout(function() { window.location.href = "your-app-unique-scheme://"; }, 100); setTimeout(function() { window.location.href = "https://apps.apple.com/us/app/your-app-id"; }, 500); });Note: This requires control over the web content you’re sharing.
To enable other apps to launch your app, they just need to use your custom URL Scheme with UIApplication.open(_:options:completionHandler:). Here’s what their code would look like:
if let yourAppURL = URL(string: "your-app-unique-scheme://open?data=sample-content") { if UIApplication.shared.canOpenURL(yourAppURL) { UIApplication.shared.open(yourAppURL, options: [:], completionHandler: nil) } else { // Redirect to App Store if your app isn't installed let appStoreURL = URL(string: "https://apps.apple.com/us/app/your-app-id")! UIApplication.shared.open(appStoreURL, options: [:], completionHandler: nil) } }
Important: For iOS 9+, other apps need to add your scheme to their Info.plist under LSApplicationQueriesSchemes to check if your app is installed:
<key>LSApplicationQueriesSchemes</key> <array> <string>your-app-unique-scheme</string> </array>
内容的提问来源于stack exchange,提问作者Xavi Tordera

