Adding Adsense to your blog

This is probably trivial to everyone around, but i’d like to share it anyways.

  1. Signup for an account here: adsense.google.com
  2. Wait till you get the first ‘Site in Review’ mail.
  3. Once you got access to the Adsense main page, hit ‘My Ads’, and create a brand new ad.
  4. Copy the source code for the ad!.
  5. Install the required plugin…
    1. If you’re using WordPress, i suggest you try this plugin: http://wordpress.org/extend/plugins/quick-adsense/
    2. If you’re using Blogger, just hit the settings, it’s already integrated.
  6. Setup QuickAdsense (or Blogger). If you dare, you can always simply inject the ad-code yourself.

No matter what, please, remember the adsense guidelines. It’s pretty simple… don’t click your own ads, and.. don’t steal content!. That includes youtube music videos, and piracy.

Got it?

Setting OSX Firmware Password

OSX is a strong and secure system. Until you realize there is a feature called ‘single user login’… which virtually grants you ROOT access, provided that you have physical access to the target machine.

  1. Boot the system.
  2. Press CMD + S.
  3. You should get a bash shell, with ROOT permissions.

That sucks, pretty much. There is just no single password screen. If you have the machine, you can access its files. How do we prevent this????.

In Lion, and Mountain Lion…:

  1. Open Terminal and type:
    defaults write com.apple.DiskUtility DUDebugMenuEnabled 1
  2. Open DiskUtility and choose “Show every partition”, from the ‘Debug’ menu you have just unlocked.
  3. Mount the ‘Recovery HD’ hidden partition.
  4. In Terminal, type:
    open /Volumes/Recovery\ HD/com.apple.recovery.boot/BaseSystem.dmg
  5. In the BaseSystem DMG you’ve just mounted, locate: Applications/Utilities.
  6. Launch ‘Firmware Password Utility’, and simply follow the instructions.

That should, at the very least, enhance a little bit your security.

Using NSLocalizedString with a dictionary stored inside a bundle

We’ve seen before how to create an iOS Assets bundle. Today, we’ll try to localize a string, using a dictionary stored in a bundle. In order to do so… we’ll rely on the following helper method:

@interface LABundles
+ (NSBundle*)someResourcesLocalizationBundle
@end

@implementation LABundles
+ (NSBundle*)someResourcesLocalizationBundle
{
   static NSBundle* someResourcesLocalizationBundle = nil;

   if ( someResourcesLocalizationBundle == nil )
   {
       // This double check increases efficiency. Don't you get bored of asking the same thing twice?.
        @synchronized(self)
        {
            if ( someResourcesLocalizationBundle == nil )
            {
               // Figure out the current language.
               NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
                NSArray* languages = [defs objectForKey:@"AppleLanguages"];

                NSString* currentLanguage = [languages objectAtIndex:0];

               // Map the current language's folder within the bundle
                NSBundle* bundle = [NSBundle someResourcesBundle];
                NSString* path = [bundle pathForResource:currentLanguage ofType:@"lproj"];

               // Okay, let's go with the default bundle
                if ( path == nil )
                {
                    someResourcesLocalizationBundle = bundle;
                }
               // Bingo!
                else
                {
                    someResourcesLocalizationBundle = [NSBundle bundleWithPath:path];
                }

                [someResourcesLocalizationBundle retain];
            }
        }
   }

    return someResourcesLocalizationBundle;
}
@end

 
What’s that all about?. Well… it turns out that order to resolve localized strings, first of all we need to figure out what’s the device’s language.

After that, we need to load a NSBundle object mapped to the folder inside the bundle, which contains the localized strings file. And then we can begin talking about resolving a localized string.

The code pasted above has been tested, of course!. But how do you use it ????

Supposing that you need to localize the string ‘Testing1’, and that you intend to use a dictionary named ‘InfoPlist.strings’, you need to do the following:

[[NSBundle someResourcesLocalizationBundle] localizedStringForKey:@"Testing1" value:nil table:@"InfoPlist"]

Of course, you can customize that. If your dictionary file is named ‘Localized.strings’, you don’t even need to specify the ‘table’ parameter.

I hope you find this useful!

Loading NIBs stored inside a bundle

When does it make sense to store your NIBs inside a bundle?. As we’ve discussed before… this becomes handy when you’re distributing a framework which has some custom NIB files.

But wait…. how do you tell a UIViewController to load its nib file from a bundle?. Short answer… we’ll rely on UIViewController’s ‘initWithNibName: bundle:’ constructor.

Before doing that… we need to take care about a small detail, which is, how to load a NSBundle object (to pass on as the ‘bundle’ parameter).

We’ll show you the required code below, encapsulated into a nice helper class:

@interface LABundles
+ (NSBundle*)someResourcesBundle;
@end

@implementation LABundles
NSString* const kSomeResourcesBundleName = @"SomeResources.bundle";

+ (NSBundle*) someResourcesBundle
{
   static NSBundle* someResourcesBundle = nil;
   if ( someResourcesBundle == nil )
   {
           @synchronized(self)
           {
               if ( someResourcesBundle == nil )
               {
                   NSString* bundlePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:kSomeResourcesBundleName];
                   someResourcesBundle = [[NSBundle bundleWithPath:bundlePath] retain];
               }
           }
   }

    return someResourcesBundle;
}
@end

Finally, in order to alloc an UIViewController, and load its nib from the SomeResources bundle, we need to do the following:

    SomeViewController* someVC = [[SomeViewController alloc] initWithNibName:NSStringFromClass([SomeViewController class])
                                                                      bundle:[LABundles someResourcesBundle]];

That’s it!. SomeViewController should be loading its nib file from the someResources bundle.