How do I know if a Mac OS application is running?

I saw in some projects something like:

#if ..... code... #endif 

but I can not find it now ...
Say, for example, if the application runs at 10.8, the application does 1 thing, if the application does not do another thing. What to do to check if it works on 10.8?
Thank.

+8
objective-c xcode cocoa osx-mountain-lion macos
Jun 15 '12 at 17:01
source share
7 answers

You are probably asking the wrong question. Except in very rare cases, you do not care which version of the system works. Instead, you should check if your particular item is available.

For example, if Apple introduces the MagicHologram class on Mac OS X 10.9 that you want to use, you do not check if the user is running Mac OS X 10.9. Instead, you check to see if the MagicHologram class is available. If so, you can use it. If not, it is unavailable. It doesn't even matter why. Perhaps they work 10.8. But maybe five years later, and Apple decided to completely abandon the MagicHologram class.

(Also, keep in mind that you will need a weak reference to HologramKit, a library that provides the MagicHologram class.)

Similarly, if they introduce a new NSString method, instead of checking the OS version, you should check if NSString knows about the new method.

However, NSApplication.h includes an external constant called NSAppKitVersionNumber . You can compare this with constants like NSAppKitVersionNumber10_7 , which (should be noted) are numbers like 1138, not 10.7. There are just a few places that are relevant, mainly where classes were private and undocumented, but significant changes were made before documenting them and becoming part of the public parts of the SDK. It can also be useful if you want to avoid a specific error that has been fixed since then.

Repeat:

  • Define individual classes and methods that should cover 99.44% of your cases.
  • Use NSAppKitVersionNumber and NSAppKitVersionNumber10_7 to cover cases where the discovery of a class or method will be yours.
  • These first two points cover all normal cases. You should not go any further. But if you must have humane-based behavior, look at abarnert's answer below. This is the most reliable way to get them.
  • Do not use operatingSystemVersionString , which is specifically indicated as unsafe for parsing.

Links / additional information:

  • SDK Compatibility Guide "Read this document if you want your application to target a specific version or multiple versions of iOS or Mac OS X."
    • Using SDK Development Describes how to use loosely coupled classes, methods, and functions to support working in several versions of the operating system.
+23
Jun 15 '12 at 18:13
source share

A quick way to do this:

 if ( NSAppKitVersionNumber >= NSAppKitVersionNumber10_7 ) { // Do stuff for Lion or later } 

More details here .

You can see all the constants available in NSApplication.h, which you can use with Open Quickly ... (Cmd-Shift O) in Xcode.

The header files contain some surprisingly interesting reading material, if you are so inclined.

+11
Jun 15 '12 at 19:21
source share

As others have said (and I would choose Steven Fisher's answer), you usually do not want to get the version number.

And if you need to make comparisons only with the main OS X version to the version of the current SDK that you are using, NSAppKitVersionNumber (as in Monolo's answer) is the right way to do this.

If you really need to get the version number for some reason (for example, to record analytics about your users, so you can decide when to stop supporting 10.6.0-10.6.5), here's how to do it:

 #import <CoreServices/CoreServices.h> SInt32 majorVersion, minorVersion, bugFixVersion; Gestalt(gestaltSystemVersionMajor, &majorVersion); Gestalt(gestaltSystemVersionMinor, &minorVersion); Gestalt(gestaltSystemVersionBugFix, &bugFixVersion); 

For 10.7.3 this gives majorVersion = 10, minorVersion = 7, bugFixVersion = 3.

Documentation 10.7 deleted the paragraph that Gestalt directly suggested as a way to get the OS version, but it's still not out of date or out of date, and there are no other suggestions. In fact, any other way to get this information (parsing is [NSProcessInfo OperatingSystemVersionString], calling sysctlbyname on "kern.osrelease" and converting the Darwin kernel version to OS X version, etc.) is explicitly contraindicated somewhere. So this is the way to do it if you really want it.

Just keep in mind that, as noted in the System 6.0.4 release in 1989, this new API may not be permanent and may be removed in a future version of the OS.

+4
Jun 15 2018-12-12T00:
source share

You can get the current version from the uname -r command (in fact, the kernel release, although it can be easily compared with Mac OS X versions), from the sw_vers , which gives you the name, version and release build ID or from the Gestalt() function (in current versions of Mac OS X, anyway). The safest way is probably to read the result of sw_vers , which is in a stable format that is easy to parse.

Please note that you probably do not want to know which version of the OS you are using. What you probably want to do is check if any feature is available. This can be done using loosely coupled frameworks, weak class references, or by checking whether the class matches the selectors of the corresponding function that you are interested in.

+2
Jun 15 '12 at 18:06
source share

As Stephen Fisher said, you should not check the version of the system, but for the accessibility of the class or method that you want to use.

Check if a particular class is available,

 if ([NSHologram class]) { // Create an instance of the class and use it. } else { // The Hologram class is not available. } 

To check if a particular method is available, use

 NSString* hologramText = @"Hologram"; if ([hologramText respondsToSelector:@selector(convertHologram)]) { [hologramText convertHologram]; } 

However, to verify the method, this method must be available on the system where you are creating the application, otherwise you will receive a compilation error.

+2
Oct 19
source share

Here is the code how I do it. I like it, mainly because I don't need A) Rely on NSTask or B) Rely on any I / O files that many processes have access to.

 static NSString* const kVarSysInfoVersionFormat = @"%@.%@.%@ (%@)"; static NSString* const kVarSysInfoKeyOSVersion = @"kern.osrelease"; static NSString* const kVarSysInfoKeyOSBuild = @"kern.osversion"; - (NSString *) _strControlEntry:(NSString *)ctlKey { size_t size = 0; if ( sysctlbyname([ctlKey UTF8String], NULL, &size, NULL, 0) == -1 ) return nil; char *machine = calloc( 1, size ); sysctlbyname([ctlKey UTF8String], machine, &size, NULL, 0); NSString *ctlValue = [NSString stringWithCString:machine encoding:[NSString defaultCStringEncoding]]; free(machine); return ctlValue; } - (NSString *) getOSVersionInfo { NSString *darwinVer = [self _strControlEntry:kVarSysInfoKeyOSVersion]; NSString *buildNo = [self _strControlEntry:kVarSysInfoKeyOSBuild]; if ( !darwinVer || !buildNo ) return nil; NSString *majorVer = @"10", *minorVer = @"x", *bugFix = @"x"; NSArray *darwinChunks = [darwinVer componentsSeparatedByCharactersInSet:[NSCharacterSet punctuationCharacterSet]]; if ( [darwinChunks count] > 0 ) { NSInteger firstChunk = [(NSString *)[darwinChunks objectAtIndex:0] integerValue]; minorVer = [NSString stringWithFormat:@"%ld", (firstChunk - 4)]; bugFix = [darwinChunks objectAtIndex:1]; } return [NSString stringWithFormat:kVarSysInfoVersionFormat, majorVer, minorVer, bugFix, buildNo]; } 

Enjoy it!

0
Sep 06
source share

You can get the OS version as follows:

 NSString *version = [[NSProcessInfo processInfo] operatingSystemVersionString]; NSLog(version); 

Output:

screenshot




And I see that you want to get only the version. This can be done as follows:
 NSString *version = [[NSProcessInfo processInfo] operatingSystemVersionString]; NSRange range = NSMakeRange(8, 4); NSString *justVersion = [version substringWithRange: range]; NSLog(@"%@", justVersion); 

Result:

screenshot

And to check:

 if ([justVersion isEqualToString:@"10.7"]) { code... } else { ... } 
-four
Jun 15 '12 at 17:55
source share



All Articles