Help us understand the problem. What is going on with this article?

iOSでクラッシュレポートにありそうなデバイス/アプリ/システム情報を取得する

Firebase Crashlytics などのクラッシュレポートには、デバイスのモデル情報や、アプリのバージョン、メモリ使用量など、クラッシュの原因調査に役立ちそうな各種情報が一緒にレポートされて便利ですよね。それらの情報を iOS で取得する方法をここにまとめておきます。

デバイスの情報

iOSのバージョン

import UIKit

func osVersion() -> String {
    return UIDevice.current.systemName + UIDevice.current.systemVersion
}

モデル名

import Foundation

func modelName() -> String {
    var size = 0
    sysctlbyname("hw.machine", nil, &size, nil, 0)

    var machine = [CChar](repeating: 0, count: size)
    sysctlbyname("hw.machine", &machine, &size, nil, 0)

    return String(cString: machine)
}

アプリのバンドルID

import Foundation

func appBundleIdentifier() -> String? {
    return Bundle.main.bundleIdentifier
}

アプリのバージョン

import Foundation

func appVersion() -> String? {
    return Bundle.main.infoDictionary?["CFBundleVersion"] as? String
}

システム情報

メモリ使用量

import Foundation

func memoryUsage() -> UInt64? {
    var info = mach_task_basic_info()
    var count = UInt32(MemoryLayout.size(ofValue: info) / MemoryLayout<integer_t>.size)
    let result = withUnsafeMutablePointer(to: &info) {
        task_info(mach_task_self_,
                  task_flavor_t(MACH_TASK_BASIC_INFO),
                  $0.withMemoryRebound(to: Int32.self, capacity: 1) { (p) in
                    UnsafeMutablePointer<Int32>(p)
            },
                  &count)
    }
    return result == KERN_SUCCESS ? info.resident_size : nil
}

URLディスクキャッシュ使用量

import Foundation

func urlDiskCacheUsage() -> Int {
    return URLCache.shared.currentDiskUsage
}
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away