Skip to content

Commit

Permalink
MetricKit 获取启动时间数据
Browse files Browse the repository at this point in the history
  • Loading branch information
ming1016 committed Nov 11, 2024
1 parent 50d955e commit 5b3042b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
12 changes: 12 additions & 0 deletions SwiftPamphletApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -958,6 +959,7 @@
3A30EFD52CDA818B0029CB2F /* WeatherKit(ap).md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = "WeatherKit(ap).md"; sourceTree = "<group>"; };
3A3168922CE0FD53004DFC5C /* HomeiOSView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeiOSView.swift; sourceTree = "<group>"; };
3A3168942CE0FFCE004DFC5C /* Launch Screen.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = "Launch Screen.storyboard"; sourceTree = "<group>"; };
3ADBA84E2CE209FB00B0050B /* MetricManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MetricManager.swift; sourceTree = "<group>"; };
3AE0D5992BAB0A0600D6D925 /* DeveloperListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeveloperListView.swift; sourceTree = "<group>"; };
3AE0D59D2BAB183100D6D925 /* EditDeveloper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditDeveloper.swift; sourceTree = "<group>"; };
3AF2A2DE2BE22A8C00F3BE1B /* UnCategoryInfoListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnCategoryInfoListView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1327,6 +1329,7 @@
086A5F052744E88E00FECE02 /* SwiftPamphletApp */ = {
isa = PBXGroup;
children = (
3ADBA84D2CE209C000B0050B /* Performance */,
086D48A02BBB820100835544 /* Info.plist */,
0846234D2BAC59AF003373D9 /* App */,
084417732B99B8EA0049297D /* HomeUI */,
Expand Down Expand Up @@ -2261,6 +2264,14 @@
path = Category;
sourceTree = "<group>";
};
3ADBA84D2CE209C000B0050B /* Performance */ = {
isa = PBXGroup;
children = (
3ADBA84E2CE209FB00B0050B /* MetricManager.swift */,
);
path = Performance;
sourceTree = "<group>";
};
3AE0D5962BAB09AB00D6D925 /* Developer */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -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 */,
Expand Down
12 changes: 11 additions & 1 deletion SwiftPamphletApp/App/SwiftPamphletAppApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -57,7 +60,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {

func applicationDidFinishLaunching(_ notification: Notification) {
print("-- AppDelegate Section --")

// 生成 Markdown 文件
// AutoTask.buildContentMarkdownFile()

Expand All @@ -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
38 changes: 38 additions & 0 deletions SwiftPamphletApp/Performance/MetricManager.swift
Original file line number Diff line number Diff line change
@@ -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())")
}
}

0 comments on commit 5b3042b

Please sign in to comment.