This is a basic technique for using AJAX to load some JSON data from your backend server. This example assumes you are going to use jQuery. Let’s use an example of a refresh button and some time based data. This means you have a UI that has a refresh button and you want to get the latest set of data from your backend. You might have a button on your UI similar to this:

...
<button id="data-refresh-btn" type="button">Refresh</button>
...

First thing you want to do is attach on onclick event and have a function that will contain your AJAX code. Similar to this in your Javascript file.

$('#data-refresh-btn').onclick = function () {

}

Now let’s add some AJAX. You’ll notice the URL that i’m using is passing the current date as a GET parameter to the page. I’m passing the date in millis because it will be easier to load that into a date object on the server side since pretty much any language has a constructor for that. There are ways to pass in a whole json payload as part of the request but that is beyond the scope of this example.

$('#data-refresh-btn').onclick = function () {
   $.ajax({
      url: 'http://<yourdomain>/datasource.php?date=' + new Date().getMilliseconds(),
      success: function(data) {
            //user your new data here.
      },
      type: 'GET'
    });
}

Since your data is going to be JSON formatted, the javascript engine can use it as if it were a regular object. You can access your data using dot notation.

Next up we need to have our data return properly from the server. I’m going to use PHP for this example but you can use anything that will return HTML to the browser.

<script type="application/json">
<?php
    $date = $_GET["date"];
    // get your new data and store it as a json string in $json
    echo $json;
?>
</script>

That’s the entire page. There’s no need to have a

<html>

or

<body>

tags. It’s very important that the object that you end up printing is a valid JSON string.

And you’re done!

This helper class can take any uiview and return to you a uiimage that is basicly a screenshot of that view. Throw this into a category and you’re good to go.

+ (UIImage*)viewAsImage:(UIView*)view {
	UIImage *image = nil;
	UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
	[view.layer renderInContext:UIGraphicsGetCurrentContext()];
	image = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
    
	return image;
}

Default address book request message
Default address book request message

If you want to show the user a custom message when asking for permission to use their contacts or address book, instead of the default one shown above, you need to add this field to your Info.plist file:

NSContactsUsageDescription

with the value being the text you want displayed. An example would be

“We would like to use your contacts to find out which of your friends are using Instagram!”

More info here: Apple Developer Documentation

Quick Guide to iOS 7 status bar customization.

  1. Ensure you set UIViewControllerBasedStatusBarAppearance to boolean YES in your plist.
  2. Ensure you set still set the default statusBar style in your plist, this will show during the splash screen.
  3. Override preferredStatusBarStyle providing your View Controller is not inside a UINavigationController.
  4. If your View Controller is inside a UINavigationController then preferredStatusBarStyle seems to be ignored completely. Instead the UINavigationController’s navigationBar’s barStyle is used. A dark barStyle makes the statusBar white, the default barStyle makes the statusBar text black.

Here’s a little script I wrote that lets you do search and replace with confirmation in bash. It uses find so it can look recursively through your files and uses vim to do the actual searching and replacing.

#!/bin/bash
#Usage ./viSearchReplace <original> <new> <filename or directory>
if [ $# -ne 3 ]; then
        echo "Usage:    ./viSearchReplace <original> <new> <filename or directory>"
        exit 1
fi
for i in `find $3 ! -type d ! -name . ! -name .. ! -name $0`; do
    vim -c '%s/'$1'/'$2'/gc' -c 'wq' $i
done
exit 0

GitHub Link

I commited some code to this awesome github repository: Colours-for-iOS
It returns [UIColor black] or [UIColor white] based on which one contrasts best with your current UIColor object.

Just throw the UIColor+Colours.h/m into your project and include the .h in any file in which you use UIColor. To use my new method, simply create a UIColor object and call it’s blackOrWhiteContrastingColor method. It returns a black or white UIColor object which you can then for example, use to change font/text color which is displayed over the original color object.

Check out my second app in the iOS App Store!

Colors: Live!

This is an amazing utility app specially designed for iOS, and capable of recognizing over 1000 colors! Colors: Live! allows you to sample any color visible through the camera on your device…live, and in real time. With over 1000 built-in color names (and more added with each update), you can instantly find the name of any color. But what if the color you are attempting to identify is not built-in? Colors: Live! will happily display the closest possible match! This app is perfect for artists, interior decorators, graphics and web designers, scrapbookers, and anyone who loves color.

Sometimes, if you don’t want to deal with autolayout or it simply isn’t performing in the way you want, the solution to your problem may be to use a separate storyboard for each device. To do that, add this code to your AppDelegate.m and make sure you have the separate storyboard files that you need.

Call this method in your application:didFinishLaunchingWithOptions: with this command:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self initializeStoryBoardBasedOnScreenSize];

AppDelegate.m

-(void)initializeStoryBoardBasedOnScreenSize {
    UIStoryboard *storyboard = nil;
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
    {    // The iOS device = iPhone or iPod Touch
        CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
        if (iOSDeviceScreenSize.height == 480)
        {   // iPhone 3GS, 4, and 4S and iPod Touch 3rd and 4th generation: 3.5 inch screen (diagonally measured)
            NSLog(@"Loading iphone 4 storyboard");
            // Instantiate a new storyboard object using the storyboard file named MainStoryboard_iPhone
            storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
        }
        if (iOSDeviceScreenSize.height == 568)
        {   // iPhone 5 and iPod Touch 5th generation: 4 inch screen (diagonally measured)
            NSLog(@"Loading iphone 5 storyboard");
            // Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone4
           storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone5" bundle:nil];
        }
        
    } else if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad){
        // The iOS device = iPad
        NSLog(@"Loading ipad storyboard");
        // Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone35
        storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil];
    }
    // Instantiate the initial view controller object from the storyboard
    UIViewController *initialViewController = [storyboard instantiateInitialViewController];
    
    // Instantiate a UIWindow object and initialize it with the screen size of the iOS device
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    // Set the initial view controller to be the root view controller of the window object
    self.window.rootViewController  = initialViewController;
    
    // Set the window object to be the key window and show it
    [self.window makeKeyAndVisible];
}

Parallax effect
Parallax effect

In iOS 7, Apple introduced a Parallax effect to the app screen. There is an API available for you to add this effect to your apps. This code lets you add a parallax effect to your apps in iOS 7. I haven’t tested this myself, but it looks like it should work.

Let me know how it goes!

UIInterpolatingMotionEffect *interpolationHorizontal = [[UIInterpolatingMotionEffect alloc]initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
interpolationHorizontal.minimumRelativeValue = @-10.0;
interpolationHorizontal.maximumRelativeValue = @10.0;

UIInterpolatingMotionEffect *interpolationVertical = [[UIInterpolatingMotionEffect alloc]initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
interpolationVertical.minimumRelativeValue = @-10.0;
interpolationVertical.maximumRelativeValue = @10.0;

[self.background addMotionEffect:interpolationHorizontal];
[self.background addMotionEffect:interpolationVertical];