WatchUserAgent.swift 1.68 KB
//
//  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
    }
    
}