Swift 3 How to manage different delegates for different Targets

1.3k Views Asked by At

We have created two Targets (Target_One & Target_Two) for same project.

  • Target_One contain SDK1 and delegate Target1SDKHelperDelegate
  • Target_Two contain SDK2 and delegate Target2SDKHelperDelegate

The reason to create two target : we need to upload two apps with same UI but different SDK integration.

As we know, each SDK has their own delegates. So we want to apply delegates specific to the target.

Example: Target_One has a class named MyClass

class MyClass: NSObject, Target1SDKHelperDelegate {

}

In above class, we have implemented Target1SDKHelperDelegate delegate. We are using same class for Target_Two also and we want to use Target2SDKHelperDelegate for Target_Two.

So how we can put two different delegates to for two different targets?

We also know to manage target we should use below code.

#if Target_One

#else

#endif

But anyone tell us how to manage delegate by using above?

We want to do something like :

 class MyClass: NSObject
       #if Target_One
            , Target1SDKHelperDelegate 
      #else
            , Target2SDKHelperDelegate 
      #endif
   {

    }
2

There are 2 best solutions below

0
On

Can try like this code with use typealias:

#if FREE_VERSION
public typealias DELEGATES = UIViewController & AttributeUDBClientMainDelegate & AttributeSubscriptionHelperDelegate
#else
public typealias DELEGATES = UIViewController
#endif

public final class SettingsViewController: DELEGATES {
0
On

Actually it is pretty simple and direct . Lately I faced the issue cause of size of iPA went too big like 20MB . I disabled not needed features from some targets and had to manage the shared files like Appdelegate when it has references to disabled features files . Solved this by simply duplicating Appdelegate file and put it in a specific paths related to specific targets . Then included each appDelegate file under its target. It worked . The idea is the same like if you have firebase push notifications pLists configuration files for multiple targets OR imageAssets folders. Hope this helps.