Skip to content

Commit 3890886

Browse files
committed
添加一个本地使用 Observable 的网络监控包,监控网络情况
1 parent 3ca6799 commit 3890886

File tree

7 files changed

+106
-1
lines changed

7 files changed

+106
-1
lines changed

SwiftPamphletApp.xcodeproj/project.pbxproj

+16-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
archiveVersion = 1;
44
classes = {
55
};
6-
objectVersion = 55;
6+
objectVersion = 60;
77
objects = {
88

99
/* Begin PBXBuildFile section */
@@ -251,6 +251,7 @@
251251
08CD61FE27758B8A008C0935 /* Lexer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08CD61FC27758B8A008C0935 /* Lexer.swift */; };
252252
08CD61FF27758B8A008C0935 /* Token.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08CD61FD27758B8A008C0935 /* Token.swift */; };
253253
08D107BD278826BB007B7009 /* HTMLEntities in Frameworks */ = {isa = PBXBuildFile; productRef = 08D107BC278826BB007B7009 /* HTMLEntities */; };
254+
08ED80162B9C54DE0069B7EC /* SMNetwork in Frameworks */ = {isa = PBXBuildFile; productRef = 08ED80152B9C54DE0069B7EC /* SMNetwork */; };
254255
08F4BE6028609D8700733F12 /* PlayCharts.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08F4BE5F28609D8600733F12 /* PlayCharts.swift */; };
255256
08F4BE6228616DC100733F12 /* PlayWeatherKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08F4BE6128616DC100733F12 /* PlayWeatherKit.swift */; };
256257
08F51BC527A374A500693AB6 /* footer_js.html in Resources */ = {isa = PBXBuildFile; fileRef = 08F51BC427A374A500693AB6 /* footer_js.html */; };
@@ -514,6 +515,7 @@
514515
08448F4E279E8CA400B61353 /* Ink in Frameworks */,
515516
08BE634027BF953A002BC6A8 /* CodeEditorView in Frameworks */,
516517
08BF26D32768A5B40064DDAC /* MarkdownUI in Frameworks */,
518+
08ED80162B9C54DE0069B7EC /* SMNetwork in Frameworks */,
517519
08D107BD278826BB007B7009 /* HTMLEntities in Frameworks */,
518520
);
519521
runOnlyForDeploymentPostprocessing = 0;
@@ -1315,6 +1317,7 @@
13151317
08448F082796A83D00B61353 /* SwiftDate */,
13161318
08448F4D279E8CA400B61353 /* Ink */,
13171319
08BE633F27BF953A002BC6A8 /* CodeEditorView */,
1320+
08ED80152B9C54DE0069B7EC /* SMNetwork */,
13181321
);
13191322
productName = SwiftPamphletApp;
13201323
productReference = 086A5F032744E88E00FECE02 /* 戴铭的开发小册子.app */;
@@ -1352,6 +1355,7 @@
13521355
08448F072796A83D00B61353 /* XCRemoteSwiftPackageReference "SwiftDate" */,
13531356
08448F4C279E8CA400B61353 /* XCRemoteSwiftPackageReference "ink" */,
13541357
08BE633E27BF953A002BC6A8 /* XCRemoteSwiftPackageReference "CodeEditorView" */,
1358+
08ED80142B9C54DE0069B7EC /* XCLocalSwiftPackageReference "SwiftPamphletApp/SharePackage/SMNetwork" */,
13551359
);
13561360
productRefGroup = 086A5F042744E88E00FECE02 /* Products */;
13571361
projectDirPath = "";
@@ -1842,6 +1846,13 @@
18421846
};
18431847
/* End XCConfigurationList section */
18441848

