Integrate the iOS SDK

更新时间:
复制 MD 格式

This guide shows you how to integrate and configure the Alibaba Cloud Mobile Monitoring SDK for iOS. It covers core procedures such as initializing the SDK, building your project, and verifying the integration.

Prerequisites

  • You have created an application in the EMAS console and obtained the required credentials.

    • Log on to the EMAS console.

    • Create an iOS application to obtain an AppKey, AppSecret, and AppRsaSecret.

    • For more information, see the quick start documentation.

  • Your development environment meets the following requirements:

    • Xcode 12.0 or later

    • iOS 10.0 or later

    • CocoaPods 1.12.0 or later

  • If your application already uses a version of the crash analysis, performance analysis, or remote log SDK earlier than 2.0.0, follow the iOS SDK Upgrade Guide to upgrade to the Mobile Monitoring SDK.

Demo projects

Add the SDK to your app

Method 1: CocoaPods (Recommended)

  1. Create or edit your Podfile:

    source 'https://github.com/CocoaPods/Specs.git'
    source 'https://github.com/aliyun/aliyun-specs.git'
    
    platform :ios, '13.0'
    use_frameworks!
    
    target 'YourTarget' do
      pod 'AlicloudApmAll', '${ApmVersion}', :subspecs => [
        'AlicloudApmPerformance',
        'AlicloudApmRemoteLog',
        'AlicloudApmMemAlloc',
        'AlicloudApmMemLeak',
        # 'AlicloudApmMemLeakSwiftSupport', # Optionally, include for Swift object support in memory leak detection.
      ]
    end
    Important

    You can find the ApmVersion in the iOS SDK release notes.

    AlicloudApmAll component and dependency details:

    • The crash analysis component, AlicloudApmCrashAnalysis, is included by default and does not need to be explicitly declared.

    • Optional capabilities are provided as subspecs and can be included as needed:

      • Performance analysis: AlicloudApmPerformance

      • Remote log: AlicloudApmRemoteLog

      • Memory allocation: AlicloudApmMemAlloc

      • Memory leak: AlicloudApmMemLeak (AlicloudApmMemLeakSwiftSupport is an add-on for Swift memory leak detection.)

    Note

    In the root directory of your Xcode project, locate and edit the Podfile to add the AlicloudApmAll dependency. You can run the pod search AlicloudApmAll command to find the latest version. If a Podfile does not exist, run the pod init command in the project's root directory to create one. If you have not installed CocoaPods, first install it from the official CocoaPods website.

  2. Run the installation commands:

    pod repo update AliyunRepo
    pod install
    
    # If you have not added the Alibaba Cloud CocoaPods repository, run the following command first to add it.
    # pod repo add AliyunRepo https://github.com/aliyun/aliyun-specs.git

Method 2: Manual

  1. Download the latest SDK package from the quick start documentation.

  2. Unzip the package and add the frameworks:

    1. Drag the following .xcframework files into your project:

      AlicloudApmAll.xcframework

      AlicloudApmCore.xcframework

      AlicloudApmCrashAnalysis.xcframework

      AlicloudApmPerformance.xcframework

      AlicloudApmRemoteLog.xcframework

      AlicloudApmMemAlloc.xcframework

      AlicloudApmMemLeak.xcframework

      UTDID.xcframework

    2. The figure below illustrates the required steps.

  3. Add the following open-source library in the same way:

  4. Navigate to Build Phases > Link Binary With Libraries and add the following built-in system libraries:

    • libc++.tbd

    • libz.tbd

    • libresolv.tbd

    • CoreTelephony.framework

    • SystemConfiguration.framework

Note
  • Xcode compatibility

    When using an earlier version of Xcode, you might need to manually add the following system libraries to ensure compatibility:

    • libz.tbd

    • libresolv.tbd

    • CoreTelephony.framework

    • SystemConfiguration.framework

  • Linker settings

    If you encounter issues during runtime, try adding the -ObjC flag:

    1. Open your Project Settings.

    2. Navigate to TARGETS.

    3. Select Build Settings.

    4. Find the Linking section.

    5. In Other Linker Flags, add -ObjC.

Use the SDK

1. Initialization

Initialize the SDK in your AppDelegate file. The following code shows an example:

#import "AlicloudApmCore/AlicloudApmCore.h"
#import "AlicloudApmCrashAnalysis/AlicloudApmCrashAnalysis.h"
#import "AlicloudApmPerformance/AlicloudApmPerformance.h"
#import "AlicloudApmRemoteLog/AlicloudApmRemoteLog.h"
#import "AlicloudApmMemAlloc/AlicloudApmMemAlloc.h"
#import "AlicloudApmMemLeak/AlicloudApmMemLeak.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    EAPMOptions *options = [[EAPMOptions alloc] initWithAppKey:@"Your_AppKey"
                                                     appSecret:@"Your_AppSecret"];
    options.appRsaSecret = @"Your_AppRsaSecret";
    // The following sdkComponents correspond to crash analysis, performance analysis, remote log, memory allocation, and memory leak. Include as needed.
    options.sdkComponents = @[[EAPMCrashAnalysis class], [EAPMPerformance class], [EAPMRemoteLog class], [EAPMMemAlloc class], [EAPMMemLeak class]];
    
    [EAPMApm startWithOptions:options];
    
    return YES;
}
@end
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, 
                   didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        let options = EAPMOptions(
            appKey: "Your_AppKey",
            appSecret: "Your_AppSecret",
            // The following sdkComponents correspond to crash analysis, performance analysis, remote log, memory allocation, and memory leak. Include as needed.
            sdkComponents: [CrashAnalysis.self, Performance.self, RemoteLog.self, MemAlloc.self, MemLeak.self]
        )
        options.appRsaSecret = "Your_AppRsaSecret"
        EAPMApm.start(options: options)
        
        return true
    }
}

For applications developed in Swift, you also need to configure a Bridging-Header.h file and import the necessary SDK header files:

image

#import "AlicloudApmCore/AlicloudApmCore.h"
// The following header files correspond to crash analysis, performance analysis, remote log, memory allocation, and memory leak. Import them as needed.
#import "AlicloudApmCrashAnalysis/AlicloudApmCrashAnalysis.h"
#import "AlicloudApmPerformance/AlicloudApmPerformance.h"
#import "AlicloudApmRemoteLog/AlicloudApmRemoteLog.h"
#import "AlicloudApmMemAlloc/AlicloudApmMemAlloc.h"
#import "AlicloudApmMemLeak/AlicloudApmMemLeak.h"

2. Build

  1. In your project's Build Settings, set Allow Non-modular Includes In Framework Modules to YES.

    编译设置

  2. Build the project.

    Note
    • If a duplicate symbol error occurs during the build, check if you have duplicate dependencies managed by both local files and CocoaPods. If so, remove the local dependency.

    • If an Undefined symbol: __swift_FORCE_LOAD_$_swiftCompatibility56 error occurs during the build, go to Target > Build Settings > Library Search Paths and add the following path: $(TOOLCHAIN_DIR)/usr/lib/swift/$(PLATFORM_NAME).

    • If you use other Alibaba Cloud products, a build failure may occur due to UTDID dependency conflicts. For a solution, see Resolve SDK UTDID Conflicts.

Integration verification

  • Run your app on a physical device or a simulator. Check the Xcode console for output similar to the following:

[AlicloudApmCore] Started Successfully
[AlicloudApmSetting] Fetched Successfully

These logs indicate that the SDK has started and fetched its configuration successfully.

Additional resources