0

I'm developing a native iOS app for multiple customers, we are using multiple targets to each different customer. Almost all storyboards will be reused because the app is almost the same for each customer.

The problem is, we need customize the storyboards with the colors and images of each customer and we don't want to replicate the storyboards. So I'm searching for something like a CSS in HTML or like the Styles in Android to apply in iOS.

How can I do it?

Brian
  • 14,610
  • 7
  • 35
  • 43
Elton Santana
  • 950
  • 3
  • 11
  • 22

2 Answers2

1

I created a library that lets you style storyboards in a way similar to CSS, but using either Swift code or JSON stylesheets.

You can set a different stylesheet for each target in its info.plist without changing any other code.

Link to more info here.

Wyetro
  • 8,439
  • 9
  • 46
  • 64
Daniel Hall
  • 13,457
  • 4
  • 41
  • 37
0

There are any number of ways to accomplish this. Here is my idea.

Create an NSObject called Style. It will hold all the information you need changed. So if for example you need a different title for each of four view controllers, a different background color for each, and a different image in two of them, it could look like this:

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface Style : NSObject

@property (nonatomic, retain) NSString* first_title;               
@property (nonatomic, retain) NSString* second_title;
@property (nonatomic, retain) NSString* third_title;
@property (nonatomic, retain) NSString* fourth_title;
@property (nonatomic) UIColor* first_background_color;
@property (nonatomic) UIColor* second_background_color;
@property (nonatomic) UIColor* third_background_color;
@property (nonatomic) UIColor* fourth_background_color;
@property (nonatomic) UIImage* first_image;
@property (nonatomic) UIImage* second_image;

@end

Then, in your Style.m, you could have methods to save this information to memory, like this:

// encode a card to memory
- (void)encodeWithCoder:(NSCoder *)coder {
    [coder encodeObject:first_title forKey:@"first_title"];
    // etc etc etc
}

// decode a card from memory
- (id)initWithCoder:(NSCoder *)coder {
    self = [self init];
    self.first_title = [coder decodeObjectForKey:@"first_title"];
    // etc etc etc
    return self;
}

Next, you could (in your app delegate or basically wherever you want) create an NSArray* of Styles - one for each of the companies involved. I could think of any number of places to do this - you could do it in the app delegate in didFinishLaunchingWithOptions, or you could even store some TSV files on a server, fetch them in the viewDidLoad and parse them into this array. Whatever you want! Anyway, once you have some awesome array of Styles - one for each company - you can fetch and save it to and from app memory like this:

-(void)writeArrayWithCustomObjToUserDefaults:(NSString*)keyName withArray:(NSMutableArray*)myArray {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:myArray];
    [defaults setObject:data forKey:keyName];
    [defaults synchronize];
}

And to get the array back from memory:

if ([[NSUserDefaults standardUserDefaults] objectForKey:@"Styles"] != nil) {
        NSData* existingData = [[NSUserDefaults standardUserDefaults] objectForKey:@"knives"];
        NSArray* styles = [NSKeyedUnarchiver unarchiveObjectWithData:existingData];

}

Then, in your various VC's, just fetch the NSArray of Styles, find the one with a name corresponding to your target, and access its variables to modify the VC!

More information on getting target name here.

More information on that memory stuff here.

Community
  • 1
  • 1
Max von Hippel
  • 2,856
  • 3
  • 29
  • 46