sentry-cocoa-sdk は何をしますか?
Appleプラットフォーム(iOS、macOS、tvOS、watchOS、visionOS)向けの完全なSentry SDKセットアップ。iOSアプリにSentryを追加する、SwiftにSentryを追加する、sentry-cocoaをインストールする、Appleアプリケーションのエラーモニタリング、トレーシング、プロファイリング、セッションリプレイ、ログ、メトリクスを設定する場合に使用します。SwiftUIとUIKitをサポートしています。
インストール
Swift Package Manager
XcodeでFile > Add Packages... を開き、以下のURLを入力します。
https://github.com/getsentry/sentry-cocoa
CocoaPods
Podfile に以下を追加します。
pod 'Sentry'
Carthage
Cartfile に以下を追加します。
github "getsentry/sentry-cocoa"
初期化
AppDelegate または SwiftUI アプリのエントリポイントでSentryを初期化します。
UIKit (Swift)
import Sentry
// AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
SentrySDK.start { options in
options.dsn = "https://[email protected]/0"
options.debug = true // デバッグビルドでは推奨
// パフォーマンストレーシングを有効化
options.tracesSampleRate = 1.0
// プロファイリングを有効化
options.profilesSampleRate = 1.0
// セッションリプレイ(プレビュー)
options.sessionReplay.sessionSampleRate = 0.0
options.sessionReplay.onErrorSampleRate = 1.0
}
return true
}
SwiftUI
import SwiftUI
import Sentry
@main
struct MyApp: App {
init() {
SentrySDK.start { options in
options.dsn = "https://[email protected]/0"
options.debug = true
options.tracesSampleRate = 1.0
options.profilesSampleRate = 1.0
}
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
基本的な使い方
エラーキャプチャ
let event = Event(level: .error)
event.message = "エラーの説明"
SentrySDK.capture(event: event)
// または、NSErrorを直接キャプチャ
let error = NSError(domain: "com.example", code: 1, userInfo: [NSLocalizedDescriptionKey: "何かが間違っています"])
SentrySDK.capture(error: error)
ユーザーコンテキストの設定
let user = User(userId: "1234")
user.email = "[email protected]"
user.username = "username"
SentrySDK.setUser(user)
ブレッドクラムの追加
let crumb = Breadcrumb(level: .info, category: "ui")
crumb.message = "ユーザーがボタンをタップしました"
SentrySDK.addBreadcrumb(crumb)