Search code examples
iosswiftcore-telephony

SWIFT: How I can use subscriberCellularProviderDidUpdateNotifier with Swift


I want use subscriberCellularProviderDidUpdateNotifier in Swift, but I don't know how and where it can be placed.
Any ideas? I have searched for that, but only Objective-C samples are available.


Solution

  • In Objective C you just need to use "set" and the name of the property block while in Swift you need to assign it:

    // Declare your class member
    let networkInfo = CTTelephonyNetworkInfo();
    
    // In viewDidLoad or in your custom method
    networkInfo.subscriberCellularProviderDidUpdateNotifier = { carrier in
        // Do whatever you wanna do when a callback comes        
    }
    

    carrier will be of type CTCarrier.

    Of course, you can always use the $0 which refers to CTCarrier argument:

    networkInfo.subscriberCellularProviderDidUpdateNotifier = { 
     $0      
     // Do whatever you need to do with it
    }
    

    and it looks a lot more cleaner.