Swift 3集成LinkedIn:如何检测设备是否已安装LinkedIn App
Hey there, let's sort out this tricky LinkedIn login issue you're dealing with—it’s a common pitfall when working with app-to-app authentication flows, so let’s break it down step by step.
First: Why is the App Store popping up even when LinkedIn is installed?
9 times out of 10, this happens because your app isn’t allowed to check if LinkedIn is installed, or you’re using the wrong URL scheme. iOS blocks apps from querying other apps unless you explicitly whitelist their schemes in your Info.plist.
Step 1: Configure Info.plist to allow detecting LinkedIn
First, add these entries to your Info.plist to whitelist LinkedIn’s schemes and set up your app’s callback URL:
<!-- Allow querying LinkedIn app --> <key>LSApplicationQueriesSchemes</key> <array> <string>linkedin</string> <string>linkedin-sdk</string> </array> <!-- Set up your app's callback URL scheme (match what you configured in LinkedIn's developer portal) --> <key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLSchemes</key> <array> <string>your-app-scheme</string> <!-- e.g., "myapp" if your redirect URI is "myapp://linkedin-callback" --> </array> <key>CFBundleURLName</key> <string>com.yourcompany.yourapp</string> <!-- Replace with your app's Bundle ID --> </dict> </array>
Make sure your redirect URI in LinkedIn’s developer dashboard exactly matches the one you’ll use in your app (e.g., myapp://linkedin-callback).
Step 2: Detect if LinkedIn App is installed
Add this helper function to check if LinkedIn is present on the device:
Swift
func isLinkedInAppInstalled() -> Bool { guard let linkedInURL = URL(string: "linkedin://") else { return false } return UIApplication.shared.canOpenURL(linkedInURL) }
Objective-C
- (BOOL)isLinkedInAppInstalled { NSURL *linkedInURL = [NSURL URLWithString:@"linkedin://"]; return [[UIApplication sharedApplication] canOpenURL:linkedInURL]; }
Step 3: Handle the login flow (install vs. not installed)
Now, use the helper function to branch your logic:
Swift (using LinkedIn's official Login SDK)
// Assuming you've set up the LinkedInLoginKit in your project @IBAction func linkedInLoginTapped(_ sender: UIButton) { if isLinkedInAppInstalled() { // Open LinkedIn app for login let loginManager = LinkedInLoginManager.shared // Set the permissions you need (adjust based on your requirements) loginManager.loginPermissions = ["r_liteprofile", "r_emailaddress"] loginManager.login(from: self) { [weak self] result in switch result { case .success(let authToken): // Login succeeded! Handle the token, then navigate to your target VC let targetVC = YourTargetViewController() self?.navigationController?.pushViewController(targetVC, animated: true) case .failure(let error): // Handle login errors (e.g., user canceled, network issues) print("LinkedIn login failed: \(error.localizedDescription)") } } } else { // Redirect to App Store to install LinkedIn guard let appStoreURL = URL(string: "https://apps.apple.com/us/app/linkedin/id288429040") else { return } if UIApplication.shared.canOpenURL(appStoreURL) { UIApplication.shared.open(appStoreURL, options: [:], completionHandler: nil) } } }
If you're not using the SDK (direct URL scheme login)
func openLinkedInDirectLogin() { let clientID = "YOUR_LINKEDIN_CLIENT_ID" let redirectURI = "your-app-scheme://linkedin-callback" // Match your Info.plist and LinkedIn portal let scope = "r_liteprofile%20r_emailaddress" // URL-encoded permissions guard let loginURL = URL(string: "linkedin://oauth/v2/authorization?response_type=code&client_id=\(clientID)&redirect_uri=\(redirectURI)&scope=\(scope)") else { return } UIApplication.shared.open(loginURL, options: [:], completionHandler: nil) }
Don’t forget to handle the callback in AppDelegate (or SceneDelegate for iOS 13+) to capture the authorization code and exchange it for an access token, then navigate to your target view controller.
Quick Troubleshooting Tip
If you still see the App Store prompt even after installing LinkedIn:
- Double-check that
LSApplicationQueriesSchemesincludes bothlinkedinandlinkedin-sdk - Verify you’re using the correct URL scheme to open LinkedIn (
linkedin://is the primary one) - Ensure your LinkedIn developer portal has the exact redirect URI configured that your app uses
内容的提问来源于stack exchange,提问作者Rupshikha

