ios - swift 类中未调用委托方法
问题描述
我正在集成一个跟踪位置点的 SDK。
SDK 演示应用程序已加载 SDK 框架,然后是扩展 SDK 委托的 UIViewController。当用户四处走动时,SDK 会调用某些委托函数——这些都可以在 SDK 提供者的演示应用程序中正常工作。所有委托回调函数,如 processStart 都在 TripsViewController 中:UIViewController、TheSDKSwiftDelegate
例子:
import TheSDKSwift
final class TripsViewController: UIViewController, TheSDKSwiftDelegate {
...
TheSDKSwift.setup(with: configuration, delegate: self, completionHandler: {success, error in
if success {
successBlock()
} else {
failureBlock(error)
}
})
...
// TheSDK Delegate callbacks
func processStart(ofDrive startInfo: DriveStartInfo) {
self.driveStatusLabel.text = "Driving"
if self.isAccidentEnabled() {
self.mockAccidentButton.isEnabled = true
}
let dateString = DateFormatter.shortStyleFormatter.string(from: Date(timeIntervalSince1970: TimeInterval(startInfo.startTimestamp/1000 )))
NotificationManager.displayNotification(message: "Trip Started: \(dateString)")
}
}
因此,在 SDK 正常运行期间,它会回调 processStart 方法以提供有关正在发生的事情的信息。
现在,在我自己的应用程序中,我不能在 UIViewController 中拥有这些函数/委托。我需要将它们放在一个单独的 Swift 文件中,并从另一个控制器调用这些函数。当我这样做时,我的 SDK 会初始化,但是 SDK 在运行时不会调用委托方法,而当 UIViewController 上的所有内容都在扩展委托时,会调用委托方法。只是当我把它放在我自己的 swift 文件中时。
这是我正在做的一个简短的片段:
import Foundation
import Capacitor
@objc(TheSDKPlugin)
public class TheSDKPlugin: CAPPlugin {
@objc func SetupSDK(_ call: CAPPluginCall) {
TheSDKPluginWrapper().StartSDK()
}
所以 TheSDKPluginiWrapper().StartSDK() 被调用。
import Foundation
import TheSDKSwift
import Capacitor
class TheSDKPluginWrapper: NSObject, TheSDKSwiftDelegate {
...
TheSDKSwift.setup(with: configuration, delegate: self, completionHandler: {success, error in
if success {
successBlock()
} else {
failureBlock(error)
}
})
...
// TheSDK Delegate callbacks
func processStart(ofDrive startInfo: DriveStartInfo) {
self.driveStatusLabel.text = "Driving"
if self.isAccidentEnabled() {
self.mockAccidentButton.isEnabled = true
}
let dateString = DateFormatter.shortStyleFormatter.string(from: Date(timeIntervalSince1970: TimeInterval(startInfo.startTimestamp/1000 )))
NotificationManager.displayNotification(message: "Trip Started: \(dateString)")
}
}
再次,SDK 初始化成功。但是现在,SDK 从不调用 TheSDKPluginiWrapper() 中的委托方法。
如何始终保留委托,以便在我的 swift 文件中调用 SDK 委托方法,就像在 UIViewController 中调用所有内容时一样?
解决方案
如果您希望将委托方法放在单独的文件中,您可以将它们定义为类的扩展:
extension TripsViewController: TheSDKSwiftDelegate {
// TheSDK Delegate callbacks
func processStart(ofDrive startInfo: DriveStartInfo) {
}
...
}
推荐阅读
- javascript - 通过 Twig 加载静态网页中引用的 Javascript
- python - 如何在 Python 3 中从 R 重现基于 SHA256 的 HMAC
- python - 如何使用 Matplotlib 或 Seaborn 从分组数据中生成多个图?
- sql - 将等值连接作为自然连接执行
- python - Sqlite3 数据库在不应该添加条目时添加了条目
- c++ - C++ 中类成员的动态分配
- python - 将值添加到上下文以在 Django 中呈现
- python - 如何构造属性在某个范围内的 Python pandas 系列对象
- java - 如何将对象转换为仅在运行时已知的类型?
- c# - 第二次点击后页面是否刷新?