swift中自定义UIApplication

原文:stackoverflow.com/questions/2…

1. 首先定义 UIApplication 的子类 SLApplication

import Foundation
import UIKit

class SLApplication: UIApplication {
    override func sendEvent(event: UIEvent) {
        super.sendEvent(event)
        print("send event")
    }
}
复制代码

2. 设置入口

新建 main.swift,

import Foundation
import UIKit

UIApplicationMain(Process.argc, Process.unsafeArgv, NSStringFromClass(SLApplication), NSStringFromClass(AppDelegate))
复制代码

3、去掉 Appdelegate 的 @UIApplicationMain

@UIApplicationMain

在类的最顶部声明 @UIApplicationMain,表示该类是 application 的 delegate。如果不用该属性标识,另外一种做法是在 main.swift 中调用 UIApplicationMain 函数,设置 delegate 和 application。以上我们就是根据这种方法来设置的。

Apple doc

Apply this attribute to a class to indicate that it is the application delegate. Using this attribute is equivalent to calling the UIApplicationMain function and passing this class’s name as the name of the delegate class.

If you do not use this attribute, supply a main.swift file with a main function that calls the UIApplicationMain(::🙂 function. For example, if your app uses a custom subclass of UIApplication
as its principal class, call the UIApplicationMain(
::🙂 function instead of using this attribute.

可以下载 demo:github.com/silan-liu/C…

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享