AppDelegate.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // AppDelegate.swift
  3. // TestQmeraLite
  4. //
  5. // Created by Qindi on 29/11/21.
  6. //
  7. import UIKit
  8. import NexilisLite
  9. struct AppFontName {
  10. static let regular = "Poppins-Regular"
  11. static let bold = "Poppins-SemiBold"
  12. static let italic = "Poppins-MediumItalic"
  13. }
  14. extension UIFontDescriptor.AttributeName {
  15. static let nsctFontUIUsage = UIFontDescriptor.AttributeName(rawValue: "NSCTFontUIUsageAttribute")
  16. }
  17. extension UIFont {
  18. static var isOverrided: Bool = false
  19. static let FONT_SELECT = 0
  20. @objc class func mySystemFont(ofSize size: CGFloat) -> UIFont {
  21. return UIFont(name: AppFontName.regular, size: size)!
  22. }
  23. @objc class func myBoldSystemFont(ofSize size: CGFloat) -> UIFont {
  24. return UIFont(name: AppFontName.bold, size: size)!
  25. }
  26. @objc class func myItalicSystemFont(ofSize size: CGFloat) -> UIFont {
  27. return UIFont(name: AppFontName.italic, size: size)!
  28. }
  29. @objc convenience init(myCoder aDecoder: NSCoder) {
  30. guard
  31. let fontDescriptor = aDecoder.decodeObject(forKey: "UIFontDescriptor") as? UIFontDescriptor,
  32. let fontAttribute = fontDescriptor.fontAttributes[.nsctFontUIUsage] as? String else {
  33. self.init(myCoder: aDecoder)
  34. return
  35. }
  36. var fontName = ""
  37. switch fontAttribute {
  38. case "CTFontRegularUsage":
  39. fontName = AppFontName.regular
  40. case "CTFontEmphasizedUsage", "CTFontBoldUsage":
  41. fontName = AppFontName.bold
  42. case "CTFontObliqueUsage":
  43. fontName = AppFontName.italic
  44. default:
  45. fontName = AppFontName.regular
  46. }
  47. self.init(name: fontName, size: fontDescriptor.pointSize)!
  48. }
  49. class func overrideInitialize() {
  50. guard self == UIFont.self, !isOverrided, FONT_SELECT == 0 else { return }
  51. // Avoid method swizzling run twice and revert to original initialize function
  52. isOverrided = true
  53. if let systemFontMethod = class_getClassMethod(self, #selector(systemFont(ofSize:))),
  54. let mySystemFontMethod = class_getClassMethod(self, #selector(mySystemFont(ofSize:))) {
  55. method_exchangeImplementations(systemFontMethod, mySystemFontMethod)
  56. }
  57. if let boldSystemFontMethod = class_getClassMethod(self, #selector(boldSystemFont(ofSize:))),
  58. let myBoldSystemFontMethod = class_getClassMethod(self, #selector(myBoldSystemFont(ofSize:))) {
  59. method_exchangeImplementations(boldSystemFontMethod, myBoldSystemFontMethod)
  60. }
  61. if let italicSystemFontMethod = class_getClassMethod(self, #selector(italicSystemFont(ofSize:))),
  62. let myItalicSystemFontMethod = class_getClassMethod(self, #selector(myItalicSystemFont(ofSize:))) {
  63. method_exchangeImplementations(italicSystemFontMethod, myItalicSystemFontMethod)
  64. }
  65. if let initCoderMethod = class_getInstanceMethod(self, #selector(UIFontDescriptor.init(coder:))), // Trick to get over the lack of UIFont.init(coder:))
  66. let myInitCoderMethod = class_getInstanceMethod(self, #selector(UIFont.init(myCoder:))) {
  67. method_exchangeImplementations(initCoderMethod, myInitCoderMethod)
  68. }
  69. }
  70. }
  71. @main
  72. class AppDelegate: UIResponder, UIApplicationDelegate {
  73. override init() {
  74. super.init()
  75. UIFont.overrideInitialize()
  76. }
  77. func transparentNavbar(){
  78. // Sets background to a blank/empty image
  79. UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
  80. // Sets shadow (line below the bar) to a blank image
  81. UINavigationBar.appearance().shadowImage = UIImage()
  82. // Sets the translucent background color
  83. UINavigationBar.appearance().backgroundColor = .clear
  84. // Set translucent. (Default value is already true, so this can be removed if desired.)
  85. UINavigationBar.appearance().isTranslucent = true
  86. }
  87. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  88. // Override point for customization after application launch.
  89. // Nexilis.connect(apiKey: "09ED34439CFE435E5D528E73F2D3DC1B23307B56EAB28A4883700D47BB5ADE07", delegate: self)
  90. // Nexilis.connect(apiKey: "E6FF534BDA45A78619488E44AC90592327DDF33ACF6213A58DB55B78300BA8EE", delegate: self)
  91. Nexilis.connect(apiKey: "***REPLACE***WITH***YOUR***ACCOUNT***", delegate: self, showButton: false)
  92. if let response = Nexilis.writeSync(message: getPrefs(key: ""), timeout: 5000) {
  93. if response.mBodies[CoreMessage_TMessageKey.ERRCOD] == "00" {
  94. let data = response.getBody(key: CoreMessage_TMessageKey.DATA)
  95. if let json = try! JSONSerialization.jsonObject(with: data.data(using: String.Encoding.utf8)!, options: []) as? [[String: Any?]] {
  96. for j in json {
  97. if let firstTab = j["app_builder_url_first_tab"] as? String {
  98. print("first tab: \(firstTab)")
  99. PrefsUtil.setURLFirstTab(value: firstTab)
  100. ViewController.sURL = firstTab
  101. }
  102. if let thirdTab = j["app_builder_url_third_tab"] as? String {
  103. print("third tab: \(thirdTab)")
  104. PrefsUtil.setURLThirdTab(value: thirdTab)
  105. ViewController.tab3 = thirdTab
  106. }
  107. if let customTab = j["app_builder_custom_tab"] as? String {
  108. print("custom tab: \(customTab)")
  109. PrefsUtil.setCustomTab(cust: customTab)
  110. }
  111. }
  112. }
  113. }
  114. }
  115. return true
  116. }
  117. // MARK: UISceneSession Lifecycle
  118. func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
  119. // Called when a new scene session is being created.
  120. // Use this method to select a configuration to create the new scene with.
  121. return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
  122. }
  123. func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
  124. // Called when the user discards a scene session.
  125. // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
  126. // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
  127. }
  128. func getPrefs(key: String) -> TMessage {
  129. let tMessage = NexilisLite.TMessage()
  130. let me = UserDefaults.standard.string(forKey: "me")
  131. tMessage.mCode = "PPR"
  132. tMessage.mStatus = CoreMessage_TMessageUtil.getTID()
  133. tMessage.mBodies[CoreMessage_TMessageKey.F_PIN] = me
  134. tMessage.mBodies[CoreMessage_TMessageKey.KEY] = key
  135. return tMessage
  136. }
  137. }
  138. extension AppDelegate: ConnectDelegate {
  139. func onSuccess(userId: String) {
  140. print(#function, "userId: \(userId)")
  141. }
  142. func onFailed(error: String) {
  143. print(#function, "error: \(error)")
  144. }
  145. }