Monday, December 28, 2009

Cookie Management in iPhone

The following code may help people who are looking for some sort of cookie management for their iPhone web application. This post will definitely get you started.

NSURLCredential *userCredentials = [NSURLCredential credentialWithUser:email.text
password:password.text
persistence:NSURLCredentialPersistencePermanent];

NSURLProtectionSpace *space = [[NSURLProtectionSpace alloc] initWithHost:@"localhost"
port:3000
protocol:@"http"
realm:nil
authenticationMethod: NSURLAuthenticationMethodDefault];


[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:userCredentials
forProtectionSpace:space];


After writing the above code, your application will be able to store cookies for the site http://localhost:3000/. You may also change it as per your need and requirement, such as:

* May use three different types of persistence storage for cookies: NSURLCredentialPersistenceNone,
NSURLCredentialPersistenceForSession,
NSURLCredentialPersistencePermanent

* May use different authentications. AuthenticationMethod should be set to one of the values in NSString *NSURLProtectionSpaceHTTPProxy;
NSString *NSURLProtectionSpaceHTTPSProxy;
NSString *NSURLProtectionSpaceFTPProxy;
NSString *NSURLProtectionSpaceSOCKSProxy;

or nil to use the default, NSURLAuthenticationMethodDefault.

For more information, refer to Apple's documentation.

Tuesday, December 15, 2009

Sound Playback in iPhone

Today I am going to post two ways to play sounds in iPhone.

1st Approach: Using Audio Toolbox
NSString *sound = [NSString stringWithFormat:@"sound"];
NSString *path = [[NSBundle mainBundle] pathForResource:sound ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound(soundID);


Of course, for this to work you need to add the AudioToolbox foundation to your project. Also it doesn't like the compressed audio of mp3's, etc. It is more suited for wav's.

2nd Approach: Using AVAudioPlayer class
The AVAudioPlayer class makes it very easy to play mp3s as well.
-(IBAction)playSound:(id)sender{

NSString *newAudioFile = [[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"mp3"];

audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile] error:NULL];

[audioPlayer setDelegate:self];
[audioPlayer prepareToPlay];
BOOL plays = [audioPlayer play];

}


For this to work, you need to add the AVFoundation to your project:
Project -> Add ->
Developer:Platforms:iPhoneSimulator.platform:Developer:SDKs:iPhoneSiumulator2.2.sdk:System:Library:Frameworks:AVFoundation.framework

Make sure to NOT copy it into your project when you are given the dialog box.

AVAudioPlayer provides a really straightforward interface, but it might not be appropriate for playing really short sounds that are synchronized with your app. AudioServices is really intended to play short sounds, and it works very well for that.

Adding the following line to your code, before you start the player, should cause the audioPlayer to loop indefinitely, until you issue a stop message:
[audioPlayer setNumberOfLoops:-1];

after the setDelegate function call.

Note that, with MP3, you may run into issues if you need to play multiple sounds at the same time:

Coding How-Tos: Audio and Video
The following limitations pertain for simultaneous sounds in iPhone OS, depending on the audio data format:

* AAC, MP3, and ALAC (Apple Lossless) audio: no simultaneous playback; one sound at a time.

Monday, November 16, 2009

Password Protect a Folder on the Mac

I was looking for a way to password protect a folder on the Mac. After a lot of research I found out a way of creating a disk image to protect our stuff in really a simple manner. I would like to mention its procedure in steps:

*First open up the “Disk Utility” application.
*Go to File -> New -> Disk Image from Folder
*Chose a folder to protect
*The next, most important part, is the encryption. You can pick either 128, or 256 bit encryption, it really doesn’t matter. I would recommend 128 bit due to speed although if you want to be extra safe pick the second option. Make sure the Image Format at the bottom is set to read/write, so you can write files into the disk image.

Once as you click ok, the engine will start working and it will create your disk image.

Enter your desired new password twice (Do not forget it.)


Once as you click ok, the image will mount and you have a standard image. You can then add in the files you want.This process creates an ordinary Macintosh disk image (.dmg) file. The disk image contains the entire contents of the folder, but cannot be opened unless the correct password is supplied. To open it, just double-click the .dmg file in Finder. A password dialog box will appear. Once you supply your correct password, Finder will automatically unencrypt your data and mount the image as a disk.

To be extra secure you may have to delete the Keychain password as it stores it by default. To do this open up Keychain Access, find your file in the list and delete it. You will then have to enter the image password if you want to access your files.

If you ever need to increase the size of you image because you have filled it up it is pretty simple. Make sure you have opened and closed Disk Utility. Click on the disk image you have created and then on Resize Image in the tool bar. Once as the box pops open click on the blue down arrow. Click on Resize Image, this will then enable you to increase the size of the image all the way up to the maximum free space on your disk. Once as you have done this you are done.

Hopefully, if you have followed the instructions OK, you will be able to password protect your files, and as a result of folder. Until Apple release a way of doing this in Finder, or the Get Info pane.

Thursday, November 5, 2009

iPhone: Shake to Update

Many applications perform some basic functions(like updating the UI, playing a sound etc.) when user shakes the iphone. Following snippet shows how to add this functionality in your app:

//In the .h file add the like this:
@interface TableViewController : UIViewController
//Add these two variables in the .h
BOOL shakeDetected;
UIAcceleration* lastAcceleration;
//Create a property
@property (retain) UIAcceleration* lastAcceleration;

//In the .m synthesize your property
@synthesize lastAcceleration;


//In the .m file, in your viewdidload add this line
[UIAccelerometer sharedAccelerometer].delegate = self;

//Add this method to the .m file
//Shake Detection Part 1
static BOOL IsDeviceShaking(UIAcceleration* last, UIAcceleration* current, double threshold) {
double deltaX = fabs(last.x - current.x);
double deltaY = fabs(last.y - current.y);
double deltaZ = fabs(last.z - current.z);
return (deltaX > threshold && deltaY > threshold) ||
(deltaX > threshold && deltaZ > threshold) ||
(deltaY > threshold && deltaZ > threshold);
}

//And add this method to the .m file also
//Shake Detection Part 2
- (void) accelerometer: (UIAccelerometer *)accelerometer didAccelerate: (UIAcceleration *)acceleration {
if (self.lastAcceleration) {
if (!shakeDetected && IsDeviceShaking(self.lastAcceleration, acceleration, 0.7)) {
shakeDetected = YES;
//This is where you put what you want to happen when it shakes
//The line below reloads the table
[tableView reload];
}
else if (shakeDetected && !IsDeviceShaking(self.lastAcceleration, acceleration, 0.2)) {
shakeDetected = NO;
}
}
self.lastAcceleration = acceleration;
}

Wednesday, October 7, 2009

Backup of mysql database

I usually need to backup my (mysql) database for transferring it from one pc to another or to transfer it to my partner working on the same project or to take a back up of the production database of my website. We could do it simply using a single command from the command prompt:

mysqldump [options] db_name [tables]

You could use this command as:
mysqldump -u root project_production > backup.sql
or
mysqldump project_production > backup.sql
(depending on the permissions)

This will generate a file backup.sql in your current directory which could be used to regenerate the database any time in future on the same or different pc.

Importing the data from the backup file is much more simpler. Just type on the command prompt:
mysql -u root project_development < backup.sql

This will copy all the data from the backup file to another database named project_development.

For more information, visit this site.