[HELP iPod] Hang after connection to the server

Discussion in 'iOS Development' started by 0r4ngB41k, Nov 9, 2009.

  1. 0r4ngB41k

    0r4ngB41k New Member

    Joined:
    Nov 9, 2009
    Messages:
    7
    Likes Received:
    0
    Device:
    iPod touch
    hi, I just joined this forum because I was interested to learn how to develop iPhone/iPod application. Sorry, if my bad english.

    I try to make an application, in which my application will connect to the server to log in and so on.

    After my application connected to the server and was able to login, but after login or finish received data from server, direct my application hangs / stack and out the message 'obj_sendMsg'.

    PHP:
    //
    //  m3classViewController.m
    //  m3class
    //
    //  Created by MacBook01 on 11/3/09.
    //  Copyright __MyCompanyName__ 2009. All rights reserved.
    //

    #import "m3cServer.h"
    #import "m3classViewController.h"
    #import "JSON/JSON.h"

    @implementation m3classViewController

    @synthesize username;
    @
    synthesize password;
    @
    synthesize btnLogin;
    @
    synthesize formCanvas;
    @
    synthesize server_url;

    /***************************************
     ** Waktu username dan password diisi, disable tombol login
     **************************************/
    -(IBAction)disableLoginButton:(id)sender
    {
        
    btnLogin.enabled NO;
    }

    /**************************************
     ** Setelah tombol login di tekan
     *************************************/
    -(IBAction)enableLoginButton:(id)sender
    {
        
    btnLogin.enabled YES;
    }

    /**************************************
     ** Setelah tombol login di tekan
     *************************************/
    -(IBAction)doLogin:(id)sender
    {
        
    NSString *usernameStr username.text;
        
    NSString *passwordStr password.text;
        
        if ([
    usernameStr length] == 0)
        {
            
    UIAlertView *alert = [[UIAlertView alloc]
                                  
    initWithTitle:@"Please enter your email"
                                  
    message:nil
                                  delegate
    :self
                                  cancelButtonTitle
    :@"Ok"
                                  
    otherButtonTitles:nil];
            [
    alert show];
            [
    alert release];
            
        }
        else if ([
    passwordStr length] == 0)
        {
            
    UIAlertView *alert = [[UIAlertView alloc]
                                  
    initWithTitle:@"Please enter your password"
                                  
    message:nil
                                  delegate
    :self
                                  cancelButtonTitle
    :@"Ok"
                                  
    otherButtonTitles:nil];
            [
    alert show];
            [
    alert release];
            
        }
        
        if ([
    usernameStr length] != && [passwordStr length] != 0)
        {
            
    NSString    *post        = [NSString stringWithFormat:@"username=%@&password=%@"username.textpassword.text];
            
    NSData        *postData    = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
            
    NSString    *postLength = [NSString stringWithFormat:@"%d", [postData length]];
            
    NSString    *url        = [NSString stringWithFormat:@"%@?c=login&m=checklogin"server_url];
            
            
    NSMutableURLRequest *request = [[[NSMutableURLRequest allocinitautorelease];
            
    // url now is http://localhost/myserver/index.php?c=login&m=checklogin
            
    [request setURL:[NSURL URLWithString:url]];
            [
    request setHTTPMethod:@"POST"];
            [
    request setValue:postLength forHTTPHeaderField:@"Content-Length"];
            [
    request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
            [
    request setHTTPBody:postData];
        
            
    NSURLConnection *conn=[[NSURLConnection allocinitWithRequest:request delegate:self];
            if (
    conn)
            {
                
    receivedData = [[NSMutableData dataretain];
            }
        }
        
        [
    usernameStr release];
        [
    passwordStr release];
    }

    /*************************************
     ** Fungsi untuk scroll view keatas waktu keyboard keluar
    *************************************/
    - (void)textFieldDidBeginEditing:(UITextField *)textField {    
        [
    UIView beginAnimations:nil context:NULL];
        [
    UIView setAnimationDelegate:self];
        [
    UIView setAnimationDuration:0.5];
        [
    UIView setAnimationBeginsFromCurrentState:YES];
        
    formCanvas.frame CGRectMake(formCanvas.frame.origin.x, (formCanvas.frame.origin.100.0), formCanvas.frame.size.widthformCanvas.frame.size.height);
        [
    UIView commitAnimations];
    }

    /*************************************
     ** Fungsi untuk scroll view kebawah/balik lagi ke posisi semula waktu keyboard di tutup
     *************************************/
    - (void)textFieldDidEndEditing:(UITextField *)textField {    
        [
    UIView beginAnimations:nil context:NULL];
        [
    UIView setAnimationDelegate:self];
        [
    UIView setAnimationDuration:0.5];
        [
    UIView setAnimationBeginsFromCurrentState:YES];
        
    formCanvas.frame CGRectMake(formCanvas.frame.origin.x, (formCanvas.frame.origin.100.0), formCanvas.frame.size.widthformCanvas.frame.size.height);
        [
    UIView commitAnimations];
    }

    - (BOOL)
    textFieldShouldReturn:(UITextField *)theTextField {
        [
    theTextField resignFirstResponder];
        
        return 
    YES;
    }

    - (
    void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
        [
    receivedData setLength:0];
    }

    - (
    void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        [
    receivedData appendData:data];
    }

    - (
    void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
        
    //label.text = [NSString stringWithFormat:@"Connection failed: %@", [error description]];
        
        
    UIAlertView *alert = [[UIAlertView alloc]
                              
    initWithTitle:@"Can't connect to server"
                              
    message:nil
                              delegate
    :self
                              cancelButtonTitle
    :@"Ok"
                              
    otherButtonTitles:nil];
        [
    alert show];
        [
    alert release];
        
        [
    connection release];
        [
    receivedData release];
    }

    - (
    void)connectionDidFinishLoading:(NSURLConnection *)connection {
        
    NSString *responseString = [[NSString allocinitWithData:receivedData encoding:NSUTF8StringEncoding];
        
        
    NSLog(responseString);
        
        
    NSString        *jsonString = [NSString stringWithString:responseString];
        
    NSDictionary    *dictionary = [jsonString JSONValue];
        
    NSString        *result = [dictionary objectForKey:@"success"];
        
        
    NSLog(@"%@"result);
        
        [
    responseString release];
        [
    receivedData release];
        [
    connection release];
        
        if ([
    result integerValue] == 1)        // if username and password valid
        
    {
            
            
    UIAlertView *alert = [[UIAlertView alloc]
                                  
    initWithTitle:@"Username or password valid"
                                  
    message:nil
                                  delegate
    :self
                                  cancelButtonTitle
    :@"Ok"
                                  
    otherButtonTitles:nil];
            [
    alert show];
            [
    alert release];
        }
        else
        {
            
    UIAlertView *alert = [[UIAlertView alloc]
                                  
    initWithTitle:@"Username or password invalid"
                                  
    message:nil
                                  delegate
    :self
                                  cancelButtonTitle
    :@"Ok"
                                  
    otherButtonTitles:nil];
            [
    alert show];
            [
    alert release];
        }
    }

    /*
    // The designated initializer. Override to perform setup that is required before the view is loaded.
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
        if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
            // Custom initialization
            
        }
        return self;
    }
    */

    /*
    // Implement loadView to create a view hierarchy programmatically, without using a nib.
    - (void)loadView {
    }
    */

    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad {
        [
    super viewDidLoad];
        
    username.delegate self;
        
    password.delegate self;
        
        
    m3cServer *_server = [[m3cServer allocinit];
        
    server_url _server.server_url;
        
        [
    _server release];
    }

    /*
    // Override to allow orientations other than the default portrait orientation.
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    */

    - (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.
    }

    - (
    void)viewDidUnload {
        
    // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }

    - (
    void)dealloc {
        [
    username release];
        [
    password release];
        [
    btnLogin release];
        [
    formCanvas release];
        [
    receivedData release];
        [
    server_url release];
        [
    super dealloc];
    }

    @
    end
    Please advice.

    regards & thanks.
    Budi

Share This Page