Sunday, January 17, 2010

shaking detection in iPhone application

Few days ago, I saw an iPhone application, which as an wallpaper type application and the image was changed when iPhone shaked.

Then I try to learn how iPhone detects shaking.
In the following I describe it:

UIResponder class has two delegate method which is called when iPhone is shaked. The methods are:

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;

The first method Tells the receiver that a motion event has begun.
The second method Tells the receiver that a motion event has ended.

->Note:
1. iPhone OS informs the first responder only when a motion event starts and when it ends; for example, it
doesn’t report individual shakes.

2. The receiving
object must be the first responder to receive motion events .

-> How we can use this methods in our app:

The UIView which you want to respond to shake, you have to subclass this view of a UIView, and you have to override the 3 methods:
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{

NSLog(@" Shake Start");

/*
do what you want when shake start;
*/

}


- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{

NSLog(@" Shake End");

/*
do what you want when shake End;
*/

}

- (BOOL)canBecomeFirstResponder{ return YES; }

and in your view controller class, always respond that view as a first responder.


You can download my demo project on iPhone shaking from here

Thursday, January 14, 2010

How to upload an image to the server from an iPhone application

Many of my friends asked me how to upload an image to a server from an iPhone application.

The following method can be used to upload an image to a server.

This method take a parameter of type UIImage, which is the image to uploaded.


-(void)uploadImage:(UIImage*)imageToUploadK{

NSData *data=UIImageJPEGRepresentation(imageToUploadK, 0);

NSString *urlString=[[NSString alloc]initWithFormat:@"http://riseuplabs.com/applications/iWallpaper/uploadImage.php]


// setting up the request object now

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

/*
add some header info now
we always need a boundary when we post a file
also we need to set the content type

You might want to generate a random boundary.. this is just the same
as my output from wireshark on a valid html post
*/

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

/*
now lets create the body of the post
*/
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:data]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// setting the body of the post to the reqeust
[request setHTTPBody:body];

NSURLConnection *uploadConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];

}

Thursday, October 8, 2009

My first blog

This is my first post in the blog world.
My name is Md. Faisal Rahman
I got my BSc degree on CSE from BUET.
now I am doing a job in a company named riseUp, as a "iPhone application developer."