ApplicationInsights-iOS/docs/Readme-template.md

382 строки
15 KiB
Markdown
Исходник Обычный вид История

2015-04-28 20:39:16 +03:00
[![Build Status](https://travis-ci.org/Microsoft/ApplicationInsights-iOS.svg?branch=master)](https://travis-ci.org/Microsoft/ApplicationInsights-iOS)
2015-03-26 22:44:40 +03:00
2015-05-07 20:23:22 +03:00
# Application Insights for iOS (1.0-beta.3)
2015-03-26 22:44:40 +03:00
2015-04-28 20:39:16 +03:00
This is the repository of the iOS SDK for Application Insights. [Application Insights](http://azure.microsoft.com/en-us/services/application-insights/) is a service that allows developers to keep their applications available, performing, and succeeding. The SDK enables you to send telemetry of various kinds (events, traces, exceptions, etc.) to the Application Insights service where your data can be visualized in the Azure Portal.
2015-01-20 19:28:03 +03:00
2015-04-28 20:39:16 +03:00
The SDK runs on devices with iOS 6.0 or higher.
2015-01-20 19:28:03 +03:00
2015-04-28 20:39:16 +03:00
## Content
1. [Release Notes](#releasenotes)
2. [Breaking Changes](#breakingchanges)
3. [Requirements](#requirements)
4. [Setup](#setup)
5. [Advanced Setup](#advancedsetup)
6. [Developer Mode](#developermode)
7. [Basic Usage](#basicusage)
8. [Automatic collection of life-cycle events](#autolifecycle)
9. [Crash Reporting](#crashreporting)
10. [Set Custom Server Endpoint](#additionalconfig)
11. [Documentation](#documentation)
12. [Contributing](#contributing)
13. [Contact](#contact)
2015-05-07 20:23:22 +03:00
<a name="releasenotes"></a>
## 1. Release Notes
2015-04-28 20:39:16 +03:00
2015-05-07 20:23:22 +03:00
* Add new API to be able to manually set session and user IDs.
2015-04-28 20:39:16 +03:00
2015-05-07 20:23:22 +03:00
``` objectivec
[MSAIApplicationInsights setUserId:@"your_user_id"];
[MSAIApplicationInsights renewSessionWithId:@"4815162342"];
```
* Allow to specify the amount of time that an app has to have been in the background before a new session is triggered.
``` objectivec
[MSAIApplicationInsights setAppBackgroundTimeBeforeSessionExpires:60];
```
* Make our sending-retry policy more robust and only delete data on unrecoverable HTTP status codes.
* Trigger saving of queued-up date when the app goes to the background since then there is a high probability it might be removed from memory by the OS.
* Add our Xcode docset part of the downloaded archive.
* Several small fixes, cleanups and optimizations under the hood.
See [here](https://github.com/Microsoft/ApplicationInsights-iOS/releases) for the release notes of previous versions.
<a name="breakingchanges"></a>
## 2. Breaking Changes
2015-04-28 20:39:16 +03:00
Starting with the first 1.0 stable release, we will start deprecating API instead of breaking old ones.
* **[1.0-beta.2]** ```MSAIAppInsights``` was the the central entry-point to use the Application Insights SDK. It has been renamed to ```MSAIApplicationInsights```.
* **[1.0-beta.2]** Setting the custom server URL now requires the complete URL to the server
2015-01-20 19:28:03 +03:00
2015-05-07 20:23:22 +03:00
<a id="requirements"></a>
## 3. Requirements
2015-01-20 19:28:03 +03:00
2015-04-28 20:39:16 +03:00
The SDK runs on devices with iOS 6.0 or higher.
2015-01-20 19:28:03 +03:00
2015-05-07 20:23:22 +03:00
<a name="setup"></a>
## 4. Setup
2015-01-20 19:28:03 +03:00
2015-04-28 20:39:16 +03:00
We recommend integration of our binary into your Xcode project to setup Application Insights for your iOS app. For other ways to setup the SDK, see [Advanced Setup](#advancedsetup).
2015-01-20 19:28:03 +03:00
### 4.1 Obtain an Instrumation Key
Please see the "[Getting an Application Insights Instrumentation Key](https://github.com/Microsoft/ApplicationInsights-Home/wiki#getting-an-application-insights-instrumentation-key)" section of the wiki for more information on acquiring a key.
2015-05-07 20:23:22 +03:00
<a id="downloadsdk"></a>
### 4.2 Download the SDK
2015-01-20 19:28:03 +03:00
2015-04-28 20:39:16 +03:00
1. Download the latest [Application Insights for iOS](https://github.com/Microsoft/AppInsights-iOS/releases) framework which is provided as a zip-File.
2. Unzip the file and you will see a folder called `ApplicationInsights` .
2015-01-20 19:28:03 +03:00
### 4.3 Copy the SDK into your projects directory in Finder
2015-01-20 19:28:03 +03:00
From our experience, 3rd-party libraries usually reside inside a subdirectory (let's call our subdirectory `Vendor`), so if you don't have your project organized with a subdirectory for libraries, now would be a great start for it. To continue our example, create a folder called "Vendor" inside your project directory and move the unzipped `ApplicationInsights`-folder into it.
2015-01-20 19:28:03 +03:00
2015-05-07 20:23:22 +03:00
<a id="setupxcode"></a>
### 4.4 Set up the SDK in Xcode
2015-01-20 19:28:03 +03:00
1. We recommend to use Xcode's group-feature to create a group for 3rd-party-libraries similar to the structure of our files on disk. For example, similar to the file structure in 4.1 above, our projects have a group called `Vendor`.
2015-04-28 20:39:16 +03:00
2. Make sure the `Project Navigator` is visible (⌘+1)
3. Drag & drop `ApplicationInsights.framework` from your window in the `Finder` into your project in Xcode and move it to the desired location in the `Project Navigator` (e.g. into the group called `Vendor`)
4. A popup will appear. Select `Create groups for any added folders` and set the checkmark for your target. Then click `Finish`.
5. Select your project in the `Project Navigator` (⌘+1).
6. Select your app target.
7. Select the tab `Build Phases`.
8. Expand `Link Binary With Libraries`.
9. Add the following system frameworks, if they are missing:
- `UIKit`
2015-03-26 22:44:40 +03:00
- `Foundation`
- `SystemConfiguration`
- `Security`
2015-04-16 01:53:45 +03:00
- `libz`
2015-03-26 22:44:40 +03:00
- `CoreTelephony`(only required if iOS > 7.0)
9. Open the `Info.plist` of your app target and add a new field of type *String*. Name it `MSAIInstrumentationKey` and set your Application Insights instrumentation key from 4.1 as its value.
2015-01-20 19:28:03 +03:00
### 4.5 Modify Code
2015-01-20 19:28:03 +03:00
2015-04-28 20:39:16 +03:00
**Objective-C**
2015-01-20 19:28:03 +03:00
2015-04-28 20:39:16 +03:00
1. Open your `AppDelegate.m` file.
2. Add the following line at the top of the file below your own #import statements:
2015-02-25 16:21:02 +03:00
```objectivec
#import <ApplicationInsights/ApplicationInsights.h>
```
2015-05-07 20:23:22 +03:00
2015-04-28 20:39:16 +03:00
3. Search for the method `application:didFinishLaunchingWithOptions:`
4. Add the following lines to setup and start the Application Insights SDK:
2015-02-25 16:21:02 +03:00
```objectivec
[[MSAIApplicationInsights sharedInstance] setup];
// Do some additional configuration if needed here
...
[[MSAIApplicationInsights sharedInstance] start];
```
2015-02-25 16:21:02 +03:00
2015-05-07 20:23:22 +03:00
You can also use the following shortcuts:
2015-02-25 16:21:02 +03:00
2015-05-07 20:23:22 +03:00
```objectivec
[MSAIApplicationInsights setup];
[MSAIApplicationInsights start];
```
2015-02-25 16:21:02 +03:00
2015-04-28 20:39:16 +03:00
**Swift**
2015-01-20 19:28:03 +03:00
2015-04-28 20:39:16 +03:00
1. Open your `AppDelegate.swift` file.
2. Add the following line at the top of the file below your own #import statements:
```swift
#import ApplicationInsights
```
2015-05-07 20:23:22 +03:00
2015-04-28 20:39:16 +03:00
3. Search for the method
```swift
application(application: UIApplication, didFinishLaunchingWithOptions launchOptions:[NSObject: AnyObject]?) -> Bool`
```
2015-05-07 20:23:22 +03:00
2015-04-28 20:39:16 +03:00
4. Add the following lines to setup and start the Application Insights SDK:
```swift
MSAIApplicationInsights.sharedInstance().setup();
MSAIApplicationInsights.sharedInstance().start();
```
2015-05-07 20:23:22 +03:00
You can also use the following shortcuts:
2015-03-26 22:44:40 +03:00
2015-05-07 20:23:22 +03:00
```swift
2015-06-04 00:24:14 +03:00
MSAIApplicationInsights.setup()
MSAIApplicationInsights.start()
2015-05-07 20:23:22 +03:00
```
2015-04-28 20:39:16 +03:00
**Congratulation, now you're all set to use Application Insights! See [Basic Usage](#basicusage) on how to use Application Insights.**
2015-01-28 21:48:59 +03:00
2015-05-07 20:23:22 +03:00
<a id="advancedsetup"></a>
## 5. Advanced Setup
2015-01-28 21:48:59 +03:00
2015-04-28 20:39:16 +03:00
### 5.1 Set Instrumentation Key in Code
2015-01-28 21:48:59 +03:00
It is also possible to set the instrumentation key of your app in code. This will override the one you might have set in your `Info.plist`. To set it in code, MSAIApplicationInsights provides an overloaded constructor:
2015-01-28 21:48:59 +03:00
2015-02-25 16:21:02 +03:00
```objectivec
2015-05-07 20:23:22 +03:00
[MSAIApplicationInsights setupWithInstrumentationKey:@"{YOUR-INSTRUMENTATIONKEY}"];
//Do additional configuration
2015-04-28 20:39:16 +03:00
2015-05-07 20:23:22 +03:00
[MSAIApplicationInsights start];
2015-04-28 20:39:16 +03:00
```
### 5.2 Setup with CocoaPods
[CocoaPods](http://cocoapods.org) is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries like ApplicationInsights in your projects. To learn how to setup CocoaPods for your project, visit the [official CocoaPods website](http://cocoapods.org/).
2015-04-28 20:39:16 +03:00
**[NOTE]**
When adding Application Insights to your podfile **without** specifying the version, `pod install` will throw an error because using a pre-release version of a pod has to be specified **explicitly**.
As soon as Application Insights 1.0 is available, the version doesn't have to be specified in your podfile anymore.
**Podfile**
```ruby
platform :ios, '8.0'
2015-05-07 20:23:22 +03:00
pod "ApplicationInsights", '1.0-beta.3'
2015-02-25 16:21:02 +03:00
```
2015-03-26 22:44:40 +03:00
2015-04-28 20:39:16 +03:00
### 5.3 iOS 8 Extensions
2015-01-28 21:48:59 +03:00
2015-04-09 22:17:15 +03:00
The following points need to be considered to use the Application Insights SDK with iOS 8 Extensions:
2015-01-28 21:48:59 +03:00
2015-02-25 16:21:02 +03:00
1. Each extension is required to use the same values for version (`CFBundleShortVersionString`) and build number (`CFBundleVersion`) as the main app uses. (This is required only if you are using the same `MSAIInstrumentationKey` for your app and extensions).
2015-04-28 20:39:16 +03:00
2. You need to make sure the SDK setup code is only invoked **once**. Since there is no `applicationDidFinishLaunching:` equivalent and `viewDidLoad` can run multiple times, you need to use a setup like the following example:
2015-01-28 21:48:59 +03:00
2015-05-07 20:23:22 +03:00
```objectivec
@interface TodayViewController () <NCWidgetProviding>
@property (nonatomic, assign) BOOL didSetupApplicationInsightsSDK;
@end
@implementation TodayViewController
- (void)viewDidLoad {
[super viewDidLoad];
if (!self.didSetupApplicationInsightsSDK) {
[MSAIApplicationInsights setup];
[MSAIApplicationInsights start];
self.didSetupApplicationInsightsSDK = YES;
}
2015-05-07 20:23:22 +03:00
}
```
<a name="developermode"></a>
## 6. Developer Mode
2015-04-28 20:39:16 +03:00
###6.1 Batching of data
The **developer mode** is enabled automatically in case the debugger is attached or if the app is running in the simulator. This will decrease the number of telemetry items sent in a batch (5 items) as well as the interval items when telemetry will be sent (3 seconds).
2015-04-28 20:39:16 +03:00
###6.2 Logging
We're all big fans of a clean debugging output without 3rd-party-SDKs messages piling up in the debugging view, right?!
That's why Application Insights keeps log messages to a minimum (like critical errors) unless the developer specifically enables debug logging before starting the SDK:
2015-04-28 20:39:16 +03:00
2015-05-07 20:23:22 +03:00
```objectivec
2015-04-28 20:39:16 +03:00
[MSAIApplicationInsights setup]; //setup the SDK
2015-03-26 22:44:40 +03:00
2015-04-28 20:39:16 +03:00
[[MSAIApplicationInsights sharedInstance] setDebugLogEnabled:YES]; //enable debug logging
2015-01-20 19:28:03 +03:00
2015-04-28 20:39:16 +03:00
[MSAIApplicationInsights start]; //start using the SDK
```
2015-01-20 19:28:03 +03:00
This setting is ignored if the app is running in an app store environment, so the user's console won't be littered with our log messages.
2015-01-20 19:28:03 +03:00
2015-05-07 20:23:22 +03:00
<a id="basicusage"></a>
## 7. Basic Usage
2015-01-20 19:28:03 +03:00
**[NOTE]** The SDK is optimized to defer everything possible to a later time while making sure e.g. crashes on startup can also be caught and each module executes other code with a delay of some seconds. This ensures that `applicationDidFinishLaunching:` will process as fast as possible and the SDK will not block the startup sequence resulting in a possible kill by the watchdog process.
2015-01-20 19:28:03 +03:00
After you have set up the SDK as [described above](#setup), the ```MSAITelemetryManager```-instance is the central interface to track events, traces, metrics, page views or handled exceptions.
2015-01-20 19:28:03 +03:00
2015-04-28 20:39:16 +03:00
### 7.1 Objective-C
2015-01-20 19:28:03 +03:00
2015-05-07 20:23:22 +03:00
```objectivec
2015-04-28 20:39:16 +03:00
// Send an event with custom properties and measurements data
[MSAITelemetryManager trackEventWithName:@"Hello World event!"
properties:@{@"Test property 1":@"Some value",
@"Test property 2":@"Some other value"}
measurements:@{@"Test measurement 1":@(4.8),
@"Test measurement 2":@(15.16),
@"Test measurement 3":@(23.42)}];
2015-04-28 20:39:16 +03:00
// Send a message
[MSAITelemetryManager trackTraceWithMessage:@"Test message"];
2015-01-20 19:28:03 +03:00
2015-04-28 20:39:16 +03:00
// Manually send pageviews (note: this will also be done automatically)
[MSAITelemetryManager trackPageView:@"MyViewController"
duration:300
properties:@{@"Test measurement 1":@(4.8)}];
2015-01-20 19:28:03 +03:00
2015-04-28 20:39:16 +03:00
// Send custom metrics
[MSAITelemetryManager trackMetricWithName:@"Test metric" value:42.2];
```
2015-04-28 20:39:16 +03:00
### 7.2 Swift
2015-05-07 20:23:22 +03:00
2015-04-28 20:39:16 +03:00
```swift
// Send an event with custom properties and measuremnts data
2015-06-04 00:24:14 +03:00
MSAITelemetryManager.trackEventWithName("Hello World event!",
properties:["Test property 1":"Some value",
"Test property 2":"Some other value"],
measurements:["Test measurement 1":4.8,
"Test measurement 2":15.16,
"Test measurement 3":23.42])
2015-04-28 20:39:16 +03:00
// Send a message
2015-06-04 21:15:45 +03:00
MSAITelemetryManager.trackTraceWithMessage("Test message")
2015-04-28 20:39:16 +03:00
// Manually send pageviews
2015-06-04 00:24:14 +03:00
MSAITelemetryManager.trackPageView("MyViewController",
2015-04-28 20:39:16 +03:00
duration:300,
2015-06-04 21:15:45 +03:00
properties:["Test measurement 1":4.8])
2015-04-28 20:39:16 +03:00
// Send a message
2015-06-04 21:15:45 +03:00
MSAITelemetryManager.trackMetricWithName("Test metric", value:42.2)
2015-04-28 20:39:16 +03:00
```
2015-01-20 19:28:03 +03:00
2015-05-07 20:23:22 +03:00
<a name="autolifecycle"></a>
## 8. Automatic collection of lifecycle events
2015-03-26 22:44:40 +03:00
Automatic collection of lifecycle events is **enabled by default**. This means that Application Insights automatically tracks the appearance of a view controller and manages sessions for you.
2015-04-30 11:53:51 +03:00
2015-05-07 20:23:22 +03:00
### 8.1. Page views
The automatic tracking of view controller appearance can be disabled between setup and start of the SDK.
2015-03-26 22:44:40 +03:00
2015-04-28 20:39:16 +03:00
```objectivec
[MSAIApplicationInsights setup]; //setup the SDK
[[MSAIApplicationInsights sharedInstance] setAutoPageViewTrackingDisabled:YES]; //disable the auto collection
2015-04-28 20:39:16 +03:00
[MSAIApplicationInsights start]; //start using the SDK
2015-05-07 20:23:22 +03:00
```
### 8.2. Sessions
2015-03-26 22:44:40 +03:00
By default, the Application Insights for iOS SDK starts a new session when the containing app is restarted (this means a 'cold start', i.e. when the app has not already been in memory prior to being launched) or when it has been in the background for more then 20 seconds.
2015-05-07 20:23:22 +03:00
You can either change this time frame:
``` objectivec
[MSAIApplicationInsights setAppBackgroundTimeBeforeSessionExpires:60];
```
turn of automatic session completely:
``` objectivec
[MSAIApplicationInsights setAutoSessionManagementDisabled:YES];
```
This then requires you to manage sessions manually:
``` objectivec
[MSAIApplicationInsights renewSessionWithId:@"4815162342"];
```
### 8.3. Users
Normally, a random anonymous ID is automatically generated for every user of your app by the SDK. Alternatively you can set your own user ID which will then be attached to all telemetry events and crashes:
```objectivec
[MSAIApplicationInsights setUserId:@"your_user_id"];
2015-03-26 22:44:40 +03:00
```
2015-03-26 23:13:40 +03:00
2015-05-07 20:23:22 +03:00
<a name="crashreporting"></a>
## 9. Crash Reporting
2015-04-28 20:39:16 +03:00
The Application Insights SDK enables crash reporting **per default**. Crashes will be immediately sent to the server the next time the app is launched.
2015-04-28 20:39:16 +03:00
To provide you with the best crash reporting, we are using [PLCrashReporter]("https://github.com/plausiblelabs/plcrashreporter") in [Version 1.2 / Commit 273a7e7cd4b77485a584ac82e77b7c857558e2f9]("https://github.com/plausiblelabs/plcrashreporter/commit/273a7e7cd4b77485a584ac82e77b7c857558e2f9").
This feature can be disabled as follows:
```objectivec
[MSAIApplicationInsights setup]; //setup the SDK
[[MSAIApplicationInsights sharedInstance] setCrashManagerDisabled:YES]; //disable crash reporting
[MSAIApplicationInsights start]; //start using the SDK
```
2015-05-07 20:23:22 +03:00
<a name="additionalconfig"></a>
## 10. Set Custom Server Endpoint
2015-04-28 20:39:16 +03:00
You can also configure a different server endpoint for the SDK if needed using a full URL
```objectivec
[MSAIApplicationInsights setup]; //setup the SDK
[[MSAIApplicationInsights sharedInstance] setServerURL:@"https://YOURDOMAIN/path/subpath"];
[MSAIApplicationInsights start]; //start using the SDK
```
2015-05-07 20:23:22 +03:00
2015-04-28 20:39:16 +03:00
<a id="documentation"></a>
## 11. Documentation
Our documentation can be found on [CocoaDocs](http://cocoadocs.org/docsets/ApplicationInsights/1.0-beta.3/).
2015-04-28 20:39:16 +03:00
<a id="contributing"></a>
## 12. Contributing
We're looking forward to your contributions via pull requests.
**Development environment**
* Mac running the latest version of OS X
* Get the latest Xcode from the Mac App Store
* [AppleDoc](https://github.com/tomaz/appledoc)
* [Cocoapods](https://cocoapods.org/)
2015-03-26 23:13:40 +03:00
<a id="contact"></a>
2015-04-28 20:39:16 +03:00
## 13. Contact
2015-03-26 23:13:40 +03:00
2015-05-07 20:23:22 +03:00
If you have further questions or are running into trouble that cannot be resolved by any of the steps here, feel free to open a Github issue here or contact us at [AppInsights-iOS@microsoft.com](mailto:AppInsights-ios@microsoft.com)