手机处于飞行模式时如何检测 iCloud 用户?
Posted
技术标签:
【中文标题】手机处于飞行模式时如何检测 iCloud 用户?【英文标题】:How to detect iCloud user when phone is in airplane mode? 【发布时间】:2016-09-19 13:17:17 【问题描述】:首先我知道这个帖子:How to identify iCloud logged in user in airplane mode?
但它似乎没有答案。一个答案是关于检测互联网连接,另一个是关于用户是否发生了变化。
这是我的代码:
container.accountStatusWithCompletionHandler
(status: CKAccountStatus, error: NSError?) in
dispatch_async(dispatch_get_main_queue(),
var title: String!
var message: String!
if error != nil
title = "Error"
message = "An error occurred = \(error)"
else
//title = "No errors occurred getting info"
switch status
case .Available:
message = "The user is logged in to iCloud"
title = "GOOD"
print("determined status was available")
self.shouldPullFromICloud()
//self.displayAlertWithTitle(title, message: message)
case .CouldNotDetermine:
message = "Could not determine if the user is logged" +
" into iCloud or not"
title = "BAD"
self.noUserIsSignedIn()
case .NoAccount:
message = "User is not logged into iCloud"
title = "BAD"
self.noUserIsSignedIn()
case .Restricted:
message = "Could not access user's iCloud account information"
title = "BAD"
self.noUserIsSignedIn()
print(title, message)
)
您需要互联网连接才能查看用户是否已登录 iCloud,这似乎很愚蠢。我该如何解决这个问题?
谢谢!
附: 如果其他帖子确实包含我的问题的答案,请您为我突出显示它吗?我找不到它。干杯!
【问题讨论】:
【参考方案1】:飞行模式只是关闭蜂窝和 wifi 数据功能。在这种情况下,检查可达性应该与检测飞行模式的目的相同。这是一个标准的 lib sn-p,它将检查可达性:
func connectedToNetwork() -> Bool
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
guard let defaultRouteReachability = withUnsafePointer(&zeroAddress,
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
) else
return false
var flags : SCNetworkReachabilityFlags = []
if SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) == false
return false
let isReachable = flags.contains(.Reachable)
let needsConnection = flags.contains(.ConnectionRequired)
return (isReachable && !needsConnection)
【讨论】:
我出于其他原因使用它,但我仍然需要一种获取用户 ID 的方法,无论他们是否处于飞行模式,你知道吗?以上是关于手机处于飞行模式时如何检测 iCloud 用户?的主要内容,如果未能解决你的问题,请参考以下文章
sim7600ce 4G模块设置飞行模式后重启模块自动关闭飞行模式,这个怎么实现重启后还是飞行模式