首页 > 解决方案 > 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 中调用所有内容时一样?

标签: iosswiftdelegates

解决方案


如果您希望将委托方法放在单独的文件中,您可以将它们定义为类的扩展:

extension TripsViewController: TheSDKSwiftDelegate {

   // TheSDK Delegate callbacks

   func processStart(ofDrive startInfo: DriveStartInfo) {

   }
   ...
}

推荐阅读