4

Simplifying Facebook iOS SDK

-

The Problem with Facebook iOS SDK

Facebook+Singleton
Now that the Facebook iOS SDK has been changed to use Graph API, many people are trying to figure out just how to use it properly. In my opinion, however, there are problems with the SDK’s architecture that make it likely to be used poorly. For example, Facebook’s documentationrecommends instantiating the Facebook class and authenticating from within your AppDelegate. This is suboptimal for a several of reasons.

First, in many apps that allow Facebook integration, the user may never actually use the Facebook features. Thus, loading the class and authenticating the user is something that should only be done when the user has decided to use a Facebook feature. In other words, we should be lazy loading the Facebook class but their documentation suggests otherwise.

Secondly, the log in method chosen by Facebook (fast-switching the user to the Facebook app or Safari to log in) makes a very poor first impression for your app. Who wants their app to look like it crashed then loaded some Web page the very first time it’s launched?

(more...)

0

iOS Code: MKiCloudSync – Sync your NSUserDefaults to iCloud with a single line of code

-

Just wrote this class, MKiCloudSync (100 lines of code) that auto syncs your NSUserDefaults to iCloud.

How to use?

All you need to do is to enable iCloud key value store entitlements, copy the files and forget about the rest.

Step 1:

Enable iCloud entitlements for your app. This is easily done in Xcode 4.2.1 by opening your target settings and checking “Enable Entitlements” from the summary tab.

This is illustrated below.

Xcode

Enabling iCloud Entitlements in Xcode 4.2.1
Tip:
If you are sharing settings with other apps, ensure that your iCloud Key-Value Store is the same across all those apps.
Step 2:
Drag the two files

  • MKiCloudSync.h
  • MKiCloudSync.m

into your project. You can find these files in the Source Code section of this post.
Step 3:
In your applicationDidFinishLaunchingWithOptions: method,
start the sync by calling,

  1. [MKiCloudSync start];
[MKiCloudSync start];

You also have to #include the header file.
This is probably the only line of code you have to write!
Step 4:
Sleep… Kidding.

There is no step 4. Continue using NSUserDefaults to save your settings. The MKiCloudSync class automatically syncs your settings to iCloud and restores them back to your NSUserDefaults when they are changed on other devices.

To top them all, it also posts a notification,

  1. kMKiCloudSyncNotification
kMKiCloudSyncNotification

to let you know that a sync has been performed. You can listen to this notification and update your user interface from NSUserDefaults.

Advantages

  • Upgrading your existing iOS 4 apps to sync settings to iCloud is now super easy.
  • With this class, you no longer have to add calls to NSUbiquitousKeyValueStore throughout your app. You can continue your existing method of saving them to NSUserDefaults. Everything is automatic. If your settings might change often, you can observe the notification kMKiCloudSyncNotification and update your user interface.

Source Code

Licensing

Royalty free for commercial or non-commercial use, though attribution will please me to write more such code :)

iOS Code: MKiCloudSync – Sync your NSUserDefaults to iCloud with a single line of code | MKBlog.

1

UIDeviceHardware Helper

-

feel free to use this class

Usage

  1. <code>UIDeviceHardware *h=[[UIDeviceHardware alloc] init];
  2. [self setDeviceModel:[h platformString]];  
  3. [h release];
  4. </code>
<code>UIDeviceHardware *h=[[UIDeviceHardware alloc] init];
[self setDeviceModel:[h platformString]];   
[h release];
</code>

UIDeviceHardware.h

  1. <code>//
  2. //  UIDeviceHardware.h
  3. //
  4. //  Used to determine EXACT version of device software is running on.
  5.  
  6. #import <Foundation/Foundation.h>
  7.  
  8. @interface UIDeviceHardware : NSObject
  9.  
  10. - (NSString *) platform;
  11. - (NSString *) platformString;
  12.  
  13. @end
  14. </code>
<code>//
//  UIDeviceHardware.h
//
//  Used to determine EXACT version of device software is running on.

#import <Foundation/Foundation.h>

@interface UIDeviceHardware : NSObject 

- (NSString *) platform;
- (NSString *) platformString;

@end
</code>

UIDeviceHardware.m

  1. <code>//
  2. //  UIDeviceHardware.m
  3. //
  4. //  Used to determine EXACT version of device software is running on.
  5.  
  6. #import "UIDeviceHardware.h"
  7. #include <sys/types.h>
  8. #include <sys/sysctl.h>
  9.  
  10. @implementation UIDeviceHardware
  11.  
  12. - (NSString *) platform{
  13.     size_t size;
  14.     sysctlbyname("hw.machine", NULL, &size, NULL, 0);
  15.     char *machine = malloc(size);
  16.     sysctlbyname("hw.machine", machine, &size, NULL, 0);
  17.     NSString *platform = [NSString stringWithCString:machine];
  18.     free(machine);
  19.     return platform;
  20. }
  21.  
  22. - (NSString *) platformString{
  23.     NSString *platform = [self platform];
  24.     if ([platform isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";
  25.     if ([platform isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
  26.     if ([platform isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
  27.     if ([platform isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
  28.     if ([platform isEqualToString:@"iPhone3,3"])    return @"Verizon iPhone 4";
  29.     if ([platform isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";
  30.     if ([platform isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";
  31.     if ([platform isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";
  32.     if ([platform isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";
  33.     if ([platform isEqualToString:@"iPad1,1"])      return @"iPad";
  34.     if ([platform isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
  35.     if ([platform isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";
  36.     if ([platform isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
  37.     if ([platform isEqualToString:@"i386"])         return @"Simulator";
  38.     return platform;
  39. }
  40.  
  41. @end
  42. </code>
<code>//
//  UIDeviceHardware.m
//
//  Used to determine EXACT version of device software is running on.

#import "UIDeviceHardware.h"
#include <sys/types.h>
#include <sys/sysctl.h>

@implementation UIDeviceHardware

- (NSString *) platform{
    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *machine = malloc(size);
    sysctlbyname("hw.machine", machine, &size, NULL, 0);
    NSString *platform = [NSString stringWithCString:machine];
    free(machine);
    return platform;
}

- (NSString *) platformString{
    NSString *platform = [self platform];
    if ([platform isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";
    if ([platform isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
    if ([platform isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
    if ([platform isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone3,3"])    return @"Verizon iPhone 4";
    if ([platform isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";
    if ([platform isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";
    if ([platform isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";
    if ([platform isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";
    if ([platform isEqualToString:@"iPad1,1"])      return @"iPad";
    if ([platform isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
    if ([platform isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";
    if ([platform isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
    if ([platform isEqualToString:@"i386"])         return @"Simulator";
    return platform;
}

@end
</code>

UPDATE (01/14/11)

Obviously, this code is a bit out of date by now, but it can certainly be updated using the code on this thread provided by Brian Robbins which includes similar code with updated models. Thanks for the support on this thread.