9

I am going through a few Swift tutorials on how to build simple apps, as I am just starting to code. I want to make my app iOS 9 compatible, as I have an iPad 3. However, all the os.log statements generate an error in Xcode which tells me to add an if #avaliable statement before any of the os.log statements. What does os.log do, and if I need it, is there an issue using an if #avaliable statement for iOS 9 compatibility? If not, what is the equivalent code for iOS 9 to go in the else statement after the if #avaliable statement? Thanks.

mfaani
  • 33,269
  • 19
  • 164
  • 293
TonyStark4ever
  • 848
  • 1
  • 9
  • 24
  • Have you read [the documentation](https://developer.apple.com/reference/os/logging)? – rmaddy Jun 01 '17 at 19:27
  • 1
    @rmaddy, Yes, I have read the documentation, and I see that it's for logging, but what does it log? Basic errors, app behavior, so on and so forth? And what is the iOS 9 equivalent? – TonyStark4ever Jun 01 '17 at 19:30
  • It logs what you tell it to :) – There is still `NSLog()` which isn't as flexible but available on all OS versions. – Martin R Jun 01 '17 at 19:36
  • @MartinR So is it basically for debugging app crashes and things like that? – TonyStark4ever Jun 02 '17 at 04:54
  • looking at the Apple website tutorial, it uses log to persist data, humph https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/PersistData.html#//apple_ref/doc/uid/TP40015214-CH14-SW1 – XueYu Jul 13 '17 at 02:57

1 Answers1

8

From Apple's documentation:

Unified logging is available in iOS 10.0 and later, macOS 10.12 and later, tvOS 10.0 and later, and watchOS 3.0 and later, and supersedes ASL (Apple System Logger) and the Syslog APIs. Historically, log messages were written to specific locations on disk, such as /etc/system.log. The unified logging system stores messages in memory and in a data store, rather than writing to text-based log files.

There is no iOS9 equivalent. You could use a third party logging tool like CocoaLumberjack, which is very popular.

As a concrete example of how to use this logging:

if #available(iOS 10.0, *) {
    let bundleID:String = Bundle.main.bundleIdentifier ?? "unknown"
    let oslog = OSLog(subsystem: bundleID, category: "Model")
    os_log("%@", log: oslog, type: .info, message)
}
David S.
  • 6,567
  • 1
  • 25
  • 45