Created
January 4, 2019 09:21
-
-
Save hisoka0917/00491389553f2b2ad5d0589e595e1bd1 to your computer and use it in GitHub Desktop.
Self awake code when iOS app launch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
protocol SelfAware: class { | |
static func awake() | |
} | |
class ModuleManagerBase { | |
static func registerModules() { | |
//let startTime = NSDate.timeIntervalSinceReferenceDate | |
let typeCount = Int(objc_getClassList(nil, 0)) | |
let types = UnsafeMutablePointer<AnyClass>.allocate(capacity: typeCount) | |
let autoreleasingTypes = AutoreleasingUnsafeMutablePointer<AnyClass>(types) | |
objc_getClassList(autoreleasingTypes, Int32(typeCount)) | |
for index in 0 ..< typeCount { | |
(types[index] as? SelfAware.Type)?.awake() | |
} | |
types.deallocate() | |
//let endTime = NSDate.timeIntervalSinceReferenceDate | |
//print("Register modules spend \(endTime - startTime)s") | |
} | |
} | |
extension UIApplication { | |
private static let runOnce: Void = { | |
ModuleManagerBase.registerModules() | |
}() | |
override open var next: UIResponder? { | |
// Called before applicationDidFinishLaunching | |
UIApplication.runOnce | |
return super.next | |
} | |
} | |
class needsAutoAwakeClass: SelfAware { | |
static func awake() { | |
// Do something here that will automatic called when app launched | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment