【问题标题】:Delegate methods not being called in swift classswift 类中未调用委托方法
【发布时间】:2021-02-15 14:35:21
【问题描述】:

我正在集成一个跟踪位置点的 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 文件中时。

这是我正在做的一个简短的 sn-p:

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 中调用所有内容时一样?

【问题讨论】:

  • SetupSDK 中,您正在创建一个TheSDKPluginWrapper 的实例作为本地匿名变量。一旦函数退出,这将被丢弃。您需要在属性中保存对实例的引用,并确保在某处持有对 TheSDKPlugin 对象的引用(也许 Capacitor 会为您这样做?我从未使用过)

标签: ios swift delegates


【解决方案1】:

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

extension TripsViewController: TheSDKSwiftDelegate {

   // TheSDK Delegate callbacks

   func processStart(ofDrive startInfo: DriveStartInfo) {

   }
   ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-13
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2011-08-12
    相关资源
    最近更新 更多