Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show error when not connected to internet #5

Merged
2 commits merged into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions HNNow.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -42,6 +43,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
653C2E022344468C008C9619 /* NetworkManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkManager.swift; sourceTree = "<group>"; };
7C7E7A1023440B1B00340C15 /* ShareSheet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShareSheet.swift; sourceTree = "<group>"; };
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 = "<group>"; };
Expand Down Expand Up @@ -124,6 +126,7 @@
7C7E7A1023440B1B00340C15 /* ShareSheet.swift */,
8F2953BE22AD6DF9000625CB /* LaunchScreen.storyboard */,
8F2953C122AD6DF9000625CB /* Info.plist */,
653C2E022344468C008C9619 /* NetworkManager.swift */,
);
path = HNNow;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down
4 changes: 3 additions & 1 deletion HNNow/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions HNNow/NetworkManager.swift
Original file line number Diff line number Diff line change
@@ -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()
}
}