【发布时间】:2016-11-27 00:49:50
【问题描述】:
我正在创建“无互联网连接”。这是为了在没有互联网连接的情况下打开应用程序。所以首先我将此代码添加到我的 bridge.h
导入“Reachability.h”
然后将以下方法添加到可达性类。首先在 .h 文件中声明:
+ (BOOL)checkIntenetRechable;
并在 .m 文件中实现
+ (BOOL)checkIntenetRechable
{
BOOL isInternetAvailable;
Reachability *internetReach = [Reachability reachabilityForInternetConnection];
[internetReach startNotifier];
NetworkStatus netStatus = [internetReach currentReachabilityStatus];
BOOL connectionRequired = [internetReach connectionRequired];
NSString *statusString = @"";
switch (netStatus)
{
case NotReachable:
{
statusString = @"Access Not Available";
isInternetAvailable = FALSE;
break;
}
case ReachableViaWWAN:
{
statusString = @"Reachable WWAN";
isInternetAvailable = TRUE;
break;
}
case ReachableViaWiFi:
{
statusString = @"Reachable WiFi";
isInternetAvailable = TRUE;
break;
}
}
if(connectionRequired)
{
statusString = [NSString stringWithFormat: @"%@, Connection Required", statusString];
isInternetAvailable = FALSE;
}
return isInternetAvailable;
}
然后,无论我想检查互联网连接,我都会将以下 sn-p 放在那里并提醒用户。
if Reachability.checkIntenetRechable() == false {
let alertView = UIAlertCole: "APP_NAME", message: "Please check your internet connection.", preferredStyle: UIAlertControllerStyle.Alert)
//alertView.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: { (action: UIAlertAction ) in }))
alertView.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction ) in
// Put some code for okay button
}))
self.presentViewController(alertView, animated: true, completion: nil)
}
过了一会儿我收到了This error...可能是什么问题??
【问题讨论】:
-
不应该将其标记为“objective-c”而不是“c++”吗?
-
将 UIAlertView = "app name", message: ... 更改为 UIAlertView("app name", message: ...)
-
感谢@JesperJuhl 的建议。
-
@Roee84,不要将答案作为 cmets。在下面发布它们作为答案,以便他们可以被接受。
-
@SaurabhJain,不要将答案留作 cmets。在下面发布它们作为答案,以便他们可以被接受。
标签: objective-c iphone swift