LoginSignup
8
6

More than 3 years have passed since last update.

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

Posted at

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
}
8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6