ViewController.swift
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
var webView: WKWebView!
var reachability: Reachability?
let hostNames = ["baidu.com", "qq.com", "apple.com"]
var hostIndex = 0
var gameurl = "http://www.37wan.com/"
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
UIApplication.shared.isIdleTimerDisabled = true
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string:gameurl)
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
// Do any additional setup after loading the view, typically from a nib.
//创建一个字符串常量,作为是否启动过的标示名称
let EVERLAUNCHED = "everlaunched"
//再创建一个字符串常量,作为首次启动的标识符名称
let FIRSTLAUNCH = "firstlaunch"
//获得变量的布尔值,当程序首次启动时由于没有设置过变量,所以默认值为否
if(!UserDefaults.standard.bool(forKey: EVERLAUNCHED))
{
//将标识是否启动过的变量更该为真,标示程序至少被启动过一次
UserDefaults.standard.set(true, forKey: EVERLAUNCHED)
//将标示是否首次启动的变量设为真,标示程序是首次启动
UserDefaults.standard.set(true, forKey: FIRSTLAUNCH)
//调用同步方法,立即保存修改
UserDefaults.standard.synchronize()
}
else{
//如果曾经启动过程序,则变量的布尔值为否
UserDefaults.standard.set(false, forKey: FIRSTLAUNCH)
//调用同步方法,立即保存修改
UserDefaults.standard.synchronize()
}
//初始化一个字符串,作为提示窗口的内容
var message = "It's the first show"
if(!UserDefaults.standard.bool(forKey: FIRSTLAUNCH))
{
message = "it's not first show"
print(message)
//let myURL = URL(string:gameurl)
//let myRequest = URLRequest(url: myURL!)
//webView.load(myRequest)
}else{
print(message)
}
//网络检测
startHost(at: 0)
}
func startHost(at index: Int) {
stopNotifier()
setupReachability(hostNames[index], useClosures: true)
startNotifier()
//DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
// self.startHost(at: (index + 1) % 3)
//}
}
func setupReachability(_ hostName: String?, useClosures: Bool) {
let reachability: Reachability?
if let hostName = hostName {
reachability = try? Reachability(hostname: hostName)
} else {
reachability = try? Reachability()
}
self.reachability = reachability
print("--- set up with host name: \(String(describing: hostName))")
if useClosures {
reachability?.whenReachable = { reachability in
self.updateWhenReachable(reachability)
}
reachability?.whenUnreachable = { reachability in
self.updateWhenNotReachable(reachability)
}
} else {
NotificationCenter.default.addObserver(
self,
selector: #selector(reachabilityChanged(_:)),
name: .reachabilityChanged,
object: reachability
)
}
}
func startNotifier() {
print("--- start notifier")
do {
try reachability?.startNotifier()
} catch {
print("Unable to start\nnotifier")
return
}
}
func stopNotifier() {
print("--- stop notifier")
reachability?.stopNotifier()
NotificationCenter.default.removeObserver(self, name: .reachabilityChanged, object: nil)
reachability = nil
}
func updateWhenReachable(_ reachability: Reachability) {
//print("\(reachability.description) - \(reachability.connection)")
if reachability.connection == .wifi {
print("Reachable via WiFi")
} else {
print("Reachable via Cellular")
}
let myURL = URL(string:gameurl)
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
stopNotifier()
}
func updateWhenNotReachable(_ reachability: Reachability) {
print("\(reachability.description) - \(reachability.connection)")
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
self.startHost(at: 0)
}
}
@objc func reachabilityChanged(_ note: Notification) {
let reachability = note.object as! Reachability
print("reachabilityChanged")
if reachability.connection != .unavailable {
//updateWhenReachable(reachability)
} else {
//updateWhenNotReachable(reachability)
}
}
deinit {
stopNotifier()
print("deinit...")
}
}
有些手机首次打开app白屏,主要是因为首次打开app需要授权网络访问权限,因此加上检测网络状态来解决这个问题。