> For the complete documentation index, see [llms.txt](https://beacon-help.gitbook.io/beacon-help/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://beacon-help.gitbook.io/beacon-help/sdk-wen-dang/sdkji-cheng-zhi-nan/iosji-cheng-zhi-nan.md).

# iOS/Mac集成指南

## **一、获取SDK及**AppKey

登录灯塔首页，创建应用，填入基本应用信息，即可获取独一无二的AppKey，并下载SDK；

[点此下载SDK for iOS](/beacon-help/sdkhuo-qu/sdk-for-ios.md)

> ## 详细接口说明可参考接口文档<https://docs.qq.com/doc/DVkltV3FYeXdaTkJx>或查看接口类的注释说明

## 二、集成上报功能

### 2.1. 导入 SDK

#### 手动导入

SDK包：BeaconAPI\_Base.framework ---基础上报SDK，必选

选择拷贝需要的framework到您的应用目录下，在Xcode中需要添加的Target中选择 ”Build Phases”->“Link Binary With Libraries”->“Add”->“Add Other”→选择framework目录。

**注意**：如果是是通过catalyst跨平台支持Mac的话，上述的SDK需要使用后缀为 .xcframework 的framework。具体可咨询 jackhuali 。

在Other linker flag里加入-ObjC标志

#### cocoapods导入

支持使用cocoapods进行包依赖管理集成灯塔上报framework。

使用cocoapods的方式，需要在Podfile文件添加腾讯的podspec源，并pod 依赖灯塔SDK，参考如下

```
source 'http://git.code.oa.com/T-CocoaPods/Specs.git'
source 'https://github.com/CocoaPods/Specs.git'

 platform :ios, '9.0'

target 'BeaconDemo' do
  use_frameworks!

  pod 'Beacon', '~> 4.0'

end
```

### 2.2 初始化SDK及上报

* **引入头文件**

```
	#import <BeaconAPI_Base/BeaconReport.h>
```

* **在- (BOOL)application:(UIApplication \*)application didFinishLaunchingWithOptions:(NSDictionary \*)launchOptions 初始化SDK**

```
BeaconTunnelInfo *mainTunnelInfo = [BeaconTunnelInfo tunnelInfoWithAppKey:@"LOGDEBUGKEY00001"];//填写上述从灯塔官网申请的appkey,// 使用实时联调2.0时可以填写：LOGDEBUGKEY00001 ；
[BeaconReport.sharedInstance startWithTunnelInfo:mainTunnelInfo config:nil];
```

* **至此，SDK已初始化完成，可以开始上报事件**

```
NSDictionary *params = @{
                @"key1" : @"event_value1",
                @"key2" : @"event_value2",
            };

BeaconEvent *realTimeEvent = [BeaconEvent realTimeEventWithCode:@"real_time_event_code_test" params:params];
// 上报实时事件
[BeaconReport.sharedInstance reportEvent:realTimeEvent];

// 上报普通事件
BeaconEvent *noralEvent = [BeaconEvent normalEventWithCode:@"normal_event_code_test" params:params];
[BeaconReport.sharedInstance reportEvent:normalEvent];
```

### 2.3 初始化接口进阶

* **设置上报配置：BeaconReportConfig**

```
BeaconReportConfig *config = [BeaconReportConfig new];
// 开发调试阶段，打开严苛模式，可以发现一些致命的基础问题，上线时必须关闭
config.strictMode = YES;
// 开启实时联调模式，可以在实时联调后台查看验证事件是否成功上报到后台
config.debugMode = NO;
// 设置本地调试时控制台输出的日志级别：1 fetal, 2 error, 3 warn, 4 info, debug, 5 debug, 10 all, 默认为0，不打印日志
config.logLevel = 10;
BeaconReport.sharedInstance.config = config;

//其余相关配置参考BeaconReportConfig接口说明
```

* **设置一些全局的ID**

```
// 设置用户唯一标识符，用以通过userId标识和分类异常用户信息
BeaconReport.sharedInstance.userId = @"userId";
// 原来使用的设备标识符，通过OMGID SDK获取
BeaconReport.sharedInstance.omgId = @"omgId";
// 小程序、H5设置的开放平台的id
BeaconReport.sharedInstance.openId = @"openId";
```

* **初始化接口tunnelInfo参数进阶：- (void)startWithTunnelInfo:(BeaconTunnelInfo \*)tunnelInfo config:(nullable BeaconReportConfig \*)config**

```
BeaconTunnelInfo *mainTunnelInfo = [BeaconTunnelInfo tunnelInfoWithAppKey:@"LOGDEBUGKEY00001"];//填写上述从灯塔官网申请的appkey,// 使用实时联调2.0时可以填写：LOGDEBUGKEY00001
// 各业务自己定义的通道版本，主APP一般采用APP的版本，其他业务或者SDK可自行定义
mainTunnelInfo.version = @"1.0";
// 当前通道登录用户的ID
mainTunnelInfo.userId = @"userId_test";
// 渠道ID
mainTunnelInfo.channelId = @"chainId_test";
// 初始化时机，添加上报的事件的附加参数,同一个appkey通道的每个事件都会上报这些参数
mainTunnelInfo.additionalParams = @{
                @"additionalKey1" : @"additional_value1",
                @"additionalKey2" : @"additional_value2",
            };
[BeaconReport.sharedInstance startWithTunnelInfo:mainTunnelInfo config:nil];
```

* **非初始化时机需要追加附加参数**

```
NSString *appKey = @"LOGDEBUGKEY00001"; 
[BeaconReport.sharedInstance addAdditionalParams:@{@"addKey1" : @"addValue1"} forAppKey:appKey];
```

### 2.4 上报功能进阶-大同专用

```
NSDictionary *params = @{
                @"key1" : @"dt_event_value1",
                @"key2" : @"dt_event_value2"
            };
// 大同实时事件上报,appkey参数为空时，事件会上报到主appKey通道，否则接入方填入自己的appKey
BeaconEvent *event = [[BeaconEvent alloc] initWithAppKey:nil code:@"dt_real_time_event_test" type:BeaconEventTypeDTRealTime success:YES params:params];
[BeaconReport.sharedInstance reportEvent:event];

// 大同实时事件上报,appkey参数为空时，事件会上报到主appKey通道，否则接入方填入自己的appKey
BeaconEvent *event = [[BeaconEvent alloc] initWithAppKey:nil code:@"dt_normal_event_test" type:BeaconEventTypeDTNormal success:YES params:params];
```

### 2.5 上报功能进阶-多通道

* **注册子通道**

```
// 注册子通道上报
BeaconTunnelInfo *tunnelInfo = [BeaconTunnelInfo tunnelInfoWithAppKey:@"LOGDEBUGKEY00002"];
[BeaconReport.sharedInstance registerSubTunnel:tunnelInfo];
```

* **上报事件到子通道**

```
BeaconEvent *event = [[BeaconEvent alloc] initWithAppKey:@"LOGDEBUGKEY00002" code:@"subTunnel_real_time_event_test" type:BeaconEventTypeRealTime success:YES params:@{@"k":@"v"}];
[BeaconReport.sharedInstance reportEvent:event];
```

### 2.6 使用设备ID Qimei功能

* 同步获取接口

```
// 同步获取qimei，只查询本地存储的qimei
BeaconQimei *qimei = [BeaconReport.sharedInstance getQimei];
NSString *messsage = [NSString stringWithFormat:@" qiemiOld:%@,\n qimeiNew:%@,\n qimeiJson:%@", qimei.qimeiOld, qimei.qimeiNew, qimei.qimeiJson];
```

* 异步获取接口

```
// 异步获取qimei，如果本地没有, 则等待网络请求的回调，针对的是APP首次安装本地没有qimei的场景。！！！只建议在APP启动阶段调用一次本异步接口，其余阶段使用同步接口获取qimei
[BeaconReport.sharedInstance getQimeiWithBlock:^(BeaconQimei * _Nullable qimei) {
NSString *messsage = [NSString stringWithFormat:@" qiemiOld:%@,\n qimeiNew:%@,\n qimeiJson:%@", qimei.qimeiOld, qimei.qimeiNew, qimei.qimeiJson];
    [self alterWithTitle:@"qimei" message:messsage];
}];
```

## 三、集成反作弊功能

### 1.导入framework

`BeaconAPI_Audit.framework`

### 2.导入头文件

`import <BeaconAPI_Audit/BeaconAuditInterface.h>`

### 3.启动渠道稽核功能

`[BeaconAuditInterface setAuditEnable:YES];`

### 4.AppDelegate中的openURL添加代码

`[BeaconAuditInterface handleOpenURL:url sourceApplication:sourceApplication];`

## 四、Mac平台

灯塔相关SDK均已通过catalyst技术同时支持iOS和Mac平台。

如果您的APP是iOS和Mac跨平台的，您需要做的额外工作是将引入的framework替换为xcframework，其余所有的集成方式和使用方法与上述的iOS SDK一致。

{% hint style="info" %}
还有疑问？欢迎反馈给`tbeacon@tencent.com`，会有专人为您服务。
{% endhint %}
