【Swift-OC混编】OpeninstallSDK 的Swift集成

2025-04-25 22:13:13

1、将从openinstall官网下载的OpeninstallSDK文件导入Swift项目工程

【Swift-OC混编】OpeninstallSDK 的Swift集成

2、通过New File -> Header File新建一个"*.h"头文件

【Swift-OC混编】OpeninstallSDK 的Swift集成

3、将文件命名为OCBridge.h

【Swift-OC混编】OpeninstallSDK 的Swift集成

4、选中工程,Build Setting的搜索框中Objective-C Bridging Heade,在Objective-C Bridging Heade中添加OCBridge.h桥接文件路径

【Swift-OC混编】OpeninstallSDK 的Swift集成

5、在OCBridge.h中引入OpenInstallSDK.h头文件#import "OpenInstallSDK.h"

6、渠道统计代码集成:在Appdelegate.swift文件中的didFinishLaunching方法下添加Op髫潋啜缅eninstallSDK初始化代码;如果要统计注册量,请在注册完成的回调方法中添加“OpenInstallSDK.reportRegister()”代码 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { //初始化OpenInstallSDK OpenInstallSDK.setAppKey("openinstall为应用分配的appkey", withDelegate: self) return true }

7、个性化安装代码集成: func application(_ application: 曷决仙喁UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { //初始化OpenInstall OpenInstallSDK.setAppKey("openinstall为应用分配的appkey", withDelegate: self) return true } //通过OpenInstall 获取自定义参数。数据为空时,也会回调此方法,方便开发者灵活处理。 func getInstallParams(fromOpenInstall params: [AnyHashable: Any], withError error: Error?) { if error == nil { print("OpenInstall 自定义数据:\(params.description)") if !params.isEmpty { let paramsStr = "\(String(describing: params["testkey"]))" let alert = UIAlertController(title: "安装参数", message: paramsStr, preferredStyle: .alert) alert.addAction(UIAlertAction(title: "确定", style: .default, handler: {(action: UIAlertAction) -> Void in })) //弹出提示框(便于调试,调试完成后删除此代码) self.window!.rootViewController!.present(alert, animated: true, completion: {}) } } else { print("OpenInstall error \(String(describing: error))") } }

8、一召堡厥熠键拉起代码集成: funcapplication(_application:UIApplication,didFinishLaunchingWithOpti泠贾高框onslaunchOptions:[UIApplicationLaunchOptionsKey:Any]?)->Bool{ //初始化OpenInstall OpenInstallSDK.setAppKey("openinstall为应用分配的appkey",withDelegate:self) returntrue } //ios9以下 URI Scheme 实现深度链接技术 func application(_ application: UIApplication, open url:URL, sourceApplication: String?, annotation: Any) -> Bool { //判断是否通过OpenInstall URL Scheme 唤起App if OpenInstallSDK.handLinkURL(url){ return true } else { //其他代码; return true } } //iOS9以上 URL Scheme 实现深度链接技术 private func application(_ app: UIApplication, open url: URL, options: [AnyHashable: Any]) -> Bool { //判断是否通过OpenInstall URL Scheme 唤起App if OpenInstallSDK.handLinkURL(url){ return true } else { //其他代码; return true } } //Universal Links 通用链接实现深度链接技术 private func application(_ application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: @escaping (_ _Nullable:[Any]) -> Void) -> Bool { //判断是否通过OpenInstall Universal Links 唤起App if OpenInstallSDK.continue(userActivity){ return true } else { //其他代码; return true } } //通过OpenInstall获取已经安装App被唤醒时的参数(如果是通过渠道页面唤醒App时,会返回渠道编号),c 为渠道编号,d为渠道自定义参数 func getWakeUpParams(fromOpenInstall params: [AnyHashable: Any], withError error: Error?) { print("OpenInstall 唤醒参数:\(params)") if !params.isEmpty { if !params.isEmpty { let paramsStr = "\(params)" let alert = UIAlertController(title: "唤醒参数", message: paramsStr, preferredStyle: .alert) alert.addAction(UIAlertAction(title: "确定", style: .default, handler: {(action: UIAlertAction) -> Void in })) //弹出提示框(便于调试,调试完成后删除此代码) self.window!.rootViewController!.present(alert, animated: true, completion: {}) } } }

9、配置Universal Links和URL Scheme请参考下面这条经验的4、5步

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