Sentry Getting Started - iOS

官方介入指南:https://docs.sentry.io/clients/cocoa/

一、接入Sentry

Cocoa

This is the documentation for our official clients for Cocoa (Swift and Objective-C). Starting with version 3.0.0 we’ve switched our internal code from Swift to Objective-C to maximize compatibility. Also we trimmed the public API of our sdk to a minimum. Some of the lesser used features that were present before are gone now, check out Migration Guide or Advanced Usage for details.

Getting Started

Getting started with Sentry is a three step process:

Sign up for an account

Install your SDK

Configure it

Installation

The SDK can be installed using CocoaPods or Carthage. This is the recommended client for both Swift and Objective-C.

We recommend installing Sentry with CocoaPods.

To integrate Sentry into your Xcode project using CocoaPods, specify it in your Podfile:

source'https://github.com/CocoaPods/Specs.git'platform:ios,'8.0'use_frameworks!target'YourApp'dopod'Sentry',:git=>'https://github.com/getsentry/sentry-cocoa.git',:tag=>'4.3.1'end

Afterwards run pod install. In case you encounter problems with dependencies and you are on a newer CocoaPods you might have to run pod repo update first.

To integrate Sentry into your Xcode project using Carthage, specify it in your Cartfile:

github"getsentry/sentry-cocoa""4.3.1"

Run carthage update to download the framework and drag the built Sentry.framework into your Xcode project.

We also provide a pre-built version for every release which can be downloaded at releases on GitHub.

Configuration

To use the client, change your AppDelegate’s application method to instantiate the Sentry client:

Showing configuration for 

your-org / your-project 

