gpt4 book ai didi

ios - 如何同步检查互联网连接?

转载 作者:行者123 更新时间:2023-11-28 22:03:32 24 4
gpt4 key购买 nike

如何同步检测网络?我希望网络检查阻塞调用线程并返回正确的结果

我试过了 tonymillion's ReachabilityAFNetworkReachabilityManager但他们都使用回调 block 。表示回调前可达性状态未知。

我想在 applicationDidFinishLaunchingWithOptions: 检查网络,但此时,可达性为 AFNetworkReachabilityStatusUnknown (AFNetworkReachabilityManager) 或不可靠(tonymillion 的 Reachability)

我看到唯一的方法是像这样对某些主机(例如 google.com)执行 NSURLConnection Check for internet connection - iOS SDK

有没有更好的办法?

最佳答案

回答我自己的问题:靠谱

阅读此 Technical Q&A QA1693 Synchronous Networking On The Main Thread

reachability — The System Configuration framework reachability API () operates synchronously by default. Thus, seemingly innocuous routines like SCNetworkReachabilityGetFlags can get you killed by the watchdog. If you're using the reachability API, you should use it asynchronously. This involves using the SCNetworkReachabilityScheduleWithRunLoop routine to schedule your reachability queries on the run loop

所以我们可以这样使用它iOS: Check whether internet connection is available

- (BOOL) isConnectionAvailable
{
SCNetworkReachabilityFlags flags;
BOOL receivedFlags;

SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(CFAllocatorGetDefault(), [@"dipinkrishna.com" UTF8String]);
receivedFlags = SCNetworkReachabilityGetFlags(reachability, &flags);
CFRelease(reachability);

if (!receivedFlags || (flags == 0) )
{
return FALSE;
} else {
return TRUE;
}
}

关于ios - 如何同步检查互联网连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24535842/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com