InjectionIII工具macOS 开发应用

638 阅读1分钟

下载InjectionIII

地址: apps.apple.com/app/injecti…

选择文件目录

下载完成后,打开应用。 点击Open Project然后选择你项目所在的目录【⚠注意⚠️】是工程文件所在的文件夹

image.png

image.png

工程配置

开启下图的选项

image.png

新建Run Script

image.png

复制脚本到箭头指向的地方

export CODESIGN_ALLOCATE\=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/codesign_allocate

INJECTION_APP_RESOURCES=/Applications/InjectionIII.app/Contents/Resources

/usr/bin/codesign --force --sign $EXPANDED_CODE_SIGN_IDENTITY  $INJECTION_APP_RESOURCES/maciOSInjection.bundle/Contents/MacOS/maciOSInjection

/usr/bin/codesign --force --sign $EXPANDED_CODE_SIGN_IDENTITY  $INJECTION_APP_RESOURCES/maciOSSwiftUISupport.bundle/Contents/MacOS/maciOSSwiftUISupport

/usr/bin/codesign --force --sign $EXPANDED_CODE_SIGN_IDENTITY $INJECTION_APP_RESOURCES/maciOSInjection.bundle/Frameworks/SwiftTrace.framework/SwiftTrace

defaults write com.johnholdsworth.InjectionIII "$PROJECT_FILE_PATH" $EXPANDED_CODE_SIGN_IDENTITY

image.png

代码使用

AppDelegate.swift

func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application
#if DEBUG
        do{
            let injectionBundle = Bundle.init(path: "/Applications/InjectionIII.app/Contents/Resources/macOSInjection.bundle")

            if let bundle = injectionBundle {
                try bundle.loadAndReturnError()
            }else{
                debugPrint("Injection注入失败,未能检测到Injection")
            }
        }catch{
            debugPrint("Injection注入失败\(error)")
        }
#endif

}

BaseViewController.swift

class UUBaseViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.

#if DEBUG
        NotificationCenter.default.addObserver(self,
            selector: #selector(configureView),
            name: Notification.Name("INJECTION_BUNDLE_NOTIFICATION"), object: nil)
#endif
    }

    @objc func configureView() {
        self.viewDidLoad()
    }

}

然后其他的控制器继承BaseViewController控制器即可,这样在控制器的viewDidLoad里面写的代码在cmd + s保存后,就会自动渲染。