Monday 29 August 2011

MD5 Encryption for Secure Transfer of Password

This blog describes how to encrypt your data using MD5 encryption algorithm.

in .h file

- (NSString *) md5:(NSString *)str;

in your .m file import
#import <CommonCrypto/CommonDigest.h>

Write this method in your .m file:-


- (NSString *) md5:(NSString *)str
{
const char *cStr = [str UTF8String];
unsigned char result[16];
CC_MD5( cStr, strlen(cStr), result ); // This is the md5 call
return [NSString stringWithFormat:
@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
];
}

Now where ever you want to get an encrypted string do

NSString *md5String=[self md5:@"test"];
NSLog(@"md5String%@",md5String);

On console you will got md5String098f6bcd4621d373cade4e832627b4f6

Integration and Customization of Coverflow Library

This blog describe how to integrate cover flow effect in application using open source library .

First get the source code for CoverFlow from this link:-https://github.com/thefaj/OpenFlow

1.)Open your project and all the files in your project.
2.)Add QuartzCore and CoreGraphics Frameworks.
3.)In your View Controller.h file
import - #import "AFOpenFlowView.h"
@interface AFOpenFlowViewController : UIViewController <AFOpenFlowViewDataSource, AFOpenFlowViewDelegate>

In .m file

in viewDidLoad method do

-(void)viewDidLoad
{
NSString *imageName;
for (int i=0; i < 30; i++) {
imageName = [[NSString alloc] initWithFormat:@"%d.jpg", i];
[(AFOpenFlowView *)self.view setImage:[UIImage imageNamed:imageName] forIndex:i];
[imageName release];
}
[(AFOpenFlowView *)self.view setNumberOfImages:30];
}
add these methods:-

- (UIImage *)defaultImage {
return [UIImage imageNamed:@"transparentdefault.png"];
}

- (void)openFlowView:(AFOpenFlowView *)openFlowView selectionDidChange:(int)index {
NSLog(@"Cover Flow selection did change to %d", index);

}

Customization of Cover Flow Effect:-

In AFItemView.m file you will see this method:-

if (self = [super initWithFrame:frame]) {
self.opaque = YES;
self.backgroundColor = NULL;
verticalPosition = 0;
horizontalPosition = 0;

// Image View
imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.opaque = YES;
[self addSubview:imageView];
}
Now suppose you want a white frame outside your image so for that what you have to do is:-

Then write this:-

if (self = [super initWithFrame:frame]) {
self.opaque = YES;
self.backgroundColor = NULL;
verticalPosition = 0;
horizontalPosition = 0;

// Image View
imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.opaque = YES;
[self addSubview:imageView];

UIView* customView=[[UIView alloc]initWithFrame:CGRectMake(0, 220, 265, 20)];
customView.backgroundColor=[UIColor whiteColor];
customView.tag=22;
[self addSubview:customView];
[customView release];

UIView* customView2=[[UIView alloc]initWithFrame:CGRectMake(0, 20, 265, 7)];
customView2.backgroundColor=[UIColor whiteColor];
[self addSubview:customView2];
[customView2 release];

UIView* customView3=[[UIView alloc]initWithFrame:CGRectMake(0, 20, 8, 220)];
customView3.backgroundColor=[UIColor whiteColor];
[self addSubview:customView3];
[customView3 release];

UIView* customView4=[[UIView alloc]initWithFrame:CGRectMake(265, 20, 8, 220)];
customView4.backgroundColor=[UIColor whiteColor];
[self addSubview:customView4];
[customView4 release];
}

add this code in your this method

REMOVING IMAGE REFLECTION EFFECT:-

in OpenFlowView.m file

- (void)setImage:(UIImage *)image forIndex:(int)index {
// Create a reflection for this image.
//UIImage *imageWithReflection = [image addImageReflection:kReflectionFraction];
NSNumber *coverNumber = [NSNumber numberWithInt:index];
//[coverImages setObject:imageWithReflection forKey:coverNumber];
[coverImages setObject:image forKey:coverNumber];
[coverImageHeights setObject:[NSNumber numberWithFloat:image.size.height] forKey:coverNumber];

// If this cover is onscreen, set its image and call layoutCover.
AFItemView *aCover = (AFItemView *)[onscreenCovers objectForKey:[NSNumber numberWithInt:index]];
if (aCover) {
//[aCover setImage:imageWithReflection originalImageHeight:image.size.height reflectionFraction:kReflectionFraction];
[aCover setImage:image originalImageHeight:image.size.height reflectionFraction:kReflectionFraction];
[self layoutCover:aCover selectedCover:selectedCoverView.number animated:NO];
}
}

