iOS make the SDK work on extension

I need to track event on extension, but for now it's not possible with the actual SDK

'sharedApplication' is unavailable: not available on iOS (App Extension) - Use view controller based solutions where appropriate instead.

some SDk have workaround

extension UIApplication: CountlyCompatible { }
extension Countly where Base: UIApplication {
    public static var shared: UIApplication? {
        let selector = NSSelectorFromString("sharedApplication")
        guard Base.responds(to: selector) else { return nil }
        return Base.perform(selector).takeUnretainedValue() as? UIApplication
    }
}


public protocol CountlyCompatible {
    associatedtype CompatibleType
    var c: CompatibleType { get }
}

public extension CountlyCompatible {
    public var c: Countly<Self> {
        return Kingfisher(self)
    }
}

use:

    // if 'sharedApplication()' is unavailable, then return
    guard let sharedApplication = Countly<UIApplication>.shared else { return }
0

Comments

3 comments

Please sign in to leave a comment.