All posts by Jay Versluis

About Jay Versluis

Jay is the founder of Pinkstone Pictures and WP Hosting. He has been developing iOS Apps since 2011, several of which have made it to the App Store. In his spare time he likes to develop WordPress plugins and drawing on tablet devices. He blogs about his coding journey here and at

How to keep a UITextView scrolled to the bottom

UITextViews are ideal for displaying large amounts of scrollable text on a small screen, or a portion of the screen. They’re also great for continuously adding text to them, which naturally appears at the bottom. However, doing so does not scroll the text upwards so that the user can see the latest addition by default.

To accomplish this, many sources suggest to add use the contentOffset property. While this works if another component (like a keyboard) may come into vision, it does not help to steady keep all the text in our text view at the bottom at all times. However, another method from the UITextView class can help us there: scrollRangeToVisible.

To use it, we need to define an NSRange struct and define the portion of the text content that we’d like to display. We don’t need to find out what the height of our text view is for this, it is indeed enough to simply specify the last line of our text content. The following code nippet will do that:

- (void)positionTextView {
    // scroll to the bottom of the content
    NSRange lastLine = NSMakeRange(self.textView.text.length - 1, 1);
    [self.textView scrollRangeToVisible:lastLine];

Call this whenever text is added to the text view, or changes to the text view arise from constraint changes, and your last line of text will always be scrolled to the bottom.

How to determine the height of the keyboard in iOS

Keyboards in iOS can have various sizes. Not only can a keyboard be presented in portrait or landscape, on various devices with a plethora of screen sizes, and users may have QuickType enabled or disabled. Too many variables for hard coding.

Lucky for us, we can check all kinds of properties about a keyboard by dissecting the dictionary that is passed with the notifications mentioned in my previous post. If you recall, we can check when a keyboard appears or disappears using an observer. Let’s take a look at that notification when the keyboard is presented, and we can extract the height like this:

- (CGFloat)grabKeyboardHeight:(NSNotification *)notification {
    // grab keyboard size
    NSDictionary *userInfo = notification.userInfo;
    NSValue *keyFrame = [userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardFrame = keyFrame.CGRectValue;
    CGFloat height = keyboardFrame.size.height;
    return height;

The keyboard’s width can be determined in much the same way. The dictionary holds many other interesting goodies, for example how long it takes iOS to animate it onto the screen:

- (NSTimeInterval)grabAnimationDuration:(NSNotification *)notification {
    // find out how long the keyboard animation lasts
    NSDictionary *userInfo = notification.userInfo;
    NSTimeInterval duration = [[userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue];
    return duration;

Apple has the full list if interesting objects for us to play with:

How to detect when the keyboard appears and disappears in iOS

To detect when the keyboard from a UITextField is being brought up (and goes away again), the UITextField Delegate Protocol won’t help us. Instead we need to listen to two notifications, namely UIKeyBoardWillShow and UIKeyboardWillHide.

We could setup the observers in viewDidLoad, like this for example:

- (void)viewDidLoad {
    [super viewDidLoad];
    // setup keyboard observers
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardCameUp:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWentAway:) name:UIKeyboardWillHideNotification object:nil];

These observers will call a method in our class (using @selector). Mine are called keyboardCameUp and keyboardWentAway:

- (void)keyboardCameUp:(NSNotification *)notification {
    NSLog(@"Keyboard came up!");

- (void)keyboardWentAway:(NSNotification *)notification {
    NSLog(@"Keyboard went away!");

The notification can be used to detect some properties of the keyboard, for example its height and width.

How to read keyboard input in C

We can use the scanf() function to get user input in C. Here’s a quick implementation of scanf():

#include <stdio.h>

char input[100];
char *output = "Thank you for your message!";

int main(int argc, const char * argv[]) {
    // ask user for some input
    puts("Tell me something nice:");
    // grab input from keyboard via scanf()
    // don't use gets() for this, apparently it's totally evil
    scanf("%s", input);
    // say goodbye
    printf("%s\n\n", output);
    return 0;

scanf() will wait for the user to press Enter before giving its returned value back to our app. We must define a variable to hold its output. We can even define multiple variables, each of which will be populated with whatever is being typed in until Enter is pressed. Consider this:

 char input1[100], input2[100], input3[100];
 scanf("%s, %s, %s", input1, input2, input3);

Here we wait for three string items to be added (numbers entered will be interpreted as strings). To grab a numeric value from the keyboard, we cam use %d like so (d as in decimal, variable defined as int):

 int number;
 scanf("%d", &number);

Note the ampersand in front of our variable, without which scanf would populate a pointer. We can also mix and match keyboard input like this:

 char string[100];
 int number;
 scanf("%s, %d", string, &number);

Demo Project

You’ll find a quick demo project on GitHub, which also serves as a template on how to setup Xcode to open Terminal and allow for keyboard input when the project is run.

How to define a struct in C

Structures (or structs) in C are something like “mini-objects”: they are wrappers around multiple variables, much like the properties of an object. Since plan C doesn’t have objects, these are as close as it gets to them, and they can be quite helpful. Apple uses them extensively (think of a CGRect for example), and it’s easy to build our own.

Here’s how we can do that.

Creating a struct

We can define a struct like this:

    // define a struct
    struct cube {
        int length;
        int width;
        int height;

Here we create a struct with the name of cube and define three variables inside it. They don’t have to be of the same type, you can mix and match int, float, char and all the rest of the merry gang. The variables of a struct are called members.

When defined, a struct can be seen as a template. To use it, we need to initialise a new instance of the struct and populate the member variables, like so:

    // initialilze our struct
    struct cube massiveCube;

    // and populate it with values
    massiveCube.length = 5;
    massiveCube.width = 5;
    massiveCube.height = 7;

Our instance of the struct is called massiveCube, and we can refer to its member variables by dot notation.

The whole setup can be done in one step too, like this:

    // or define and init
    struct oval {
        int width;
        int height;
    } bigOval;
    bigOval.height = 5;
    bigOval.width = 7;

Note here that we still need to populate the variables. Further structs from the same template can be created as shown above.

Typedef-ing a struct

If we don’t want to create new struct instances with “struct cube myCube”, we can write a type definition for a struct.

typedef struct cube MegaCube;

I’ve typedef’d previous cube structure as MegaCube. Of course we can choose any name we like here, something descriptive is always a good idea. Again, our friends at Apple to this quite frequently. Now we can create new instances simply by referring to them without the struct word and only use our own definition:

MegaCube rubiksCube;
rubiksCube.length = rubiksCube.height = rubiksCube.width = 3;

This makes our code more readable and is a way of making C code “our own”.

How to submit your apps to the App Store with Xcode 8

In case it’s been a while that you had to deal with App Store submissions, here’s a quick refresher. This assumes that iTunes Connect is ready to receive a new app or update.

Make sure that the version number and build values have both been increased if your submission is an update. In Xcode, do the following:

  • verify that the Archive Scheme is set to Release (under Product – Scheme – Edit Scheme)
  • make sure that you’ve selected “Generic iOS Device” as the active scheme
  • head over to Product – Archive to create an archive of your current version
  • the Organizer window opens, showing previously archived versions of your app (don’t delete those)
  • now hit Validate to see if there are any issues with your app
  • if all is well, hit the big blue Upload to App Store button
  • now pray that your app is approved