mcpField
S
sentry-ios-swift-setup

sentry-ios-swift-setup

github.com/getsentry开发2597 次安装35 周前更新
代码仓库

关于 sentry-ios-swift-setup

为 Swift 和 SwiftUI iOS 项目提供 Sentry 安装与配置指导,面向需要设置错误监控、追踪或会话回放的开发者。涵盖常见配置、自动采集能力及故障排查。

主要功能

  1. 01Swift Package Manager 与 CocoaPods 安装
  2. 02SwiftUI 和 UIKit 初始化配置
  3. 03错误、追踪与日志设置
  4. 04会话回放及内容遮罩
  5. 05自动采集性能与网络数据
  6. 06Fastlane 构建大小分析

适用场景

  1. 01在 SwiftUI 或 UIKit 应用中接入 Sentry
  2. 02配置 iOS 错误监控与性能追踪
  3. 03设置会话回放和敏感内容遮罩
  4. 04通过 Fastlane 上传构建大小数据

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/getsentry/sentry-agent-skills --skill sentry-ios-swift-setup

安装后在 Claude Code 中输入/sentry-ios-swift-setup确认已加载。

详细介绍

Sentry iOS Swift 设置

使用 Swift 和 SwiftUI 在 iOS 项目中安装并配置 Sentry。

何时调用此 Skill

  • 用户要求在 Swift 应用中「将 Sentry 添加到 iOS」或「安装 Sentry」
  • 用户希望在 iOS 中实现错误监控、链路追踪或会话回放
  • 用户提到「sentry-cocoa」或 iOS 崩溃报告

重要: 下方的配置选项和代码示例仅供参考。在实施前请务必对照 docs.sentry.io 进行核实,因为 API 和默认值可能已发生变化。

要求

  • iOS 15.0+、macOS 12.0+、tvOS 15.0+、watchOS 8.0+

安装

Swift Package Manager(推荐)
  1. File > Add Package Dependencies
  2. 输入:https://github.com/getsentry/sentry-cocoa.git
  3. 选择版本规则:从 9.5.0 开始的「Up to Next Major」

SPM 产品: 根据你的需求选择:

产品使用场景
Sentry默认(静态链接)
Sentry-Dynamic动态框架
SentrySwiftUISwiftUI 视图性能追踪
Sentry-WithoutUIKitOrAppKitApp 扩展或命令行工具
CocoaPods
# Podfile
pod 'Sentry', :git => 'https://github.com/getsentry/sentry-cocoa.git', :tag => '9.5.0'

然后运行 pod install。

配置

SwiftUI App
import SwiftUI
import Sentry

@main
struct YourApp: App {
    init() {
        SentrySDK.start { options in
            options.dsn = "YOUR_SENTRY_DSN"
            options.debug = true
            
            // Tracing
            options.tracesSampleRate = 1.0
            
            // Profiling
            options.configureProfiling = {
                $0.sessionSampleRate = 1.0
                $0.lifecycle = .trace
            }
            
            // Session Replay
            options.sessionReplay.sessionSampleRate = 1.0
            options.sessionReplay.onErrorSampleRate = 1.0
            
            // Logs (SDK 9.0.0+; for 8.55.0-8.x use options.experimental.enableLogs)
            options.enableLogs = true
            
            // Error context
            options.attachScreenshot = true
            options.attachViewHierarchy = true
        }
    }
    
    var body: some Scene {
        WindowGroup { ContentView() }
    }
}
UIKit App
import UIKit
import Sentry

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        SentrySDK.start { options in
            options.dsn = "YOUR_SENTRY_DSN"
            options.debug = true
            options.tracesSampleRate = 1.0
            options.enableLogs = true
        }
        return true
    }
}

配置选项

选项描述默认值
dsnSentry DSN必填
tracesSampleRate被追踪的 transaction 百分比0
sessionReplay.sessionSampleRate被回放的会话百分比0
sessionReplay.onErrorSampleRate被回放的发生错误的会话百分比0
enableLogs将日志发送到 Sentryfalse
attachScreenshot出错时附加截图false
attachViewHierarchy出错时附加视图层级false

自动插桩功能

功能捕获内容
App 启动冷启动/热启动时间
网络URLSession 请求
UIUIViewController 加载、用户交互
文件 I/O读/写操作
Core Data获取/保存操作
帧慢帧和冻结帧检测

日志

let logger = SentrySDK.logger

logger.info("User action", attributes: [
    "userId": "123",
    "action": "checkout"
])

// Log levels: trace, debug, info, warn, error, fatal

会话回放

iOS 26+ / Xcode 26+ 注意事项: 由于 Apple 的 Liquid Glass 渲染会破坏遮罩的可靠性,使用 Xcode 26.0+ 构建时,SDK 8.57.0+ 会自动在 iOS 26.0+ 上禁用会话回放。在 iOS < 26 或 Xcode < 26 上,回放仍然有效。要强制启用(请谨慎使用):options.experimental.enableSessionReplayInUnreliableEnvironment = true。

遮罩
// SwiftUI modifiers
Text("Safe content").sentryReplayUnmask()
Text(user.email).sentryReplayMask()

用户上下文

let user = User()
user.userId = "user_123"
user.email = "[email protected]"
SentrySDK.setUser(user)

// Clear on logout
SentrySDK.setUser(nil)

验证

// Test error capture
SentrySDK.capture(message: "Test from iOS")

// Or trigger a test error
do {
    try someFailingFunction()
} catch {
    SentrySDK.capture(error: error)
}

生产环境设置

SentrySDK.start { options in
    options.dsn = "YOUR_SENTRY_DSN"
    options.debug = false
    options.tracesSampleRate = 0.2  // 20%
    options.sessionReplay.sessionSampleRate = 0.1  // 10%
    options.sessionReplay.onErrorSampleRate = 1.0  // 100% on error
    options.enableLogs = true
}

体积分析(Fastlane)

使用 Fastlane 插件通过 Sentry 跟踪 App 包体积。

安装插件
bundle exec fastlane add_plugin fastlane-plugin-sentry
配置身份验证
# Environment variable (recommended for CI)
export SENTRY_AUTH_TOKEN=your_token_here

或创建 .sentryclirc(添加到 .gitignore):

[auth]
token=YOUR_SENTRY_AUTH_TOKEN
Fastfile Lane
lane :sentry_size do
  build_app(
    scheme: "YourApp",
    configuration: "Release",
    export_method: "app-store"
  )

  sentry_upload_build(
    org_slug: "your-org",
    project_slug: "your-project",
    build_configuration: "Release"
  )
end
运行体积分析
bundle exec fastlane sentry_size

上传完成后,在 Sentry UI 中查看结果。

疑难解答

问题解决方案
事件未出现检查 DSN,启用 debug = true
没有链路追踪将 tracesSampleRate 设置为 > 0
没有回放将 sessionSampleRate 设置为 > 0,检查 SDK 8.31.1+。在 iOS 26+/Xcode 26+ 上请参阅上文的 Liquid Glass 注意事项
没有日志设置 enableLogs = true(SDK 9.0.0+)或 experimental.enableLogs = true(SDK 8.55.0-8.x)
CocoaPods 失败运行 pod repo update,检查 iOS 15+ target
体积上传失败检查 SENTRY_AUTH_TOKEN,核实 org/project slug

评论

支持 Markdown