Swift 4如何获取设备已安装应用名称列表?是否有替代方案?
Hey there! Great question—let’s break this down clearly, since there’s a common misconception here.
First off: it’s not Swift 4 that removed this capability. The real story is that Apple started tightening privacy restrictions way back with iOS 9, and over subsequent versions, completely blocked third-party apps from accessing a full list of installed applications. The timing might have lined up with Swift 4’s release, but the change was driven by Apple’s privacy policies, not the Swift language itself. This is to protect users from apps collecting unnecessary personal data about their app usage.
Depending on what you actually need to achieve, there are a couple of valid, App Store-safe options:
Get info about your own app
This is totally allowed and straightforward using theBundleclass. You can grab your app’s display name, version, and other metadata easily:let appDisplayName = Bundle.main.infoDictionary?["CFBundleDisplayName"] as? String ?? Bundle.main.infoDictionary?["CFBundleName"] as? String ?? "Unknown App" print("My app name: \(appDisplayName)")Check if a specific known app is installed
If you only need to verify if a particular app (like WhatsApp, WeChat, etc.) is present on the device, you can use URL schemes. Here’s how:- First, add the target app’s URL scheme to your
Info.plistunder theLSApplicationQueriesSchemesarray. For example, to check for WeChat:<key>LSApplicationQueriesSchemes</key> <array> <string>weixin</string> </array> - Then use this helper function to check if the app can be opened:
func isTargetAppInstalled(scheme: String) -> Bool { guard let appURL = URL(string: scheme) else { return false } return UIApplication.shared.canOpenURL(appURL) } // Usage example if isTargetAppInstalled(scheme: "weixin://") { print("WeChat is installed!") } else { print("WeChat isn't on this device.") }
Important note: You can only check schemes you’ve explicitly listed in
Info.plist—you can’t use this to scan for all possible apps.- First, add the target app’s URL scheme to your
Enterprise/jailbroken environments (not for App Store)
If you’re building an app for internal enterprise use or a jailbroken device, there are private APIs (likeLSApplicationWorkspace) that can fetch installed app lists. But do not use these for App Store submissions—they’ll get your app rejected immediately, and Apple may ban your developer account. These methods are also unstable, as Apple can change or block private APIs at any time.
For regular App Store apps, there’s no way to get a full list of installed applications anymore. Stick to the first two options above if you want to stay compliant with Apple’s rules.
内容的提问来源于stack exchange,提问作者Haibara

