Wednesday, June 8, 2011

A first hand look at building an Android application

Adding a UIButton Programatically

You might know how to add a button via IB – here is a code sample of how to do that using code

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
[btn setTitle:@"SKIP" forState:UIControlStateNormal];
btn.frame = CGRectMake(0, 100, 320, 50);
[self.view addSubview:btn];

and if you want to add a background image


UIImage *someImage = [UIImage imageNamed:@"splashImage.png"];
[btn setBackgroundImage:someImage forState:UIControlStateNormal];

Difference between 2 UIDatePicker’s in hours

UIDatePicker *date1;
UIDatePicker *date2;

The function to show difference between these two date pickers


NSDate *date1Val = date1.date;
NSDate *date2Val = date2.date;
NSTimeInterval interval = [date2Val timeIntervalSinceDate:date1Val];
int hours = (int)interval / 3600;
int minutes = (interval - (hours*3600)) / 60;
NSString *timeDiff = [NSString stringWithFormat:@"%d:%d", hours, minutes];

Hacking into PhoneGap iPhone/iPad Apps

Initially a lot of web-developers started with PhoneGap which lets you make webpages and embed them inside an iPhone app.

Now similarly its very easy to get access to those web pages. Let me show you how.
Steps:
Get hold of an phonegap iPhone App. I have one, which I had created a long time back.
- So go to – http://www.phonegap.com/apps
- Select iPhone or iPad
- Pick an app for which you want code (Let me pick my application)


- Get the link to the app and open with iTunes

Download the app. It will save the app in your Apps Folder

Right Click on the app and select “Show in Finder”
You will see the .ipa file.
TicTacToe 1.1.ipa
Rename it to
TicTacToe 1.1.zip
Unzip the file and you will find something like this

Right Click on TicTacToe and select “Show Package Contents” – There will be mostly a folder in it “www” which will have the entire HTML, JS and CSS files or whatever the person used.

iPhone/iPad AirPrinting Tutorial in 3 steps

Step 1:

Start Xcode and make a New Project – Select View-based Application – I call it AirPrinting.


Add an image to your project – I added demo.png – Here is the image

Step 2:

Go to AirPrintingViewController.h and add the following code

#import <UIKit/UIKit.h>
 
@interface AirPrintingViewController : UIViewController <UIPrintInteractionControllerDelegate>{
 
}
 
-(void)printItem;
 
@end

Next in AirPrintingViewController.m add the following code

#import "AirPrintingViewController.h"
 
@implementation AirPrintingViewController
 
-(void)printItem {
 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"png"];
    NSData *dataFromPath = [NSData dataWithContentsOfFile:path];
 
    UIPrintInteractionController *printController = [UIPrintInteractionController sharedPrintController];
 
    if(printController && [UIPrintInteractionController canPrintData:dataFromPath]) {
 
        printController.delegate = self;
 
        UIPrintInfo *printInfo = [UIPrintInfo printInfo];
        printInfo.outputType = UIPrintInfoOutputGeneral;
        printInfo.jobName = [path lastPathComponent];
        printInfo.duplex = UIPrintInfoDuplexLongEdge;
        printController.printInfo = printInfo;
        printController.showsPageRange = YES;
        printController.printingItem = dataFromPath;
 
        void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
            if (!completed && error) {
                NSLog(@"FAILED! due to error in domain %@ with error code %u", error.domain, error.code);
            }
        };
 
        [printController presentAnimated:YES completionHandler:completionHandler];
 
    }
}
 
- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn addTarget:self action:@selector(printItem) forControlEvents:UIControlEventTouchDown];
    [btn setTitle:@"PRINT" forState:UIControlStateNormal];
    btn.frame = CGRectMake(0, 100, 320, 50);
    [self.view addSubview:btn];
}
 
@end

Step 3:

Build and Run – You will see a print button – Click the print Button and you will see UIPrintInteractionController popup – Go ahead and Print.

Here is the entire Code

 

Thursday, June 2, 2011

Mac Terminal Error : Could not determine audit condition

When I launched my terminal today, was welcomed with this error :-

login: PAM Error (line 396): System error
login: Could not determine audit condition

[Process completed]

It is most probably because I was playing with my /usr/bin permissions the other day.
The fix is easy. Just delete the "/usr/bin/login" dir.

But how do I delete it, if I can't access the "Terminal" altogether ?
Come on - You can access any folder using the "Finder".

1. Open "Finder"
2. Open "Go To Folder"
3. Type "/usr/bin/login"
4. Delete it.

Monday, May 30, 2011

How To Stop iTunes Automatically Syncing Your iPhone, iPod or iPad

iTunes Auto Sync Slowing You Down?

If you’re wondering how you can stop iTunes from automatically kicking off the sync + backup + update process when you plug your iOS device in then look no further.

By holding down the Shift + Control keys (Windows) or Command + Option keys (Mac) and then plugging your iPad, iPhone or iPod into your USB port you will tell iTunes not to sync!

If you happen to miss it and sync kicks off anyway, either slide the unlock slider on your device or hit the eject symbol in iTunes and your device will gracefully stop syncing and disconnect.

This was tested using both an iPad 2 and an iPhone 3GS on iTunes 10.2 (Mac).