ฉันเป็นมือใหม่ในการพัฒนาแอป IOS และต้องการ "แจ้งเตือนป๊อปอัปเมื่อการเชื่อมต่อการเข้าถึงขาดหายไประหว่างการใช้แอป (IOS xcode รวดเร็ว)" แต่ฉันจะได้รับการแจ้งเตือนป๊อปอัปเมื่อเริ่มแอปเท่านั้น ไม่มีป๊อปอัปแจ้งเตือนระหว่างใช้แอปของฉันเมื่อการเชื่อมต่ออินเทอร์เน็ตขาดหาย กรุณาช่วยด้วยขอบคุณ!
สิ่งที่ฉันทำ: 1) สร้างไฟล์ Reachability.swift และ worte
import Foundation
public class Reachability {
class func isConnectedToNetwork()->Bool{
var Status:Bool = false
let url = NSURL(string: "http://google.com/")
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "HEAD"
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData
request.timeoutInterval = 10.0
var response: NSURLResponse?
var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: nil) as NSData?
if let httpResponse = response as? NSHTTPURLResponse {
if httpResponse.statusCode == 200 {
Status = true
}
}
return Status
}
}
2) แก้ไขไฟล์ ViewController.swift ดังต่อไปนี้
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var WebView: UIWebView!
//ViewDidLoad method
override func viewDidLoad() {
super.viewDidLoad()
if Reachability.isConnectedToNetwork() == true {
println("Internet connection OK")
} else {
println("Internet connection FAILED")
var alert = UIAlertView(title: "No Internet Connection",
message: "Make sure your device is connected to the internet.",
delegate: nil,
cancelButtonTitle: "OK")
alert.show()
}
var URL = NSURL(string: "http://www.google.com/")
WebView.loadRequest(NSURLRequest(URL: URL!))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}