- (void)layoutCover:(AFItemView *)aCover selectedCover:(int)selectedIndex animated:(Boolean)animated
{

//newPosition.y = halfScreenHeight + aCover.verticalPosition; //comment this
newPosition.y = halfScreenHeight ;//add this
}

INCREASING/DECREASING IMAGE WIDTH:-

IN AFItemView.m file

- (void)setImage:(UIImage *)newImage originalImageHeight:(CGFloat)imageHeight reflectionFraction:(CGFloat)reflectionFraction {
[imageView setImage:newImage];
verticalPosition = imageHeight * reflectionFraction / 2;
originalImageHeight = imageHeight;
self.frame = CGRectMake(0, 20, newImage.size.width+40, newImage.size.height-20);//adjust coordinates yourself
}

Displaying multiple locations in MapView

This Blog describes how to show multiple locations in map view.

First you have to create an array in which multiple annotations will be saved.

In your ViewController.h file

NSMutableArray *addAnnotationsArray;
AddressAnnotation *addAnnotation;


In your ViewController.m file

addAnnotationsArray=[[NSMutableArray alloc]init];

Now initialize the for loop and and play it till how much locations you have to show on the map(In my case I have passed the count of my mapAddressArray which have the names of the locations ).
int k;
for(k=0;k<[self.mapAddressArray count];k++)
{
CLLocationCoordinate2D location;

I have called addressLocation method which returns the location coordinates of that location.


location=[self addressLocation:[self.mapAddressArray objectAtIndex:k]];

I have created seperate AddressAnnotation class and in this view controller created a object of that class and called initWithCoordinate method.

addAnnotation=[[AddressAnnotation alloc]initWithCoordinate:location];

[addAnnotation setMTitle:[self.companiesName objectAtIndex:k]];
[addAnnotation setMSubTitle:[self.mapAddressArray objectAtIndex:k]];

Add annotation to the addAnnotationsArray .

[self.addAnnotationsArray addObject:addAnnotation];

[addAnnotation release];
}

You have added the annotations in the array .Now you have to display these annotations on the map

int l;
for(l=0;l<[self.addAnnotationsArray count];l++)
{
[map addAnnotation:[self.mapAddAnnotations objectAtIndex:l]];
}

AddressLocation Method:-

-(CLLocationCoordinate2D) addressLocation:(NSString *)addrss {
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv",
[addrss stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
NSArray *listItems = [locationString componentsSeparatedByString:@","];

double latitude = 0.0;
double longitude = 0.0;

if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
}
else {
//Show error
}
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;

return location;
}

AddressAnnotation.h

#import<MapKit/MapKit.h>


@interface AddressAnnotation :NSObject<MKAnnotation> {

CLLocationCoordinate2D coordinate;
NSString *mTitle;
NSString *mSubTitle;

}

@property (nonatomic, retain) NSString *mSubTitle;
@property (nonatomic, retain) NSString *mTitle;


@end


AddressAnnotation.m


#import "AddressAnnotation.h"


@implementation AddressAnnotation

@synthesize coordinate,mTitle, mSubTitle,mapId;

- (NSString *)subtitle{
return mSubTitle;
}

- (NSString *)title{
return mTitle;
}


-(id)initWithCoordinate:(CLLocationCoordinate2D) c{
coordinate=c;
//NSLog(@"%f,%f",c.latitude,c.longitude);
return self;
}



@end

How to select videos from iPhone gallery in App

When you open UIImagePickerController class object you can select only images, video files are not displayed there for selection.
This Blog describes how to select video files stored in iphone library and then do whatever as per the requirements (like playing,uploading...)
First attach MediaPlayer Framework.

