From 5b3042b39c4e1dbd7c6c5bd414a191d7574f18d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=88=B4=E9=93=AD?= Date: Mon, 11 Nov 2024 17:57:14 +0800 Subject: [PATCH] =?UTF-8?q?MetricKit=20=E8=8E=B7=E5=8F=96=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E6=97=B6=E9=97=B4=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SwiftPamphletApp.xcodeproj/project.pbxproj | 12 ++++++ .../App/SwiftPamphletAppApp.swift | 12 +++++- .../Performance/MetricManager.swift | 38 +++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 SwiftPamphletApp/Performance/MetricManager.swift diff --git a/SwiftPamphletApp.xcodeproj/project.pbxproj b/SwiftPamphletApp.xcodeproj/project.pbxproj index 7439765d..009164f0 100644 --- a/SwiftPamphletApp.xcodeproj/project.pbxproj +++ b/SwiftPamphletApp.xcodeproj/project.pbxproj @@ -481,6 +481,7 @@ 3A3168952CE0FFCE004DFC5C /* Launch Screen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 3A3168942CE0FFCE004DFC5C /* Launch Screen.storyboard */; platformFilter = ios; }; 3A46DEF52BD7AAB2008AD993 /* SMDate in Frameworks */ = {isa = PBXBuildFile; productRef = 3A46DEF42BD7AAB2008AD993 /* SMDate */; }; 3ACA74FB2BD7964A0024B18E /* SMFile in Frameworks */ = {isa = PBXBuildFile; productRef = 3ACA74FA2BD7964A0024B18E /* SMFile */; }; + 3ADBA84F2CE209FB00B0050B /* MetricManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3ADBA84E2CE209FB00B0050B /* MetricManager.swift */; }; 3AE0D59A2BAB0A0600D6D925 /* DeveloperListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AE0D5992BAB0A0600D6D925 /* DeveloperListView.swift */; }; 3AE0D59E2BAB183100D6D925 /* EditDeveloper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AE0D59D2BAB183100D6D925 /* EditDeveloper.swift */; }; 3AE4CA382BD7813D005BEF2C /* InfoOrganizer in Frameworks */ = {isa = PBXBuildFile; productRef = 3AE4CA372BD7813D005BEF2C /* InfoOrganizer */; }; @@ -958,6 +959,7 @@ 3A30EFD52CDA818B0029CB2F /* WeatherKit(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "WeatherKit(ap).md"; sourceTree = ""; }; 3A3168922CE0FD53004DFC5C /* HomeiOSView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeiOSView.swift; sourceTree = ""; }; 3A3168942CE0FFCE004DFC5C /* Launch Screen.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = "Launch Screen.storyboard"; sourceTree = ""; }; + 3ADBA84E2CE209FB00B0050B /* MetricManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MetricManager.swift; sourceTree = ""; }; 3AE0D5992BAB0A0600D6D925 /* DeveloperListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeveloperListView.swift; sourceTree = ""; }; 3AE0D59D2BAB183100D6D925 /* EditDeveloper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditDeveloper.swift; sourceTree = ""; }; 3AF2A2DE2BE22A8C00F3BE1B /* UnCategoryInfoListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnCategoryInfoListView.swift; sourceTree = ""; }; @@ -1327,6 +1329,7 @@ 086A5F052744E88E00FECE02 /* SwiftPamphletApp */ = { isa = PBXGroup; children = ( + 3ADBA84D2CE209C000B0050B /* Performance */, 086D48A02BBB820100835544 /* Info.plist */, 0846234D2BAC59AF003373D9 /* App */, 084417732B99B8EA0049297D /* HomeUI */, @@ -2261,6 +2264,14 @@ path = Category; sourceTree = ""; }; + 3ADBA84D2CE209C000B0050B /* Performance */ = { + isa = PBXGroup; + children = ( + 3ADBA84E2CE209FB00B0050B /* MetricManager.swift */, + ); + path = Performance; + sourceTree = ""; + }; 3AE0D5962BAB09AB00D6D925 /* Developer */ = { isa = PBXGroup; children = ( @@ -2849,6 +2860,7 @@ 086A5F362744ED9600FECE02 /* RepoView.swift in Sources */, 084417752B99B9060049297D /* HomeView.swift in Sources */, 3AF2A2E72BE239A300F3BE1B /* ArchivedInfoListView.swift in Sources */, + 3ADBA84F2CE209FB00B0050B /* MetricManager.swift in Sources */, 086A5F072744E88E00FECE02 /* SwiftPamphletAppApp.swift in Sources */, 08D8F00E2BF044FB00AA0020 /* WWDCDetailView.swift in Sources */, 08069CAD2BDE7A6B00D48E24 /* GuideDetailView.swift in Sources */, diff --git a/SwiftPamphletApp/App/SwiftPamphletAppApp.swift b/SwiftPamphletApp/App/SwiftPamphletAppApp.swift index e4b38054..c60b2a2e 100644 --- a/SwiftPamphletApp/App/SwiftPamphletAppApp.swift +++ b/SwiftPamphletApp/App/SwiftPamphletAppApp.swift @@ -12,10 +12,13 @@ import InfoOrganizer import SMFile import SMGitHub + @main struct SwiftPamphletAppApp: App { #if os(macOS) @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate + #elseif os(iOS) + @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate #endif init() { let gr = GitHubReq.shared @@ -57,7 +60,6 @@ class AppDelegate: NSObject, NSApplicationDelegate { func applicationDidFinishLaunching(_ notification: Notification) { print("-- AppDelegate Section --") - // ็”Ÿๆˆ Markdown ๆ–‡ไปถ // AutoTask.buildContentMarkdownFile() @@ -68,4 +70,12 @@ class AppDelegate: NSObject, NSApplicationDelegate { } } +#elseif os(iOS) +class AppDelegate: NSObject, UIApplicationDelegate { + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { + _ = MetricKitManager.shared + print("didFinishLaunchingWithOptions") + return true + } +} #endif diff --git a/SwiftPamphletApp/Performance/MetricManager.swift b/SwiftPamphletApp/Performance/MetricManager.swift new file mode 100644 index 00000000..f3d05602 --- /dev/null +++ b/SwiftPamphletApp/Performance/MetricManager.swift @@ -0,0 +1,38 @@ +// +// MetricManager.swift +// SwiftPamphletApp +// +// Created by Ming on 2024/11/11. +// + +import MetricKit + +@objc class MetricKitManager: NSObject, MXMetricManagerSubscriber { + @MainActor @objc public static let shared = MetricKitManager() + + private override init() { + super.init() + start() + } + + func start() { + let metricManager = MXMetricManager.shared + metricManager.add(self) + } +} + +extension MetricKitManager { + #if os(iOS) + @available(iOS 13.0, *) + func didReceive(_ payloads: [MXMetricPayload]) { + guard let firstPayload = payloads.first else { return } + print("Launch Time Data: \(firstPayload.dictionaryRepresentation())") + } + #endif + + @available(iOS 14.0, *) + func didReceive(_ payloads: [MXDiagnosticPayload]) { + guard let firstPayload = payloads.first else { return } + print("Diagnostic Data: \(firstPayload.dictionaryRepresentation())") + } +}