【问题标题】:opening an app without internet connection and showing alert on iOS在没有互联网连接的情况下打开应用程序并在 iOS 上显示警报
【发布时间】: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


【解决方案1】:
    let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
     alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil)) 
self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)

【讨论】:

  • 我收到了这个error..可能是什么错误?(drive.google.com/file/d/0B0Je1_NFqcfXWHUwdGVXSkxZR1E/…)
  • @Babymouse 您只需要在视图控制器上显示视图控制器。当您在应用程序委托类中编写此代码时,您可以执行self.window!.rootviewcontroller!.presentViewControoler(alert) 之类的操作
  • 非常感谢@SaurabhJain
  • 非常感谢@DashAndRest
  • @SaurabhJain 这适用于我在没有互联网连接的情况下打开我的应用程序时的应用程序吗?因为我现在遇到的是,只要我没有互联网连接,我的应用程序就会继续加载。跨度>
猜你喜欢
  • 1970-01-01
  • 2018-07-08
  • 2016-01-17
  • 2017-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多