Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ios distribution #1796

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
4 changes: 3 additions & 1 deletion ios/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
MeasureSDK.xcodeproj/xcuserdata/
MeasureSDK.xcodeproj/project.xcworkspace/xcuserdata
MeasureSDK.xcodeproj/project.xcworkspace/xcuserdata
.build/
Package.resolved
1 change: 0 additions & 1 deletion ios/DemoApp/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
let clientInfo = ClientInfo(apiKey: "msrsh_38514d61493cf70ce99a11abcb461e9e6d823e2068c7124a0902b745598f7ffb_65ea2c1c",
apiUrl: "http://localhost:8080")
let config = BaseMeasureConfig(enableLogging: true,
trackScreenshotOnCrash: false,
samplingRateForErrorFreeSessions: 1.0)
measureInstance.initialize(with: clientInfo, config: config)
measureInstance.setUserId("test_user_ios")
Expand Down
4 changes: 2 additions & 2 deletions ios/DemoApp/Controller/ObjcDetailViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
//

#import <UIKit/UIKit.h>
#import <MeasureSDK/MeasureSDK.h>
//#import <MeasureSDK/MeasureSDK-Swift.h>

NS_ASSUME_NONNULL_BEGIN

@interface ObjcDetailViewController : MSRViewController
@interface ObjcDetailViewController : UIViewController

@end

Expand Down
18 changes: 18 additions & 0 deletions ios/MeasureSDK.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Pod::Spec.new do |spec|
spec.name = "MeasureSDK"
spec.version = "1.0.0"
spec.summary = "spec.summary"
spec.description = "spec.description"
spec.homepage = "https://github.com/measure-sh/measure"
spec.license = { :type => "MIT", :file => "LICENSE" }
spec.author = { "Adwin Ross" => "[email protected]" }
spec.platform = :ios, "12.0"
spec.source = { :path => "." }
spec.source_files = "Sources/MeasureSDK/**/*.{h,m,swift}"
spec.resources = ["Sources/MeasureSDK/**/*.xcdatamodeld"]
spec.frameworks = "Foundation", "UIKit", "CoreData"
spec.swift_version = "5.0"
spec.static_framework = true
spec.dependency "PLCrashReporter"
end

1,848 changes: 948 additions & 900 deletions ios/MeasureSDK.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

18 changes: 0 additions & 18 deletions ios/MeasureSDK/MeasureSDK.h

This file was deleted.

44 changes: 44 additions & 0 deletions ios/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// swift-tools-version: 5.7
import PackageDescription

let package = Package(
name: "MeasureSDK",
platforms: [
.iOS(.v12)
],
products: [
.library(
name: "MeasureSDK",
targets: ["MeasureSDK"]
),
],
dependencies: [
.package(url: "https://github.com/microsoft/plcrashreporter.git", from: "1.11.2")
],
targets: [
.target(
name: "MeasureSDKObjc",
dependencies: [],
path: "Sources/MeasureSDK/Objc",
publicHeadersPath: "include"
),
.target(
name: "MeasureSDK",
dependencies: [
"MeasureSDKObjc",
.product(name: "CrashReporter", package: "plcrashreporter")
],
path: "Sources/MeasureSDK/Swift"
),
.testTarget(
name: "MeasureSDKTests",
dependencies: ["MeasureSDK"],
path: "Tests/MeasureSDKTests"
),
.testTarget(
name: "MeasureUITests",
dependencies: ["MeasureSDK"],
path: "Tests/MeasureUITests"
),
]
)
178 changes: 178 additions & 0 deletions ios/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
# Measure iOS SDK

