Tích hợp SDK
Tìm hiểu cách khởi tạo và khởi động SDK iOS.
Trước khi bạn bắt đầu
- Trước khi tích hợp, bạn phải Cài đặt SDK.
- Tài liệu này chứa các cách thực hiện mẫu. Đảm bảo thay thế những thứ sau:
<YOUR_DEV_KEY>
: Dev key AppsFlyer.<APPLE_APP_ID>
: ID Ứng dụng Apple (không cóid
tiền tố).- Phần giữ chỗ bổ sung, nếu cần.
Khởi tạo SDK iOS
Bước 1: Nhập phần phụ thuộc
Nhập AppsFlyerLib
:
// AppDelegate.h
#import <AppsFlyerLib/AppsFlyerLib.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@end
import UIKit
import AppsFlyerLib
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
// ...
}
Bước 2: Khởi tạo SDK
In didFinishLaunchingWithOptions
cấu hình ID Ứng dụng Apple và dev key AppsFlyer của bạn:
[[AppsFlyerLib shared] setAppsFlyerDevKey:@"<YOUR_DEV_KEY>"];
[[AppsFlyerLib shared] setAppleAppID:@"<APPLE_APP_ID>"];
AppsFlyerLib.shared().appsFlyerDevKey = "<YOUR_DEV_KEY>"
AppsFlyerLib.shared().appleAppID = "<APPLE_APP_ID>"
Khởi động SDK iOS
In applicationDidBecomeActive
, call start
:
[[AppsFlyerLib shared] start];
func applicationDidBecomeActive(_ application: UIApplication) {
AppsFlyerLib.shared().start()
// ...
}
Add SceneDelegate support
Tùy chọn
Chỉ thực hiện những thao tác sau nếu bạn sử dụng SceneDelegate
:
In didFinishLaunchingWithOptions
, thêm UIApplicationDidBecomeActiveNotification
trình quan sát và đặt để chạy start
:
@implementation AppDelegate
// SceneDelegate support - start AppsFlyer SDK
- (void)sendLaunch:(UIApplication *)application {
[[AppsFlyerLib shared] start];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// ...
// SceneDelegate support
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(sendLaunch:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
// ...
return YES;
}
// ...
@end
import UIKit
import AppsFlyerLib
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
...
// SceneDelegate support
NotificationCenter.default.addObserver(self, selector: NSSelectorFromString("sendLaunch"), name: UIApplicationdidBecomeActiveNotification, object: nil)
return true
}
// SceneDelegate support - start AppsFlyer SDK
@objc func sendLaunch() {
AppsFlyerLib.shared().start()
}
// ...
}
Start with completion handler
Tùy chọn
Để xác nhận rằng SDK đã khởi động thành công và thông báo cho máy chủ AppsFlyer, hãy gọi ra start
cùng một trình xử lý hoàn thành. Sau đó, bạn có thể áp dụng logic để xử lý trạng thái thành công hay thất bại của hoạt động khởi chạy SDK.
[[AppsFlyerLib shared] startWithCompletionHandler:^(NSDictionary<NSString *,id> *dictionary, NSError *error) {
if (error) {
NSLog(@"%@", error);
return;
}
if (dictionary) {
NSLog(@"%@", dictionary);
return;
}
}];
AppsFlyerLib.shared()?.start(completionHandler: { (dictionary, error) in
if (error != nil){
print(error ?? "")
return
} else {
print(dictionary ?? "")
return
}
})
Ví dụ đầy đủ
#import "AppDelegate.h"
#import <AppsFlyerLib/AppsFlyerLib.h>
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate ()
@end
@implementation AppDelegate
// Start the AppsFlyer SDK
- (void)sendLaunch:(UIApplication *)application {
[[AppsFlyerLib shared] start];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
/** APPSFLYER INIT **/
[AppsFlyerLib shared].appsFlyerDevKey = @"<YOUR_DEV_KEY>";
[AppsFlyerLib shared].appleAppID = @"<APPLE_APP_ID>";
/* Uncomment the following line to see AppsFlyer debug logs */
// [AppsFlyerLib shared].isDebug = true;
// SceneDelegate support
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(sendLaunch:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
if (@available(iOS 10, *)) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
}];
}
else {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
[[UIApplication sharedApplication] registerForRemoteNotifications];
return YES;
}
@end
import UIKit
import AppsFlyerLib
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
AppsFlyerLib.shared().appsFlyerDevKey = "<YOUR_DEV_KEY>"
AppsFlyerLib.shared().appleAppID = "<APPLE_APP_ID>"
/* Uncomment the following line to see AppsFlyer debug logs */
// AppsFlyerLib.shared().isDebug = true
// SceneDelegate support
NotificationCenter.default.addObserver(self, selector: NSSelectorFromString("sendLaunch"), name: UIApplication.didBecomeActiveNotification, object: nil)
return true
}
// SceneDelegate support
@objc func sendLaunch() {
AppsFlyerLib.shared().start()
}
// ...
}
Setting the Customer User ID
Tùy chọn
The Customer User ID (CUID) is a unique user identifier created outside the SDK by the app owner. If made available to the SDK, it can be associated with installs and other in-app events. These CUID-tagged events can be cross-referenced with user data from other devices and applications.
Set the CUID
To set the CUID:
[AppsFlyerLib shared].customerUserID = @"my user id";
AppsFlyerLib.shared().customerUserID = "my user id"
Associate the CUID with the install event
If it’s important for you to associate the install event with the CUID, you should set to set the customerUserId
before calling the start
method. This is because start
sends the install event to AppsFlyer. If the CUID is set after calling start
, it will not be associated with the install event.
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Your custom logic of retrieving CUID
NSString *customUserId = [[NSUserDefaults standardUserDefaults] stringForKey:@"customerUserId"];
if (customUserId != nil && ![customUserId isEqual: @""]) {
// Set CUID in AppsFlyer SDK for this session
[AppsFlyerLib shared].customerUserID = customUserId;
// Start
[[AppsFlyerLib shared] start];
}
}
func applicationDidBecomeActive(_ application: UIApplication) {
// your logic to retrieve CUID
let customUserId = UserDefaults.standard.string(forKey: "customUserId")
if(customUserId != nil && customUserId != ""){
// Set CUID in AppsFlyer SDK for this session
AppsFlyerLib.shared().customerUserID = customUserId
AppsFlyerLib.shared().start() // Start
}
}
Log sessions
The SDK sends an af_app_opened
message whenever the app is opened or brought to the foreground, providing that start
is called in the didBecomeActive
lifecycle event method. Before the message is sent, the SDK makes sure that the time passed since sending the last message is not smaller than a predefined interval.
Setting the time interval between app launches
Đặt minTimeBetweenSessions
to the minimal time interval that must lapse between two af_app_opened
messages. The default interval is 5 seconds.
Hỗ trợ iOS 14
Sau đây là hướng dẫn về cách thiết lập hỗ trợ cho các tính năng của iOS 14+.
Enabling App Tracking Transparency (ATT) support
Bắt đầu từ iOS 14.5, quyền truy cập IDFA được điều chỉnh bởi khung ATT.
Việc bật hỗ trợ ATT trong SDK sẽ xử lý việc thu thập IDFA trên các thiết bị cài đặt iOS 14.5
+.
Chú ý
Gọi ra
waitForATTUserAuthorization
chỉ khi bạn có ý định gọi rarequestTrackingAuthorization
đâu đó trong ứng dụng của bạn.
Bước 1: Thiết lập waitForATTUserAuthorization
Khi Khởi tạo SDK, trước khi gọi ra start
In applicationDidBecomeActive
, call waitForATTUserAuthorization
:
[[AppsFlyerLib shared] waitForATTUserAuthorizationWithTimeoutInterval:60];
AppsFlyerLib.shared().waitForATTUserAuthorization(timeoutInterval: 60)
Đặt timeoutInterval
sao cho người dùng ứng dụng có đủ thời gian để xem và tương tác với lời nhắc ATT. Một vài ví dụ:
- Nếu lời nhắc ATT được hiển thị khi khởi chạy ứng dụng–thì khoảng thời gian 60 giây là đủ
- Nếu lời nhắc ATT được hiển thị sau phần hướng dẫn cần 2 phút để hoàn thành–thì khoảng thời gian 120 giây là đủ.
Bước 2: Gọi requestTrackingAuthorization
Gọi ra requestTrackingAuthorization
ở nơi bạn muốn hiển thị lời nhắc:
- (void)didBecomeActiveNotification {
// start is usually called here:
// [[AppsFlyerLib shared] start];
if @available(iOS 14, *) {
[ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
NSLog(@"Status: %lu", (unsigned long)status);
}];
}
}
@objc func didBecomeActiveNotification() {
// start is usually called here:
// AppsFlyerLib.shared().start()
if #available(iOS 14, *) {
ATTrackingManager.requestTrackingAuthorization { (status) in
switch status {
case .denied:
print("AuthorizationSatus is denied")
case .notDetermined:
print("AuthorizationSatus is notDetermined")
case .restricted:
print("AuthorizationSatus is restricted")
case .authorized:
print("AuthorizationSatus is authorized")
@unknown default:
fatalError("Invalid authorization status")
}
}
}
}
Lưu ý
- Bạn cần nhập
AppTrackingTransparency
khung để gọi rarequestTrackingAuthorization
.- Theo tài liệu của Apple:
requestTrackingAuthorization
chỉ được gọi ra nếu ứng dụng ở trạng tháiUIApplicationStateActive
.requestTrackingAuthorization
không thể gọi ra từ Phần mở rộng của Ứng dụng.
Customizing the ATT consent dialog
Hộp thoại đồng ý ATT có thể được tùy chỉnh bằng cách trong dự án Xcode của bạn, sửa đổi info.plist
:
Để biết hướng dẫn chi tiết, hãy xem tài liệu của Apple.
Attributing App Clips
Phân bổ Apple App Clip đã được ra mắt trong SDK iOS V6.0.8
. Xem hướng dẫn tích hợp App Clip của chúng tôi để biết hướng dẫn chi tiết.
Sending SKAN postback copies to AppsFlyer
iOS 15
Cấu hình ứng dụng của bạn để gửi các bản sao đăng lại tới AppsFlyer.
Để đăng ký điểm cuối AppsFlyer:
- Thêm
NSAdvertisingAttributionReportEndpoint
mã khóa vào ứng dụng của bạninfo.plist
. - Đặt giá trị của mã khóa thành
https://appsflyer-skadnetwork.com/
.
Theo Apple, bạn chỉ có thể đặt một điểm cuối. Bản sao của các bài đăng lại nhận được có sẵn trong báo cáo bản sao bài đăng lại.
Bật chế độ gỡ lỗi
Bạn có thể bật nhật ký gỡ lỗi bằng cách đặt isDebug thành true
:
[AppsFlyerLib shared].isDebug = true;
AppsFlyerLib.shared().isDebug = true
Lưu ý
Để xem toàn bộ nhật ký gỡ lỗi, hãy đảm bảo đặt
isDebug
trước khi gọi ra các phương thức SDK khác.Xem ví dụ.
cảnh báo
Để tránh rò rỉ thông tin nhạy cảm, hãy đảm bảo rằng nhật ký gỡ lỗi đã được tắt trước khi phân bố ứng dụng.
Thử nghiệm tích hợp
Để được hướng dẫn kiểm tra tích hợp chi tiết, hãy xem phần hướng dẫn kiểm tra tích hợp SDK trên iOS.
Đã cập nhật 2 ngày trước