我正在通过Swift编写类似于runkeeper的程序来跟踪用户的移动。我在监控用户位置方面没有任何问题,但是,我想知道是否有办法确定用户是否已进入建筑物。
Geofencing克服了我的想法但这意味着要确定用户输入的每个建筑物。
想到的另一个想法是不断激发确定用户信号的代码(但这看起来有点烦人 - 尽管如果这是我留下的唯一选择,我会这样做)。我的理由是,如果我无法确定用户是否已进入建筑物,我至少可以通过CoreTelephony框架检测到他们的信号是否很差。
每当初始化一个运行时(通过一个我称之为WalkingEngine的对象 - 注意它还没有在这里实现),我使用WalkingEngine作为我的Reachability助手的自定义init的一部分,并开始连续发送该信号的过程(可能有更好的方法来做到这一点,例如等待信号实际上变差的时候?)这是代码:
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
}
}