Find the Deployment Target at runtime of a framework that I can't recompile
Asked Answered
P

1

6

It's pretty straight-forward to determine the Deployment Target for an app at compile time with the macro __IPHONE_OS_VERSION_MIN_REQUIRED. It's also straight-forward to find the current iOS version the app is running on at runtime. I'm trying to figure out the deployment target of an app at runtime. I need to use this in a framework that's already compiled. Is there a public API method to determine this? I'm not able to find one.

Just to be clear. I'm looking to get the Deployment Target for an iOS app at runtime from my framework. NOT compile time. In my framework I need to alert the user of the framework that certain features will not work if their deployment target is too far back.

I get that is easy to do while compiling but my framework is already compiled and I don't want to require the user of the framework to add a macros to their code for my framework to determine their deployment target.

Pudency answered 14/8, 2014 at 16:38 Comment(6)
It would probably help if you explained why you needed to know the deployment target at runtime. That's unusual. What's the goal of this?Adapter
There's also LSMinimumSystemVersion, but that can be manually changed to not match the Deployment Target.Metaphrast
Josh, sorry I didn't specify that I was looking to do this in iOS. That's private in iOS.Pudency
Why not just include your compile-time logic in your public header and warn them via a compile-time warning? Even better, a compile-time warning around the use of particular pieces of functionality, perhaps via availability attributes?Pugging
@Pudency it's not private in iOS; follow Josh's link, scroll down to "MinimumOSVersion" in the 'Key Summary' and you'll see Apple explicitly telling you that, under iOS you are instructed "Do not use. Use “LSMinimumSystemVersion” instead." If it's something Apple tells you to do in the documentation, it's not private.Backset
Don't know if this helps for iOS, but on OS X this information is available in the LC_VERSION_MIN_MACOSX Mach-O loader command, which is reachable from the Mach-O header accessible using _dyld_get_image_header(0). You would use the Mach-O structures defined in /usr/include/mach-o/loader.h to parse the load commands.Hecht
L
2

This is an old question, but I just verified the answer so here goes:

On iOS, you should be using the MinimumOSVersion key in the Info.plist to find the minimum supported version at runtime.

The source is actually linked from the documentation that Josh Caswell posted, about LSMinimumSystemVersion.

Note that the documentation it explains that you, as the developer, should not set the MinimumOSVersion value yourself, and that this is auto-populated for you by Xcode during a build. This explains the rather cryptic "Do Not Use" in another part of this documentation.

Posting the description of this key here, for posterity (December 2016), in case the documentation changes:

MinimumOSVersion (String - iOS). When you build an iOS app, Xcode uses the iOS Deployment Target setting of the project to set the value for the MinimumOSVersion key. Do not specify this key yourself in the Info.plist file; it is a system-written key. When you publish your app to the App Store, the store indicates the iOS releases on which your app can run based on this key. It is equivalent to the LSMinimumSystemVersion key in macOS.

I verified this by printing out the value in a test app (and changing the Xcode project's Deployment target to different values:

if let infoPlist = Bundle.main.infoDictionary {
  let minimumVersion = infoPlist["MinimumOSVersion"]
  if let minimumVersion = minimumVersion {
    print("Minimum version is \(minimumVersion)")
  }
}
Longhair answered 17/12, 2016 at 1:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.