1849+
/* Begin XCLocalSwiftPackageReference section */
1850+
08ED80142B9C54DE0069B7EC /* XCLocalSwiftPackageReference "SwiftPamphletApp/SharePackage/SMNetwork" */ = {
1851+
isa = XCLocalSwiftPackageReference;
1852+
relativePath = SwiftPamphletApp/SharePackage/SMNetwork;
1853+
};
1854+
/* End XCLocalSwiftPackageReference section */
1855+
18451856
/* Begin XCRemoteSwiftPackageReference section */
18461857
08448F072796A83D00B61353 /* XCRemoteSwiftPackageReference "SwiftDate" */ = {
18471858
isa = XCRemoteSwiftPackageReference;
@@ -1924,6 +1935,10 @@
19241935
package = 08D107BB278826BA007B7009 /* XCRemoteSwiftPackageReference "swift-html-entities" */;
19251936
productName = HTMLEntities;
19261937
};
1938+
08ED80152B9C54DE0069B7EC /* SMNetwork */ = {
1939+
isa = XCSwiftPackageProductDependency;
1940+
productName = SMNetwork;
1941+
};
19271942
/* End XCSwiftPackageProductDependency section */
19281943
};
19291944
rootObject = 086A5EFB2744E88E00FECE02 /* Project object */;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm/configuration/registries.json
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
.netrc
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// swift-tools-version: 5.10
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "SMNetwork",
8+
platforms: [.iOS(.v17), .macOS(.v14)],
9+
products: [
10+
// Products define the executables and libraries a package produces, making them visible to other packages.
11+
.library(
12+
name: "SMNetwork",
13+
targets: ["SMNetwork"]),
14+
],
15+
targets: [
16+
// Targets are the basic building blocks of a package, defining a module or a test suite.
17+
// Targets can depend on other targets in this package and products from dependencies.
18+
.target(
19+
name: "SMNetwork"),
20+
.testTarget(
21+
name: "SMNetworkTests",
22+
dependencies: ["SMNetwork"]),
23+
]
24+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// The Swift Programming Language
2+
// https://docs.swift.org/swift-book
3+
4+
5+
import Observation
6+
import Network
7+
8+
@Observable
9+
public final class NetworkMonitor {
10+
public var hasNetworkConnection = true
11+
public var isUsingMobileConnection = false // low data usage ( 3G / 4G / etc )
12+
13+
private let networkMonitor = NWPathMonitor()
14+
15+
public init() {
16+
networkMonitor.pathUpdateHandler = { [weak self] path in
17+
self?.hasNetworkConnection = path.status == .satisfied
18+
self?.isUsingMobileConnection = path.usesInterfaceType(.cellular)
19+
}
20+
21+
networkMonitor.start(queue: DispatchQueue.global())
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import XCTest
2+
@testable import SMNetwork
3+
4+
final class SMNetworkTests: XCTestCase {
5+
func testExample() throws {
6+
// XCTest Documentation
7+
// https://developer.apple.com/documentation/xctest
8+
9+
// Defining Test Cases and Test Methods
10+
// https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods
11+
}
12+
}

SwiftPamphletApp/SwiftPamphletAppApp.swift

+15
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import SwiftUI
99
import Combine
10+
import SMNetwork
1011

1112
@main
1213
struct SwiftPamphletAppApp: App {
@@ -52,8 +53,12 @@ struct V: View {
5253
struct SwiftPamphletApp: View {
5354

5455
@StateObject var appVM = AppVM()
56+
@State var networkMonitor = NetworkMonitor()
57+
5558
@State var sb = Set<AnyCancellable>()
5659
@State var alertMsg = ""
60+
61+
@State private var showNetworkAlert = false // 网络监控
5762

5863
let timerForDevs = Timer.publish(every: SPC.timerForDevsSec, on: .main, in: .common).autoconnect()
5964
let timerForExp = Timer.publish(every: SPC.timerForExpSec, on: .main, in: .common).autoconnect()
@@ -113,6 +118,16 @@ struct SwiftPamphletApp: View {
113118
} // end ToolbarItemGroup
114119
} // end .toolbar
115120
.environmentObject(appVM)
121+
// 网络监控
122+
.environment(networkMonitor)
123+
.onChange(of: networkMonitor.hasNetworkConnection, { oldValue, newValue in
124+
showNetworkAlert = !newValue
125+
})
126+
.alert("NO INTERNET",
127+
isPresented: $showNetworkAlert,
128+
actions: {
129+
Text("Close")
130+
})
116131
}
117132
}
118133

0 commit comments

Comments
 (0)