การแจ้งเตือนป๊อปอัปเมื่อการเชื่อมต่อการเข้าถึงหายไประหว่างการใช้แอพ (IOS xcode รวดเร็ว)

ฉันเป็นมือใหม่ในการพัฒนาแอป 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()
}

}

person Eric Chong    schedule 14.07.2015    source แหล่งที่มา


คำตอบ (1)


ลองใช้คลาส ความสามารถในการเข้าถึง นี้ เพิ่มลงในโปรเจ็กต์ของคุณและทำสิ่งต่อไปนี้ใน viewController

let reachability = Reachability.reachabilityForInternetConnection()

reachability.whenReachable = { reachability in
if reachability.isReachableViaWiFi() {
 let alertController = UIAlertController(title: "Alert", message: "Reachable via WiFi", preferredStyle: .Alert)

    let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
    alertController.addAction(defaultAction)

    presentViewController(alertController, animated: true, completion: nil)

} 
else {
let alertController = UIAlertController(title: "Alert", message: "Reachable via Cellular", preferredStyle: .Alert)

    let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
    alertController.addAction(defaultAction)

    presentViewController(alertController, animated: true, completion: nil)
    }
  }
reachability.whenUnreachable = { reachability in
let alertController = UIAlertController(title: "Alert", message: "Not Reachable", preferredStyle: .Alert)

    let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
    alertController.addAction(defaultAction)

    presentViewController(alertController, animated: true, completion: nil)
}

reachability.startNotifier()
person iAnurag    schedule 14.07.2015
comment
ขอบคุณ แต่ฉันเป็นมือใหม่จริงๆ โปรดช่วยแนะนำฉันทีละขั้นตอนว่าต้องทำอย่างไร - person Eric Chong; 14.07.2015
comment
1.) เพิ่มไฟล์ accessability.swift ในโครงการของคุณ 2)และวางโค้ดด้านบนในวิธี viewDidLoad ของคุณ หรือเมธอด applicationDidFinishLaunchingWithOptions และรันโค้ด - person iAnurag; 14.07.2015
comment
ขอบคุณมาก! ฉันทำตามคำตอบของคุณและมันได้ผล 99% ฉันใส่วิธี viewDidLoad ของคุณเป็น ViewController - person Eric Chong; 14.07.2015
comment
ปัญหาเดียวในตอนนี้คือไม่มีการแจ้งเตือนเมื่อเริ่มแอปโดยไม่ต้องเชื่อมต่ออินเทอร์เน็ต คุณช่วยได้ไหม? - person Eric Chong; 14.07.2015
comment
แทนที่จะ println ให้ใส่รหัสแจ้งเตือนของคุณ และ UIAlertView เลิกใช้แล้วใน iOS8 ใช้ UIAlertController - person iAnurag; 14.07.2015
comment
ใช่ฉันทำ. นั่นเป็นสาเหตุที่ทำให้แอปแสดงการแจ้งเตือนหากแอปเริ่มทำงานแล้วการเชื่อมต่อล้มเหลว แต่หากไม่มีการเชื่อมต่อและเริ่มแอป จะไม่มีการแจ้งเตือน Code accessability.whenUnreachable = { ความสามารถในการเข้าถึงใน println (ไม่สามารถเข้าถึงได้) var alert = UIAlertView (หัวข้อ: ไม่มีการเชื่อมต่ออินเทอร์เน็ต ข้อความ: ตรวจสอบให้แน่ใจว่าอุปกรณ์ของคุณเชื่อมต่อกับอินเทอร์เน็ต ผู้รับมอบสิทธิ์: ไม่มี, cancelButtonTitle: ตกลง) alert.show() } - person Eric Chong; 14.07.2015
comment
ใช้ self.presentviewController ใน viewDidLoad - person iAnurag; 14.07.2015
comment
ตอนนี้ไม่มีข้อผิดพลาดแต่ก็เหมือนเดิม ไม่มีการแจ้งเตือนเมื่อเริ่มแอปโดยไม่ต้องเชื่อมต่ออินเทอร์เน็ต - person Eric Chong; 14.07.2015
comment
Dispatch_async มีอยู่ในโปรเจ็กต์ของฉัน ฉันค้นหาบนเว็บและมันซับซ้อนเกินไปสำหรับฉันที่จะเข้าใจในขณะนี้ ฉันต้องการเวลาในการตรวจสอบ - person Eric Chong; 14.07.2015