2016-08-14 4 views
1

接続が失われたときに、アプリケーションの任意のビューにアラートを表示する最善の方法は何ですか。接続が失われたときにUIAlertViewを表示します。

現在使用して:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AppDelegate.networkStatusChanged(_:)), name: ReachabilityStatusChangedNotification, object: nil) 
Reach().monitorReachabilityChanges() 

func networkStatusChanged(notification: NSNotification) { 
    if let info = notification.userInfo as? [String:String] { 
     let status = info["Status"]! 
     if !status.containsString("Online") { 

     } 
    } 
} 

答えて

3

UIAlertViewsではなくUIAlertControllerを使用し、推奨されていません。

let alert = UIAlertController(title: "Error", message: "You you seem to have lost internet connectivity.", preferredStyle: .Alert) 
alert.addAction(UIAlertAction(title: "Dismiss", style: .Cancel, handler: nil)) 
self.presentViewController(alert, animated: true, completion: nil) 
1

このコードを使用すると、プロジェクトのすべてのビューに警告が表示されます。例については

let imageView = UIImageView(frame: CGRectMake(70, 8, 30, 30)) //left side of alertview 
imageView.image = UIImage(named: "error") //add this if you like to show error icon on alertview 


let alertController = UIAlertController(title: "Alert!", message: "\nPlease Check Your Internet Availability", preferredStyle: UIAlertControllerStyle.Alert) 
alertController.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.Default,handler: nil)) 
alertController.view.addSubview(imageView) //Add this if you like to user image on alerview 
self.presentViewController(alertController, animated: true, completion: nil) 

func networkStatusChanged(notification: NSNotification) { 
     if let info = notification.userInfo as? [String:String] { 
      let status = info["Status"]! 
      if !status.containsString("Online") { 
       let imageView = UIImageView(frame: CGRectMake(70, 8, 30, 30)) //left side of alertview 
       imageView.image = UIImage(named: "error") //add this if you like to show error icon on alertview 


       let alertController = UIAlertController(title: "Alert!", message: "\nPlease Check Your Internet Availability", preferredStyle: UIAlertControllerStyle.Alert) 
       alertController.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.Default,handler: nil)) 
       alertController.view.addSubview(imageView) //Add this if you like to user image on alerview 
       self.presentViewController(alertController, animated: true, completion: nil) 
      } 
      else 
      { 
       print("Available") 
      } 
     } 
    } 
+0

親愛なる接続は、私はすべての画面だけではなく、一度アラートビューのためのコードを記述する必要が警告を表示失ったら、私の質問は –

関連する問題