In your ViewController.h file import these after attaching mediaplayer framework

#import<MobileCoreServices/UTCoreTypes.h>
#import <MediaPlayer/MediaPlayer.h>


UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
imagePicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeMovie];
NSArray *sourceTypes = [UIImagePickerController availableMediaTypesForSourceType:imagePicker.sourceType];
if (![sourceTypes containsObject:(NSString *)kUTTypeMovie ])
{
NSLog(@"no video");
}

else
{
[self presentModalViewController:imagePicker animated:YES];
}
imagePicker release];

Getting the URL of that selected video


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
if ([type isEqualToString:(NSString *)kUTTypeVideo] || [type isEqualToString:(NSString *)kUTTypeMovie])
{
NSURL *urlvideo = [info objectForKey:UIImagePickerControllerMediaURL];
}
}

Getting images and uploading it on webServer


This Blog entry describes how to open photo gallery for selecting images and uploading them on server.

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;

If you want image to edit the image then write
imagePicker.allowsEditing = YES;

imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];

There are two methods of UIImagePickerController and this method is called when the image is selected.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
self.image=nil;
self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *imagedata=[NSData dataWithData:UIImagePNGRepresentation(self.image)];
NSString *base64string=[imagedata base64EncodedString];
NSString *str = [NSString stringWithFormat:testurl];
NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:base64string forKey:@"keyForUploadingImage"];
[request setRequestMethod:@"POST"];
[request setDelegate:self];
[request setDidFinishSelector:@selector(SuccessResponse:)];
[request setDidFailSelector:@selector(SuccessresponseFailed:)];
[networkQueue addOperation: request];
[networkQueue go];
[self dismissModalViewControllerAnimated:YES];
}

If u want to upload the edited image instead of original image then write:-
image = [info objectForKey:UIImagePickerControllerEditedImage];

Setting the selected image of phone in the imageView

[self.ImageView setImage:image];

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissModalViewControllerAnimated:YES];
}

Deletion of Data displaying in TableView


This blog describe an easy method to delete values showing in the list without adding some sort of buttons .

In your ViewController.h file

IBOutlet UITableView *countriesTableView;
UIBarButtonItem *donebutton;

-(void)editClicked;
-(void)doneClicked;


In your ViewController.m file

- (void)viewDidLoad {
donebutton = [[UIBarButtonItem alloc]initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(editClicked) ];
self.navigationItem.rightBarButtonItem = donebutton;
}

-(void)editClicked
{
self.countriesTableView.editing=YES;
donebutton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(doneClicked)] ;
self.navigationItem.rightBarButtonItem = donebutton;

}

-(void)doneClicked
{
self.countriesTableView.editing=NO;
donebutton = [[UIBarButtonItem alloc]initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(editClicked) ];
self.navigationItem.rightBarButtonItem = donebutton;

}

countriesArray is my array which holds the values that will be displayed (you can write array of your own requirement)

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


if([self.countriesArray count]>0)
{
return [self.countriesArray count];
}
else {
return 0;
}

}

- (UITableViewCell *)tableView:(UITableView *)tbleView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

cell.textLabel.text=[self.countriesArray objectAtIndex:indexPath.row];

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tbleView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

if (tbleView.editing == NO || !indexPath)
return UITableViewCellEditingStyleNone;
if (tbleView.editing) {
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleNone;
}



-(void)tableView:(UITableView *)tbleView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {
rowNo=indexPath.row;
[self.countriesArray removeObjectAtIndex:indexPath.row];
[countriesTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}


Happy Coding!!!

Rearrangement of Rows in Table View

Hi All,
This blog describes how to move table rows upwards and downwards.


First for integrating the below code you should have a table view and the delegate methods as cellforRowAtIndexPath etc.

For Rearrangement you have to copy below methods of table view

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES; 
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {

NSString *stringToMove = [[self.dataArray objectAtIndex:sourceIndexPath.row] retain]; 
[self.dataArray removeObjectAtIndex:sourceIndexPath.row];
[self.dataArray insertObject:stringToMove atIndex:destinationIndexPath.row];
[stringToMove release];
[tableView reloadData];

} 
dataArray is my Array which contain the values displayed in table view