importSentryfuncapplication(_application:UIApplication,didFinishLaunchingWithOptionslaunchOptions:[UIApplication.LaunchOptionsKey:Any]?)->Bool{// Create a Sentry client and start crash handlerdo{Client.shared=tryClient(dsn:"https://@sentry.io/")tryClient.shared?.startCrashHandler()}catchleterror{print("\(error)")}returntrue}

If you prefer to use Objective-C you can do so like this:

Showing configuration for 

your-org / your-project 

@importSentry;NSError*error=nil;SentryClient*client=[[SentryClientalloc]initWithDsn:@"https://@sentry.io/"didFailWithError:&error];SentryClient.sharedClient=client;[SentryClient.sharedClientstartCrashHandlerWithError:&error];if(nil!=error){NSLog(@"%@",error);}

Debug Symbols

Before you can start capturing crashes you will need to tell Sentry about the debug information by uploading dSYM files. Depending on your setup this can be done in different ways:

With Bitcode

Without Bitcode

Testing a Crash

If you would like to test the crash reporting you will need to cause a crash. While the seemingly obvious method would be to make it crash on launch, this will not give the Sentry client a chance to actually submit the crash report. Instead, we recommend triggering a crash from a button tap.

You can use the following methods to cause a crash:

Swift:

Client.shared?.crash()

Objective-C:

[SentryClient.sharedClientcrash];

Note that if you crash with a debugger attached nothing will happen.

Crashes are only submitted upon re-launching the application. To see the crash in Sentry, close the app and launch it again from the springboard.

二、上传dSYM

Uploading Debug Symbols

A dSYM upload is required for Sentry to symoblicate your crash logs for viewing. The symoblication process unscrambles Apple’s crash logs to reveal the function, variables, file names, and line numbers of the crash. The dSYM file can be uploaded through the sentry-cli tool or through a Fastlane action.

With Bitcode

If Bitcode is enabled in your project, you will have to upload the dSYM to Sentry after it has finished processing in the iTunesConnect. We also recommend using the latest Xcode version for submitting your build. The dSYM can be downloaded in three ways.

Use Fastlane

Use the Fastlane’s action, download_dsyms, to download the dSYMs from iTunesConnect and upload to Sentry. The dSYM won’t be generated until after the app is done processing on iTunesConnect so this should be run in its own lane.

lane:upload_symbolsdodownload_dsymsupload_symbols_to_sentry(auth_token: '...',org_slug: '...',project_slug: '...',)end

If you have a legacy API key instead you can supply it with api_key instead of auth_token.

On Prem

By default fastlane will connect to sentry.io. For on-prem you need to provide the api_host parameter to instruct the tool to connect to your server:

api_host: 'https://mysentry.invalid/'

Use sentry-cli

There are two ways to download the dSYM from iTunesConnect. After you do one of the two following ways, you can upload the dSYM using sentry-cli.

Open Xcode Organizer, go to your app, and click “Download dSYMs…”

Login to iTunes Connect, go to your app, go to “Activity, click the build number to go into the detail page, and click “Download dSYM”

Afterwards manually upload the symbols with sentry-cli:

sentry-cli--auth-tokenYOUR_AUTH_TOKEN upload-dif--orgYOUR_ORG_SLUG--projectYOUR_PROJECT_SLUG PATH_TO_DSYMS

On Prem

By default sentry-cli will connect to sentry.io. For on-prem you need to export the SENTRY_URL environment variable to instruct the tool to connect to your server:

export SENTRY_URL=https://mysentry.invalid/

Without Bitcode

When not using bitcode you can directly upload the symbols to Sentry as part of a build.

Use Fastlane

If you are already using Fastlane you can use it in this situation as well. If you are not, you might prefer the sentry-cli integerated into Xcode.

lane:builddogymupload_symbols_to_sentry(auth_token: '...',org_slug: '...',project_slug: '...',)end

If you have a legacy API key instead you can supply it with api_key instead of auth_token.

On Prem

By default fastlane will connect to sentry.io. For on-prem you need to provide the api_host parameter to instruct the tool to connect to your server:

api_host: 'https://mysentry.invalid/'

Upload Symbols with sentry-cli

Your project’s dSYM can be upload during the build phase as a “Run Script”. For this you need to st the DEBUG_INFORMATION_FORMAT to be DWARF with dSYM File. By default, an Xcode project will only have DEBUG_INFORMATION_FORMAT set to DWARF with dSYM File in Release so make sure everything is set in your build settings properly.

You need to have an Auth Token for this to work. You can create an Auth Token here.

You will need to copy the below into a new Run Script and set your AUTH_TOKENORG_SLUG, and PROJECT_SLUG

Download and install sentry-cli — The best place to put this is in the /usr/local/bin/ directory

Showing configuration for 

your-org / your-project 

if which sentry-cli>/dev/null;thenexport SENTRY_ORG=your-orgexport SENTRY_PROJECT=your-projectexport SENTRY_AUTH_TOKEN=YOUR_AUTH_TOKENERROR=$(sentry-cli upload-dif"$DWARF_DSYM_FOLDER_PATH"2>&1>/dev/null)if[!$?-eq0];thenecho"warning: sentry-cli - $ERROR"fi

elseecho"warning: sentry-cli not installed, download from https://github.com/getsentry/sentry-cli/releases"fi

On Prem

By default sentry-cli will connect to sentry.io. For on-prem you need to export the SENTRY_URL environment variable to instruct the tool to connect to your server:

export SENTRY_URL=https://mysentry.invalid/

Manually with sentry-cli

Your dSYM file can be upload manually by you (or some automated process) with the sentry-cli tool. You will need to know the following information:

API Key

Organization slug

Project slug

Path to the build’s dSYM

Download and install sentry-cli — The best place to put this is in the /usr/local/bin/ directory.

Then run this – note that --auth-token needs to go before upload-dif:

sentry-cli--auth-tokenYOUR_AUTH_TOKEN upload-dif--orgYOUR_ORG_SLUG--projectYOUR_PROJECT_SLUG PATH_TO_DSYMS

On Prem

By default, sentry-cli will connect to sentry.io. For on-prem you need to export the SENTRY_URL environment variable to instruct the tool to connect to your server:

export SENTRY_URL=https://mysentry.invalid/

ON THIS PAGE

With Bitcode

Use Fastlane

Use sentry-cli

Without Bitcode

Use Fastlane

Upload Symbols with sentry-cli

Manually with sentry-cli

Found an error? Help us fix it

三、使用Swift实例

1、Podfile增加

pod 'Sentry', :git => 'https://github.com/getsentry/sentry-cocoa.git', :tag => '4.3.1'

2、初始化CrashHandler及日志上报代码

//配置sentry日志监控

 // Create a Sentry client and start crash handler 水滴 xxxxx代表示例测试地址,使用时替换成自己的地址

 do {

 Client.shared = try Client(dsn: "https://[email protected]")

 try Client.shared?.startCrashHandler()

} catch let error {

 print("\(error)")

        }

 //sentry测试信息 crash event

 Client.shared?.crash()

 let event = Event(level: .debug)

event.message = "Test Message"

 Client.shared?.send(event: event) { (error) in

 // Optional callback after event has been sent

        }

3、dSYM配置

Upload Symbols with sentry-cli

Your project’s dSYM can be upload during the build phase as a “Run Script”. For this you need to st the DEBUG_INFORMATION_FORMAT to be DWARF with dSYM File. By default, an Xcode project will only have DEBUG_INFORMATION_FORMAT set to DWARF with dSYM File in Release so make sure everything is set in your build settings properly.

You need to have an Auth Token for this to work. You can create an Auth Token here.

You will need to copy the below into a new Run Script and set your AUTH_TOKENORG_SLUG, and PROJECT_SLUG

Download and install sentry-cli — The best place to put this is in the /usr/local/bin/ directory

Showing configuration for 

your-org / your-project 

if which sentry-cli>/dev/null;thenexport SENTRY_ORG=your-orgexport SENTRY_PROJECT=your-projectexport SENTRY_AUTH_TOKEN=YOUR_AUTH_TOKEN

export SENTRY_URL= https://sentry.sample.cnERROR=$(sentry-cli upload-dif"$DWARF_DSYM_FOLDER_PATH"2>&1>/dev/null)if[!$?-eq0];thenecho"warning: sentry-cli - $ERROR"fi

elseecho"warning: sentry-cli not installed, download from https://github.com/getsentry/sentry-cli/releases"fi

你可能感兴趣的:(Sentry Getting Started - iOS)