我正在通过 Swift 编写一个类似于 runkeeper 的程序来跟踪用户的移动。我在监控用户的位置方面没有任何问题,但是,我想知道是否有办法确定用户是否进入了建筑物。
我想到了地理围栏,但这意味着确定用户进入的每一栋建筑。
想到的另一个想法是不断触发确定用户信号的代码(但这似乎有点烦人——尽管如果这是我剩下的唯一选择,我会这样做)。我的理由是,如果我无法确定用户是否进入了建筑物,我至少可以通过 CoreTelephony 框架检测到他们何时信号不佳。
每当初始化一次运行时(通过我称为 WalkingEngine 的对象——注意它还没有在此处实现),我使用该 WalkingEngine 作为我的可达性助手的自定义初始化的一部分,并开始持续发送该信号的过程(可能有一个更好的方法来做到这一点,例如等待信号实际上变差的时候?)这是代码:
import Foundation
import CoreTelephony
//When can we detect whether we have a connection or not? You'll have to lose a connection.
//MARK: Initialize the actual enum? We don't know where it's going to implemented otherwise,
enum ConnectionQuality : Int {
case EBWConnectionQualityZero, EBWConnectionQualityLo, EBWConnectionQualityHi
}
class ReachabilityHelper {
//TODO Determine best initialization method. Check
//WalkEngine Init for reference
//MARK: Check Phone's general connection status
// func checkForGPSConnection () {
//
// let reachability: Reachability
// let networkStatus = reachability.currentReachabilityStatus
//
// switch (networkStatus) {
// case .NotReachable
//
// }
//
//
// }
//MARK: Check for the Phone's Connection Type
func radioAccessType() -> String {
//Print the current telephone's connection type for development
//purposes.
var radioTelephoneInfo: CTTelephonyNetworkInfo
print(radioTelephoneInfo)
radioTelephoneInfo.currentRadioAccessTechnology
}
//MARK: Check for the quality of the Connection Type
//TODO This is an incredibly ugly if-else statement. There are much more elegenat ways
//to express this via Swift. This code was written quickly so as to solve the problem.
func connectionQualityForTechnology(ebwConnectionQuality: ConnectionQuality) -> (radioAccessTechnology: String) {
if radioAccessTechnology == CTRadioAccessTechnologyGPRS {
return ConnectionQuality.EBWConnectionQualityZero
}
else if radioAccessTechnology == CTRadioAccessTechnologyEdge{
return ConnectionQuality.EBWConnectionQualityZero
}
else if radioAccessTechnology == CTRadioAccessTechnologyWCDMA {
return ConnectionQuality.EBWConnectionQualityLo
}
else if radioAccessTechnology == CTRadioAccessTechnologyHSDPA {
return ConnectionQuality.EBWConnectionQualityLo
}
else if radioAccessTechnology == CTRadioAccessTechnologyHSUPA {
return ConnectionQuality.EBWConnectionQualityLo
}
else if radioAccessTechnology == CTRadioAccessTechnologyCDMA1x {
return ConnectionQuality.EBWConnectionQualityZero
}
else if radioAccessTechnology == CTRadioAccessTechnologyCDMAEVDORev0 {
return ConnectionQuality.EBWConnectionQualityLo
}
else if radioAccessTechnology == CTRadioAccessTechnologyCDMAEVDORevA {
return ConnectionQuality.EBWConnectionQualityLo
}
else if radioAccessTechnology == CTRadioAccessTechnologyCDMAEVDORevB {
return ConnectionQuality.EBWConnectionQualityLo
}
else if radioAccessTechnology == CTRadioAccessTechnologyeHRPD {
return ConnectionQuality.EBWConnectionQualityHi
}
else if radioAccessTechnology == CTRadioAccessTechnologyLTE {
return ConnectionQuality.EBWConnectionQualityHi
}
return ConnectionQuality.EBWConnectionQualityHi
}
}