在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Swift实现一句代码不用写加载首次启动引导图# kImageNamsArray:引导页的图片数组 实现原理1.给Appdelegate增加扩展, 添加guaidWindow属性private struct AssociatedKeys { static var guideWindowKey = "guideWindowKey" } private var guaidWindow: UIWindow? { get { return objc_getAssociatedObject(self, &AssociatedKeys.guideWindowKey) as? UIWindow } set { if newValue != nil { objc_setAssociatedObject(self, &AssociatedKeys.guideWindowKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) } } } 2.通过运行时进行方法的交换public override class func initialize(){ DispatchQueue.once(token: "交换appDelegate方法") { let lastVersion = UserDefaults.standard.float(forKey: "lastVersionKey") let currentVersion = Float((Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String) ?? "0")! if currentVersion > lastVersion { let originalMethod = class_getInstanceMethod(self, #selector(application(_:didFinishLaunchingWithOptions:))) let customMethod = class_getInstanceMethod(self, #selector(guide_application(_:didFinishLaunchingWithOptions:))) method_exchangeImplementations(originalMethod, customMethod) } } } func guide_application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { guaidWindow = UIWindow() guaidWindow!.frame = UIScreen.main.bounds guaidWindow!.backgroundColor = UIColor.white guaidWindow!.windowLevel = UIWindowLevelStatusBar + 1 self.guaidWindow!.makeKeyAndVisible() let vc = ZSGuaidViewController() vc.hide = {[weak self] in print("1111") let currentVersion = Float((Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String) ?? "0")! UserDefaults.standard.set(currentVersion, forKey: "lastVersionKey") UserDefaults.standard.synchronize() self?.guaidWindow?.resignKey() self?.guaidWindow?.isHidden = true self?.guaidWindow = nil } guaidWindow?.rootViewController = vc return guide_application(application, didFinishLaunchingWithOptions: launchOptions) } } Demo地址觉得有用给个��, Demo也实现了一句话加载广告页. |
请发表评论