iOS Swift: Separate AppDelegate for XCTest

前端 未结 3 809
感动是毒
感动是毒 2021-02-19 04:27

Due to a couple issues, I want the XCTest target in a project to run a separate app delegate. Using ObjC, this was a relatively straightforward process: manipulate main.m<

3条回答
  •  不思量自难忘°
    2021-02-19 04:57

    This solution to this is as follows:

    1. Duplicate your existing application's Target and rename it to something appropriate. In your case maybe 'TestingHarness' or some such. Note that you'll also want to change the bundle identifier and rename the corresponding Info.plist file. Renaming the Info.plist file means you'll need to change the Info.plist filename setting in your new target's Build Settings tab to match the new name.

    2. Create another *AppDelegate.swift file. In your case I'd call it TestAppDelegate.swift.

    3. Copy over your existing AppDelegate.swift file's contents into TestAppDelegate.swift and edit as desired. Make sure to leave the @UIApplicationMain annotation and implement the needed UIApplicationDelegate callbacks.

    4. Change the target membership of each of your *AppDelegate.swift files so that AppDelegate.swift is not included in your new 'TestHarness' target and TestAppDelegate.swift is not included in your main app's target. (You edit a file's Target Membership by selecting it in the File Browser and opening the File Inspector which you can access in the right-sidebar by default, or by choosing it in the menu under View -> Utilities.)

    5. Now you have two separate targets with separate App Delegates that you can build and run independently. The final step is to select your new 'TestHarness' target as the Host Application for your test targets. (Click the top-level project entry in the File Browser, then click your desired test Target in the sub-listing. On the General tab you'll see Host Application as the only available drop down.)

    Note: these instructions are for Xcode 7.2.

提交回复
热议问题