Make Your iPhone App Personalized with Custom Messages

 Bomb Dodge game over screen with a custom message and stats.

Bomb Dodge game over screen with a custom message and stats.

We want people to enjoy using the app. Instead of displaying the same message over and over again you can make it customized. Create a set of expressions or messages that you want the user to share on social networks or when they brag about an achievement in your game, or how helpful the app was in saving time or reducing complexity. 

I use these custom messages in Bomb Dodge (free on the App Store). Download it and try pressing the Twitter or Facebook buttons after you explode. You can also see custom messages that we display after each game to give the death screen a bit more character.

Code

At the top of your ViewController you'll want to connect a UILabel and a UIButton to your code. This connection can be made quickly with the Assistant Editor and the right-click and drag method. After you do that you'll need an array to store values from the text file. Use NSArray instead of NSMutableArray because you won't need to change it after you load it from disk.

#import "ViewController.h"

@interface ViewController () {
    NSArray *_socialMessages;
}
@property (weak, nonatomic) IBOutlet UILabel *messageLabel;
@property (weak, nonatomic) IBOutlet UIButton *nextButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _socialMessages = [[NSMutableArray alloc] init];
}

The code is straight forward, you'll just need a text file with messages separated by a return (newline). NSString has a convenient method to load a text file into a string using it's file path. You can separate each message and create an array using the componentsSeparatedByString: method using the newline character @"\n".

- (void)loadSocialMessages {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"socialMessages.txt" ofType:nil];
    NSError *error = nil;
    NSString *fileContents = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
    if(error) {
        NSLog(@"Unable to load social messages: %@", [error localizedDescription]);
    }
    _socialMessages = [fileContents componentsSeparatedByString:@"\n"];
}

Now that you can load messages it would help to have a text file. Go to Xcode > File > New > File. Select Other on iOS > Empty. 

 Choose iOS > Other > Empty to create a .txt file.

Choose iOS > Other > Empty to create a .txt file.

After you create it you'll want to save it as a .txt file type.

 Save the file as a .txt file so that we can load it into our iPhone app.

Save the file as a .txt file so that we can load it into our iPhone app.

Add some funny messages or serious messages that might be useful for a user.

Are you ready to explode? 
Bet you can't beat my score!
Bomb Dodge is awesome, you can smash bombs!
Bomb Dodge is so much FUN!
Bombee is so cute! He died, so I'll have to try better next time.
Boom goes the dynamite!
BOOM! My fuse ran out in Bomb Dodge ... can you can do better?
Can you outlast me in Bomb Dodge?
Click Like to save lives!
Do the exact opposite of what a bomb is supposed to do in Bomb Dodge!
Don't explode, stay alive!!!
Don't let me explode in Bomb Dodge!
Don't mix fire and explosives.
Download Bomb Dodge and don't explode!
Endless runner meets bomb defense game. Don't blow up!

In your viewDidLoad: method you should add the method call to loadSocialMessages below where you create the array.

_socialMessages = [[NSMutableArray alloc] init];
[self loadSocialMessages];

To get a message you'll use the arc4random_uniform(10) function that returns a number, for example between 0-9. You can use the array.count to get the total number of messages and then choose one. After you implement that method you can connect our nextButton to pull a random message and display it in your text field.

- (NSString *)randomSocialMessage {
    NSString *message = nil;
    if(_socialMessages.count > 0) {
        int messageIndex = arc4random_uniform([_socialMessages count]);
        message = _socialMessages[messageIndex];
    }
    return message;
}

- (IBAction)nextButtonPressed:(id)sender {

    self.messageLabel.text = [self randomSocialMessage];
}

Last but not least, you should make sure that when you run the app that it starts with a message. Grab a random message in the viewDidLoad: method and set it on the label.

- (void)viewDidLoad {
    [super viewDidLoad];

    _socialMessages = [[NSMutableArray alloc] init];
    [self loadSocialMessages];
    self.messageLabel.text = [self randomSocialMessage];
}

Checkout some screens where you can use the message on a share sheet for Facebook or Twitter. The Social.Framework makes it super easy to setup. 

 Twitter Share Sheet on iOS

Twitter Share Sheet on iOS

 Facebook Share Sheet on iOS - Look it's a different message every time!

Facebook Share Sheet on iOS - Look it's a different message every time!

Connect

  1. Download the sample Xcode project and make your apps more friendly or spunky!
  2. Email me a screenshot of how you use it in your app at
  3. Subscribe to my iPhone mailing list.