* [Minimum requirements](#minimum-requirements)
* [Self host compatibility](#self-host-compatibility)
* [Quick reference](#quick-reference)
* [Getting started](#getting-started)
* [Custom events](#custom-events)
* [Screen view](#screen-view)
* [Features](#features)
* [Session](#session)

# Minimum requirements

| Name | Version |
|-------------------------------|---------------|
| Xcode | 15.0+ |
| Minimum iOS Deployments | 12.0+ |
| Swift Version | 5.10+ |

# Getting started

Once you have access to the dashboard, create a new app and follow the steps below:

### 1. Install Measure SDK

Measure SDK supports **CocoaPods** and **Swift Package Manager (SPM)** for installation.

#### Using CocoaPods

[CocoaPods](https://cocoapods.org) is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate MeasureSDK into your Xcode project using CocoaPods, specify it in your `Podfile`:

```ruby
pod 'MeasureSDK'
```
#### Using Swift Package Manager

The [Swift Package Manager](https://swift.org/package-manager/) is a tool for automating the distribution of Swift code and is integrated into the `swift` compiler.

Add Measure as a dependency by adding `dependencies` value to your `Package.swift` or the Package list in Xcode.

```swift
dependencies: [
.package(url: "https://github.com/measure-sh/measure.git", .upToNextMajor(from: "0.0.1"))
]
```

### 2. Initialize the SDK

Add the following to your AppDelegate's `application(_:didFinishLaunchingWithOptions:)` to capture early crashes and launch time metrics.

> [!IMPORTANT]
> To detect early crashes and ensure accurate launch time metrics,
> initialize the SDK as soon as possible in `application(_:didFinishLaunchingWithOptions:)`.

```swift
import MeasureSDK

func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let config = BaseMeasureConfig()
let clientInfo = ClientInfo(apiKey: "<apiKey>", apiUrl: "<apiUrl>")
Measure.shared.initialize(with: clientInfo, config: config)
return true
}
```

```objc
#import <MeasureSDK/MeasureSDK.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
BaseMeasureConfig *config = [[BaseMeasureConfig alloc] init];
ClientInfo *clientInfo = [[ClientInfo alloc] initWithApiKey:@"<apiKey>" apiUrl:@"<apiUrl>"];
[[Measure shared] initializeWith:clientInfo config:config];
return YES;
}

```

### 3. Verify

The SDK automatically collects data when a crash occurs. You can verify if the SDK is working by triggering a test event or a crash after initializing the SDK.

To verify crash reporting, trigger a crash in your application:

Reopen the app and check the Measure dashboard—you should see the crash report.

> [!CAUTION]
> Make sure to **remove the test crash code** before releasing the app to production.

> [!IMPORTANT]
> When triggering a crash, **make sure the Xcode debugger is not attached**, as crashes may not be properly reported when running the app in debug mode.

🎉 Congratulations! You have successfully integrated Measure into your app!

# Custom Events

Custom events provide more context on top of automatically collected events. They provide the context specific to the app to debug issues and analyze impact.

To track a custom event, use the `trackEvent` method:

```swift
Measure.shared.trackEvent(name: "event_name")
```

```objc
[[Measure shared] trackEvent:@"event_name"];
```

A custom event can also contain attributes, which are key-value pairs:

- Attribute keys must be **strings** with a maximum length of **64 characters**.
- Attribute values must be one of the supported types: **string, integer, float, double, or boolean**.
- String attribute values can have a maximum length of **256 characters**.

```swift
Measure.shared.trackEvent(name: "event_name", attributes: ["is_premium_user": .bool(true)])
```

```objc
[[Measure shared] trackEvent:@"event_name" attributes:@{@"is_premium_user": @YES}];
```

If an event occurred before the SDK was initialized, you can track it with a custom timestamp. The timestamp must be in **milliseconds since epoch**.

```swift
Measure.shared.trackEvent(name: "event_name", timestamp: 1734443973879)
```
```objc
[[Measure shared] trackEvent:@"event_name" timestamp:@(1734443973879)];
```

Apart from sending custom events, the following event can be tracked with a predefined schema:

- [ScreenView](#screen-view)


### Screen View

Measure SDK automatically tracks view controller [lifecycle events](../docs/ios/features/feature_navigation_and_lifecycle.md). However, if your app uses a custom navigation system, you can manually trigger `screen_view` events to track user flow.

To track a screen view manually, use the following method:

```swift
Measure.shared.trackScreenView("Home")
```
```objc
[[Measure shared] trackScreenView:@"Home"];
```

# Features

* [Crash tracking](../docs/ios/features/feature_crash_tracking.md)
* [Network monitoring](../docs/ios/features/feature_network_monitoring.md)
* [Network changes](../docs/ios/features/feature_network_changes.md)
* [Gesture tracking](../docs/ios/features/feature_gesture_tracking.md)
* [Layout Snapshots](../docs/ios/features/feature_layout_snapshots.md)
* [Navigation & Lifecycle](../docs/ios/features/feature_navigation_and_lifecycle.md)
* [App Lifecycle](../docs/ios/features/feature_app_lifecycle.md)
* [App launch](../docs/ios/features/feature_app_launch.md)
* [CPU monitoring](../docs/ios/features/feature_cpu_monitoring.md)
* [Memory monitoring](../docs/ios/features/feature_memory_monitoring.md)


# Session

A session represents a continuous period of activity in the app. A new session begins when an app is launched for the first time,
or when there's been no activity for a 20-minute period. A single session can continue across multiple app background and
foreground events; brief interruptions will not cause a new session to be created. This approach is helpful when reviewing
session replays, as it shows the app switching between background and foreground states within the same session.

The current session can be retrived by using `getSessionId` method.

```swift
let sessionId = Measure.shared.getSessionId()
```
```objc
NSString *sessionId = [[Measure shared] getSessionId];
```
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
//

#import "MSRViewController.h"
//#import <MeasureSDK/MeasureSDK-Swift.h>

#if __has_include(<MeasureSDK/MeasureSDK-Swift.h>)
#import <MeasureSDK/MeasureSDK-Swift.h>
#endif

@interface MSRViewController ()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Created by Adwin Ross on 03/09/24.
//

import MachO
import Foundation
import UIKit

Expand Down Expand Up @@ -64,15 +65,9 @@ final class DeviceAttributeProcessor: BaseComputeOnceAttributeProcessor {
}

func getCPUArchitecture() -> String {
var sysInfo = utsname()
uname(&sysInfo)

let machineMirror = Mirror(reflecting: sysInfo.machine)
let identifier = machineMirror.children.reduce("") { identifier, element in
guard let value = element.value as? Int8, value != 0 else { return identifier }
return identifier + String(UnicodeScalar(UInt8(value)))
if let archInfo = NXGetLocalArchInfo() {
return String(cString: archInfo.pointee.name)
}

return identifier
return AttributeConstants.unknown
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,6 @@ final class BaseConfigProvider: ConfigProvider {
return getMergedConfig(\.enableLogging)
}

var trackScreenshotOnCrash: Bool {
return getMergedConfig(\.trackScreenshotOnCrash)
}

var eventsBatchingIntervalMs: Number {
return getMergedConfig(\.eventsBatchingIntervalMs)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import Foundation
///
struct Config: InternalConfig, MeasureConfig {
let enableLogging: Bool
let trackScreenshotOnCrash: Bool
let samplingRateForErrorFreeSessions: Float
let eventsBatchingIntervalMs: Number
let sessionEndLastEventThresholdMs: Number
Expand All @@ -41,10 +40,8 @@ struct Config: InternalConfig, MeasureConfig {
let layoutSnapshotDebounceInterval: Number

internal init(enableLogging: Bool = DefaultConfig.enableLogging,
trackScreenshotOnCrash: Bool = DefaultConfig.trackScreenshotOnCrash,
samplingRateForErrorFreeSessions: Float = DefaultConfig.sessionSamplingRate) {
self.enableLogging = enableLogging
self.trackScreenshotOnCrash = trackScreenshotOnCrash
self.samplingRateForErrorFreeSessions = samplingRateForErrorFreeSessions
self.eventsBatchingIntervalMs = 30000 // 30 seconds
self.maxEventsInBatch = 500
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ import Foundation
/// Default values of configuration options for the Measure SDK.
struct DefaultConfig {
static let enableLogging = false
static let trackScreenshotOnCrash = true
static let sessionSamplingRate: Float = 0.0
}
Loading