Sunday, February 12, 2017

How to use targets to launch multiple different apps using the same code base
Here are the steps:

1- Duplicate current target
Hint:
  • Rename the new target to whatever you want, also add a scheme for it or you may need to just rename it
  • If you want to rename the new TargetName copy-info.plist file to whatever you want, you have to change the name in the Build settings in that target. This can be done by selecting the target from targets then build settings then under packaging set the new name of the plist file under "Info.plist file" field.

























2- Make sure you added the files of the project to the new target

Hint: be ware not to add the info.plist files to the targets or you will get this warning
"Warning: The Copy Bundle Resources build phase contains this target's Info.plist file '...-Info.plist'."
































3- Add user defined attributes to the project under build settings

Hint: you add it to the project so they are added to all the targets automatically



4- Set the values of the attributes
Hint: the attributes are Strings even if you put them as NO, Yes



5- Add user Settings property to the info.plist of each target with the keys corresponding to the attributes you added in the target's user defined attributes

It is a dictionary containing the keys you shall call from inside your code mapping to the user defined attributes Ex:
ButtonBackgroundColor : $(USER_BUTTON_BACKGROUND_COLOR)

<key>UserSettings</key>
<dict>
<key>ButtonBackgroundColor</key>
<string>$(USER_BUTTON_BACKGROUND_COLOR)</string>
</dict>

Note: you need to have the same dictionary in all the xxx-info.plist files. And by default when you duplicate a target that dictionary is present in the new copy of the newTarget-info.plist.




6- Create a class that will get the values of the properties from the info.plist
Hint: this class needs to be added to all targets

Class example: Swift 3

class TargetsOptions {
    
    static let shared = TargetsOptions()
    
    let infoMap = Bundle.main.infoDictionary?["UserSettings"]
    
    func buttonBackgroundColor() -> String {
        if let infoMap = infoMap as? Dictionary<String, Any> , let optionValue = infoMap["ButtonBackgroundColor"] as? String {
            return optionValue
        } else {
            return "#ffffff"
        }
    }
    
}

7- Add assets directory for each target if needed and set it in the target configs
Hint:
A) you may want to make an assets directory shared between all targets to reduce space in your repo and also to prevent duplication for easier maintenance later.
B) You can have one single Assets directory shared between multiple targets to keep the shared images between multiple targets, so you don't have duplicates.






8- Constraints in Xib files or storyboards

You may need to add constraints to a view for a target and others for other target and connect them to a collection outlet and add code to enable/disable those constraints on user defined attributes

Example code: Swift 3

func activateSegmentedControlConstraints() {
        if self.showSegmentedControlUp {
            for constraint in segmentedDownConstraints {
                constraint.isActive = false
            }
            for constraint in segmentedUpConstraints {
                constraint.isActive = true
            }
        } else {
            for constraint in segmentedDownConstraints {
                constraint.isActive = true
            }
            for constraint in segmentedUpConstraints {
                constraint.isActive = false
            }
        }
        self.segmentedControls.layoutIfNeeded()
 }




9- Add target to Podfile if you use cocoapods so it setups the settings of the pods for that target
Ex:

target 'MultipleTargets' do
    source 'https://github.com/CocoaPods/Specs.git'
    use_frameworks!
    
    # Pods for MultipleTargets
    pod 'SwiftyJSON'
    pod '...', '~> 1.2'
    ...
end

target 'MultipleTargets-Green' do
    source 'https://github.com/CocoaPods/Specs.git'
    use_frameworks!
    
    # Pods for MultipleTargets-Green
    pod 'SwiftyJSON'
    pod '...'
end
c- SOF -how-do-i-build-multiple-versions-of-the-same-ios-application-for-oems

No comments:

Post a Comment