WatchUserAgent.swift
1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//
// WatchUserAgent.swift
// hippo-watch Watch App
//
// Created by shihao on 2025/7/10.
//
import Foundation
import UIKit
import SwiftUI
#if canImport(WatchKit)
import WatchKit
#endif
class WatchUserAgent {
static let share = WatchUserAgent()
var unifiedUA: String = ""
var finalUA: String = ""
private(set) var appVersion: String?
private(set) var buildVersion: String?
func genUA(){
#if canImport(WatchKit)
let systemVersion = WKInterfaceDevice.current().systemVersion
#else
let systemVersion = UIDevice.current.systemVersion
#endif
let formattedVersion = systemVersion.replacingOccurrences(of: ".", with: "_")
self.unifiedUA = "Mozilla/5.0 (Apple Watch; CPU Watch OS \(formattedVersion) like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148"
self.finalUA = self.unifiedUA + self.customUA()
}
func customUA() -> String {
let version = CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), kCFBundleVersionKey) as! NSString
let bundle = Bundle.main.infoDictionary?["CFBundleShortVersionString"] ?? ""
#if canImport(WatchKit)
let hardware = WKInterfaceDevice.current().model
let systemVersion = WKInterfaceDevice.current().systemVersion
#else
let hardware = UIDevice.current.model
let systemVersion = UIDevice.current.systemVersion
#endif
let height = ScreenSize.height
let width = ScreenSize.width
let ua = " doublefeel/\(version)(\(bundle))(\(hardware); iOS \(systemVersion); \(height)x\(width))"
appVersion = bundle as? String
buildVersion = version as String
return ua
}
}