diff --git a/HNNow.xcodeproj/project.pbxproj b/HNNow.xcodeproj/project.pbxproj index db461e9..5fe7591 100644 --- a/HNNow.xcodeproj/project.pbxproj +++ b/HNNow.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 653C2E032344468C008C9619 /* NetworkManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 653C2E022344468C008C9619 /* NetworkManager.swift */; }; 7C7E7A1123440B1B00340C15 /* ShareSheet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C7E7A1023440B1B00340C15 /* ShareSheet.swift */; }; 8F2953B422AD6DF7000625CB /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F2953B322AD6DF7000625CB /* AppDelegate.swift */; }; 8F2953B622AD6DF7000625CB /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F2953B522AD6DF7000625CB /* SceneDelegate.swift */; }; @@ -42,6 +43,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 653C2E022344468C008C9619 /* NetworkManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkManager.swift; sourceTree = ""; }; 7C7E7A1023440B1B00340C15 /* ShareSheet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShareSheet.swift; sourceTree = ""; }; 8F2953B022AD6DF7000625CB /* HNNow.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = HNNow.app; sourceTree = BUILT_PRODUCTS_DIR; }; 8F2953B322AD6DF7000625CB /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; @@ -124,6 +126,7 @@ 7C7E7A1023440B1B00340C15 /* ShareSheet.swift */, 8F2953BE22AD6DF9000625CB /* LaunchScreen.storyboard */, 8F2953C122AD6DF9000625CB /* Info.plist */, + 653C2E022344468C008C9619 /* NetworkManager.swift */, ); path = HNNow; sourceTree = ""; @@ -303,6 +306,7 @@ 8F78FADB22B0449700A9FCA4 /* ActivityIndicatorView.swift in Sources */, 8F775E1222B0359400FDDEF2 /* WebView.swift in Sources */, 8F2953B422AD6DF7000625CB /* AppDelegate.swift in Sources */, + 653C2E032344468C008C9619 /* NetworkManager.swift in Sources */, 8F2953B622AD6DF7000625CB /* SceneDelegate.swift in Sources */, 8F7FE12822AD774900EBD44D /* StoryRow.swift in Sources */, 8FB4AB7322AD7DB200363A7C /* StoryStore.swift in Sources */, diff --git a/HNNow/AppDelegate.swift b/HNNow/AppDelegate.swift index b61d708..16b9aba 100644 --- a/HNNow/AppDelegate.swift +++ b/HNNow/AppDelegate.swift @@ -11,15 +11,17 @@ import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { - + var network = NetworkManager() func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. + network.startNotifier() return true } func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. + network.stopNotifier() } // MARK: UISceneSession Lifecycle diff --git a/HNNow/NetworkManager.swift b/HNNow/NetworkManager.swift new file mode 100644 index 0000000..45f30e9 --- /dev/null +++ b/HNNow/NetworkManager.swift @@ -0,0 +1,42 @@ +// +// NetworkManager.swift +// HNNow +// +// Created by Rizwan on 02/10/19. +// Copyright © 2019 Nathaniel Fredericks. All rights reserved. +// + +import Foundation +import UIKit +import Network + +class NetworkManager { + private var monitor:NWPathMonitor + private let queue = DispatchQueue(label: "NetworkMonitor") + private let alert: UIAlertController + + init() { + monitor = NWPathMonitor() + alert = UIAlertController(title: "Network Error", message: "Check your internet connection.", preferredStyle: .alert) + alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil)) + + monitor.pathUpdateHandler = { path in + if path.status != .satisfied { + print("No connection.") + DispatchQueue.main.async { + if (!self.alert.isBeingPresented) { + UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.rootViewController?.present(self.alert, animated: true, completion: nil) + } + } + } + } + } + + func startNotifier() { + monitor.start(queue: queue) + } + + func stopNotifier() { + monitor.cancel() + } +}