SWIFT: How I can use subscriberCellularProviderDidUpdateNotifier with Swift

时间:2023-01-22 16:28:02

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.

我想在Swift中使用subscriberCellularProviderDidUpdateNotifier,但我不知道如何以及在何处放置它。有任何想法吗?我已经搜索过了,但只有Objective-C样本可用。

2 个解决方案

#1


2  

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

在Objective C中你只需要使用“set”和属性块的名称,而在Swift中你需要分配它:

// 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 CTCarreir.

载体将是CTCarreir类型。

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

当然,你总是可以使用引用CTCarreir参数的$ 0:

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

and it looks a lot more cleaner.

它看起来更清洁了。

#2


-1  

I couldn't test it but this is the code from this answer: Possible way to detect sim card detection in ios? translated to swift:

我无法测试它,但这是这个答案的代码:在ios中检测SIM卡检测的可能方法?翻译成swift:

override init() {
    super.init()
    print("init");

    let info = CTTelephonyNetworkInfo()
    info.subscriberCellularProviderDidUpdateNotifier = self.cellularProviderDidUpdate
}
func cellularProviderDidUpdate(inCTCarrier: CTCarrier) {
    print("event")
}

Don't forget import CoreTelephony

不要忘记导入CoreTelephony

#1


2  

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

在Objective C中你只需要使用“set”和属性块的名称,而在Swift中你需要分配它:

// 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 CTCarreir.

载体将是CTCarreir类型。

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

当然,你总是可以使用引用CTCarreir参数的$ 0:

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

and it looks a lot more cleaner.

它看起来更清洁了。

#2


-1  

I couldn't test it but this is the code from this answer: Possible way to detect sim card detection in ios? translated to swift:

我无法测试它,但这是这个答案的代码:在ios中检测SIM卡检测的可能方法?翻译成swift:

override init() {
    super.init()
    print("init");

    let info = CTTelephonyNetworkInfo()
    info.subscriberCellularProviderDidUpdateNotifier = self.cellularProviderDidUpdate
}
func cellularProviderDidUpdate(inCTCarrier: CTCarrier) {
    print("event")
}

Don't forget import CoreTelephony

不要忘记导入CoreTelephony