Hey guys, I'm trying to turn this method into K/N,...
# kotlin-native
j
Hey guys, I'm trying to turn this method into K/N, but my cast always fails:
Copy code
func isVPNConnected() -> Bool {
    let cfDict = CFNetworkCopySystemProxySettings()
    let nsDict = cfDict!.takeRetainedValue() as NSDictionary
    let keys = nsDict["__SCOPED__"] as! NSDictionary

    for key: String in keys.allKeys as! [String] {
        if (key == "tap" || key == "tun" || key == "ppp" || key == "ipsec" || key == "ipsec0") {
            return true
        }
    }
    return false
}
Kotlin:
Copy code
fun isVPNConnected(): Boolean {
    val cfDict = CFNetworkCopySystemProxySettings()
    val nsDifct = cfDict as? NSDictionary //It fails here
    val keys = nsDifct?.valueForKey("__SCOPED__") as NSDictionary?

    return keys?.allKeys?.any { key ->
        key == "tap" || key == "tun" || key == "ppp" || key == "ipsec" || key == "ipsec0" || key == "utun1"
    } ?: false
}
Could anybody help me to understand how
takeRetainedValue
should be mapped into kotlin?
a
Maybe something like
CFBridgingRetain
would make sense here but I’m not expert enough.
☝️ 1
j
@Artyom Degtyarev [JB] CFBridgingRetain seems like a good direction, but unfortunately it didn't help
a
Maybe something like
Copy code
val cfDict:CFDictionaryRef? = CFNetworkCopySystemProxySettings()
val nsDifct:NSDictionary = CFBridgingRelease(cfDict) as NSDictionary
should work? I got no working sample for now, but at least it follows some logic if I understand things correctly.
r
Yeah that really looks like something I do sometimes
j
Thanks guys, I'll give it a go soon
It worked, thanks a lot 🙂
🎉 1