How to set different AppDelegate for different targets within same project?

十年热恋 提交于 2019-12-10 12:19:57

问题


I'm very very new to the target concept in Xcode. I have followed this tutorial to learn to create two targets in the same project. I just want to know how to make target A use AppDelegateA.swift as its designated app delegate, and target B use AppDelegateB.swift as its designated app delegate. Because on the tutorial, it actually teaches how to make two apps from the same AppDelegate. But I make two (almost) completely different apps, that share a lot of resources and libraries.

And while we're on the subject, can I also have target A use a storyboard called Main, and target B also use a storyboard called Main, but they are actually a different storyboard (but put together inside the same project)?


回答1:


Yes you can create 2 different based upon the target make following changes:

in the projects

main.m

you could do something like

int main(int argc, char *argv[])
{
    @autoreleasepool {

        NSString *appDelegateName;
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
            appDelegateName =  NSStringFromClass([AppDelegateIPhone class]);
        } else {
            appDelegateName =  NSStringFromClass([AppDelegateIPad class]);
        }
        return UIApplicationMain(argc, argv, nil, appDelegateName);
    }
}

But IMO you should not do it.

Instead doi it as apple does it to, in app delegate load different view controllers or different XIBs.

    @implementation AppDelegate

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
        // Override point for customization after application launch.
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
            self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];
        } else {
            self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil] autorelease];
        }
        self.window.rootViewController = self.viewController;
        [self.window makeKeyAndVisible];
        return YES;
    }

@end



回答2:


Hope my answer here helps for second part of including multiple AppDelegate files based on targets. https://stackoverflow.com/a/43227300/2715840

For first part of using multiple targets you can do so by simply doublicating the base target , you will get your plist already copied , change name based on new target or keep it same name plist.info but in different path. for separating code files(like Appdelegates) , storyboards and Assets , or firebase plist config files you can find my answer in the above like valid for these purposes.

Hope this helps.




回答3:


New answer to an old question that I was having myself. The answer is quite simple these days:

  1. Duplicate the main.m for each target
  2. Set the target membership for each main.m in the side panel on the right.
  3. Create the AppDelegate class for each target. They can have the same or different names and set the target membership for each AppDelegate appropriately.
  4. In each of the mains.m call the AppDelegate class that it needs to use. It can even have different names.


来源:https://stackoverflow.com/questions/40253702/how-to-set-different-appdelegate-for-different-targets-within-same-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!