iOS Get Signal Strength in Swift (Core Telephony)
Asked Answered
D

2

3

I'm new to iOS, and am learning to code with Swift. My app needs to measure the signal strength. I've found this code working on Objective-C/C, and need some help to implement on Swift. Here is what I got. Hope someone can help me finish it.

OBJECTIVE C

    int getSignalStrength()
    {
       void *libHandle = dlopen("/System/Library/Frameworks/CoreTelephony.framework/CoreTelephony", RTLD_LAZY);
       int (*CTGetSignalStrength)();
       CTGetSignalStrength = dlsym(libHandle, "CTGetSignalStrength");
       if( CTGetSignalStrength == NULL) NSLog(@"Could not find CTGetSignalStrength");
       int result = CTGetSignalStrength();
       dlclose(libHandle);
       return result;
    }

SWIFT

    func getSignalStrength()->Int{
       var result : Int! = 0
       let libHandle = dlopen ("/System/Library/Frameworks/CoreTelephony.framework/CoreTelephony", RTD_LAZY)
       ** help **
       var CTGetSignalStrength = dlsym(libHandle, "CTGetSignalStrength")
       if (CTGetSignalStrength != nil){
           result = CTGetSignalStrength()
       }
       dlclose(libHandle)
       return result
    }
Dystopia answered 21/8, 2014 at 15:2 Comment(3)
Why don't you implement it as class method in obj-c class?Khat
Im trying to make it a Swift-only app.Dystopia
Did you solve it? There is just no way to do it? :(Piliferous
S
2

Don't use dlopen to load CoreTelephony. Use import CoreTelephony at the top of your Swift file. Then just use CTGetSignalStrength as if it were any other function.

Separatrix answered 21/8, 2014 at 15:16 Comment(2)
Then there is an error : Use of unresolved identifier 'CTGetSignalStrength'Dystopia
Ah, it's a private API. I don't think it's possible to use private APIs directly from Swift. However, you can keep the working Objective-C code and use it from Swift.Separatrix
I
2

Swift 3 Solution

import CoreTelephony
import Darwin

    static func getSignalStrength()->Int{
        var result : Int = 0
        //int CTGetSignalStrength();
        let libHandle = dlopen ("/System/Library/Frameworks/CoreTelephony.framework/CoreTelephony", RTLD_NOW)
        let CTGetSignalStrength2 = dlsym(libHandle, "CTGetSignalStrength")

        typealias CFunction = @convention(c) () -> Int

        if (CTGetSignalStrength2 != nil) {
            let fun = unsafeBitCast(CTGetSignalStrength2!, to: CFunction.self)
            let result = fun()
            return result;
            print("!!!!result \(result)")
        } 
     return -1
 }
Iormina answered 7/10, 2017 at 23:28 Comment(4)
This function always returns 100 for me on iOS11, does it still work for you on iOS11?Engobe
No unfortunately this solution doesn't work anymore since iOS11.Iormina
Thanks for the info. Do you by any chance know of a method that works on iOS11 as well?Engobe
Nothing that I know of, I can refer you to this video which explains how to use builtin field tester app: youtube.com/watch?v=GBCi2zeSra4 Only way to figure this out I think is to decompile field tester app.Iormina

© 2022 - 2024 — McMap. All rights reserved.