Translate

Thursday, March 29, 2012

Upload an image and text from iPhone to Server

NSArray *formFields = [NSArray arrayWithObjects:@"stuid", @"parent_id", @"stu_name", @"gender", @"email", @"grade", @"dob", nil];
    NSArray *formValues = [NSArray arrayWithObjects:(id)childID, (id)parID, (id)childName, (id)childGender, (id)childMailID,(id)childGrade,(id)childDOB,nil];
    NSDictionary *textParams = [NSDictionary dictionaryWithObjects:formValues forKeys:formFields];
       
    NSArray *imageParams = [NSArray arrayWithObjects:childImage, nil];
   
    NSString *urlString = @"http://mysite.com/myscript.php";
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
   
    NSMutableData *body = [NSMutableData data];
   
    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];
   
    // add the image form fields
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: attachment; name=\"image%d\"; filename=\"%@\"\r\n", 0, [imageParams objectAtIndex:0]] dataUsingEncoding:NSUTF8StringEncoding]];
   
        [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithData:childImage]];
        [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
           
    // add the text form fields
    for (id key in textParams) {
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:[textParams objectForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    }
   
    // close the form
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
   
    // set request body
    [request setHTTPBody:body];
   
    // send the request (submit the form) and get the response
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
   
    NSLog(@"%@", returnString);

Wednesday, March 28, 2012

Create ipa file

If u have apple developer certificate and corresponding provisional profile means it is easy to create .ipa file in XCode 4.2 with iOS 5 sdk.

 1) Select ios device in scheme.
 2) Archive your project.
 3) Select your archive in Organizer and share this.
 4) give url which u want to share and save your .ipa file to any location.
 5) .ipa and plist file will be create. upload these and make wireless distribution via html.

Monday, March 19, 2012

How to receive text data from server in iPhone.

In this application we will see how to fetch data from server and replace it to the view in iPhone. This is very simple example. So let see how it will work.
Step 1: Open a Xcode, Create a View base application. Give the application name ”ReceiveData”.
Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.
Step 3: We need to add one UIViewController class in the project. So select the project -> New File -> Cocoa Touch -> ViewController subclass and give the class name “DisplayData”.
Step 4: In the ReceiveDataViewController.h file, we need to define DisplayData class , create instance of  UIButton class and define one IBAction: method. So make the following changes:
#import <UIKit/UIKit.h>
@class DisplayData;
@interface ReceiveDataViewController : UIViewController {
UIButton *button;
DisplayData *displayData;
}
@property(nonatomic,retain) IBOutlet UIButton *button;
-(IBAction)DisplayData:(id)sender;
@end
Step 5: Double click the ReceiveDataViewController.xib file and open it to the Interface Builder. First drag the button from the library and place it to the view window. Select the UIButton and bring up Connection Inspector, connect Touch Up Inside to File’s Owner icon and select DisplayData: method. Now save the Interface Builder and go back to the Xcode.
Step 6: Open the ReceiveDataViewController.m file and make the following changes:
#import "ReceiveDataViewController.h"
#import "DisplayData.h"
@implementation ReceiveDataViewController
@synthesize button;
-(IBAction)DisplayData:(id)sender
{
displayData = [[DisplayData alloc]
initWithNibName:@"DisplayData"
bundle:nil];
[self.view addSubview:displayData.view];
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark – View lifecycle
- (void)viewDidUnload
{
[super viewDidUnload];
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
Step 7: Now open DisplayData.h file and make the following changes:
#import <UIKit/UIKit.h>
@interface DisplayData : UIViewController {
NSMutableData *webData;
UIScrollView *titleScrollView;
UITextView *textView;
UIActivityIndicatorView* loadIndicator;
}
@property(nonatomic, retain) NSMutableData *webData;
@property(nonatomic,retain) IBOutlet    UIScrollView *titleScrollView;
@property (nonatomic, retain) UIActivityIndicatorView *loadIndicator;
- (void)ActivityIndigator;
@end
Step 8: Double click the DisplayData.xib file and open it to the Interface Builder, first drag the scrollview from the library and place it to the view window and connect File’s Owner icon to the view window and select  titleScrollView. Now save the .xib file and go back to the Xcode.
Step 9: Open the DisplayData.m file and make the following changes:
#import "DisplayData.h"
@implementation DisplayData
@synthesize webData,titleScrollView,loadIndicator;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn’t have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren’t in use.
}
#pragma mark – View lifecycle
- (void)ActivityIndigator
{
loadIndicator =
[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(130, 200, 40, 40)];
loadIndicator.activityIndicatorViewStyle =UIActivityIndicatorViewStyleWhiteLarge;
[loadIndicator startAnimating];
[self.view addSubview:loadIndicator];
[loadIndicator release];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self ActivityIndigator];
NSURL *url = [NSURLURLWithString:@"http://www.chakrainteractive.com/mob//ReceiveData/Data.txt"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSURLConnection *theConnection = [[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
}
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response
{
[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message : @"An error has occured.Please verify your internet connection."
delegate:nil
cancelButtonTitle :@"OK"
otherButtonTitles :nil];
[alert show];
[alert release];
[loadIndicator removeFromSuperview ];
[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"DONE. Received Bytes: %d", [webData length]);
NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes]length:[webData length] encoding:NSUTF8StringEncoding];
textView = [[UITextView alloc] initWithFrame:CGRectMake(5, 30, 320,400)];//size.height-30 )];
textView.text = loginStatus;
[textView setFont:[UIFont systemFontOfSize:14]];
[textView setBackgroundColor:[UIColor clearColor]];
[textView setTextColor:[UIColor blackColor]];
textView.editable = NO;
textView.dataDetectorTypes = UIDataDetectorTypeNone;
[loadIndicator removeFromSuperview ];
[titleScrollView addSubview:textView];
[loginStatus release];
[textView release];
[connection release];
[webData release];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
Step 10: Now Compile and run the application in Simulator.