[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 receive data from server and show UIAlert (Username correct or username incorrect) or my application can't connect to server (coz, web server offline), my application hangs / stack and out the message 'obj_sendMsg' and cannot reconnect.

    this is my code
    [OBJC]//
    // 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

    Please Register or Log in to view images

    id)sender
    {
    btnLogin.enabled = NO;
    }

    /**************************************
    ** Setelah tombol login di tekan
    *************************************/
    -(IBAction)enableLoginButton

    Please Register or Log in to view images

    id)sender
    {
    btnLogin.enabled = YES;
    }

    /**************************************
    ** Setelah tombol login di tekan
    *************************************/
    -(IBAction)doLogin

    Please Register or Log in to view images

    id)sender
    {
    NSString *usernameStr = username.text;
    NSString *passwordStr = password.text;

    if ([usernameStr length] == 0)
    {
    UIAlertView *alert = [[UIAlertView alloc]
    initWithTitle

    Please Register or Log in to view images

    "Please enter your email"
    message:nil
    delegate:self
    cancelButtonTitle

    Please Register or Log in to view images

    "Ok"
    otherButtonTitles:nil];
    [alert show];
    [alert release];

    }
    else if ([passwordStr length] == 0)
    {
    UIAlertView *alert = [[UIAlertView alloc]
    initWithTitle

    Please Register or Log in to view images

    "Please enter your password"
    message:nil
    delegate:self
    cancelButtonTitle

    Please Register or Log in to view images

    "Ok"
    otherButtonTitles:nil];
    [alert show];
    [alert release];

    }

    if ([usernameStr length] != 0 && [passwordStr length] != 0)
    {
    NSString *post = [NSString stringWithFormat

    Please Register or Log in to view images

    "username=%@&password=%@", username.text, password.text];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat

    Please Register or Log in to view images

    "%d", [postData length]];
    NSString *url = [NSString stringWithFormat

    Please Register or Log in to view images

    "%@?c=login&m=checklogin", server_url];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    // url now is http://localhost/myserver/index.php?c=login&m=checklogin
    [request setURL:[NSURL URLWithString:url]];
    [request setHTTPMethod

    Please Register or Log in to view images

    "POST"];
    [request setValue

    Please Register or Log in to view images

    ostLength forHTTPHeaderField

    Please Register or Log in to view images

    "Content-Length"];
    [request setValue

    Please Register or Log in to view images

    "application/x-www-form-urlencoded" forHTTPHeaderField

    Please Register or Log in to view images

    "Content-Type"];
    [request setHTTPBody

    Please Register or Log in to view images

    ostData];

    NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (conn)
    {
    receivedData = [[NSMutableData data] retain];
    }
    }

    [usernameStr release];
    [passwordStr release];
    }

    /*************************************
    ** Fungsi untuk scroll view keatas waktu keyboard keluar
    *************************************/
    - (void)textFieldDidBeginEditing

    Please Register or Log in to view images

    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.y - 100.0), formCanvas.frame.size.width, formCanvas.frame.size.height);
    [UIView commitAnimations];
    }

    /*************************************
    ** Fungsi untuk scroll view kebawah/balik lagi ke posisi semula waktu keyboard di tutup
    *************************************/
    - (void)textFieldDidEndEditing

    Please Register or Log in to view images

    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.y + 100.0), formCanvas.frame.size.width, formCanvas.frame.size.height);
    [UIView commitAnimations];
    }

    - (BOOL)textFieldShouldReturn

    Please Register or Log in to view images

    UITextField *)theTextField {
    [theTextField resignFirstResponder];

    return YES;
    }

    - (void)connection

    Please Register or Log in to view images

    NSURLConnection *)connection didReceiveResponse

    Please Register or Log in to view images

    NSURLResponse *)response {
    [receivedData setLength:0];
    }

    - (void)connection

    Please Register or Log in to view images

    NSURLConnection *)connection didReceiveData

    Please Register or Log in to view images

    NSData *)data {
    [receivedData appendData:data];
    }

    - (void)connection

    Please Register or Log in to view images

    NSURLConnection *)connection didFailWithError

    Please Register or Log in to view images

    NSError *)error {
    //label.text = [NSString stringWithFormat

    Please Register or Log in to view images

    "Connection failed: %@", [error description]];

    UIAlertView *alert = [[UIAlertView alloc]
    initWithTitle

    Please Register or Log in to view images

    "Can't connect to server"
    message:nil
    delegate:self
    cancelButtonTitle

    Please Register or Log in to view images

    "Ok"
    otherButtonTitles:nil];
    [alert show];
    [alert release];

    [connection release];
    [receivedData release];
    }

    - (void)connectionDidFinishLoading

    Please Register or Log in to view images

    NSURLConnection *)connection {
    NSString *responseString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];

    NSLog(responseString);

    NSString *jsonString = [NSString stringWithString:responseString];
    NSDictionary *dictionary = [jsonString JSONValue];
    NSString *result = [dictionary objectForKey

    Please Register or Log in to view images

    "success"];

    NSLog(@"%@", result);

    [responseString release];
    [receivedData release];
    [connection release];

    if ([result integerValue] == 1) // if username and password valid
    {

    UIAlertView *alert = [[UIAlertView alloc]
    initWithTitle

    Please Register or Log in to view images

    "Username or password valid"
    message:nil
    delegate:self
    cancelButtonTitle

    Please Register or Log in to view images

    "Ok"
    otherButtonTitles:nil];
    [alert show];
    [alert release];
    }
    else
    {
    UIAlertView *alert = [[UIAlertView alloc]
    initWithTitle

    Please Register or Log in to view images

    "Username or password invalid"
    message:nil
    delegate:self
    cancelButtonTitle

    Please Register or Log in to view images

    "Ok"
    otherButtonTitles:nil];
    [alert show];
    [alert release];
    }
    }

    /*
    // The designated initializer. Override to perform setup that is required before the view is loaded.
    - (id)initWithNibName

    Please Register or Log in to view images

    NSString *)nibNameOrNil bundle

    Please Register or Log in to view images

    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 alloc] init];
    server_url = _server.server_url;

    [_server release];
    }

    /*
    // Override to allow orientations other than the default portrait orientation.
    - (BOOL)shouldAutorotateToInterfaceOrientation

    Please Register or Log in to view images

    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
    [/OBJC]
    I've followed all the instructions, but still can not.

    Please advice.

    regards,
    Budi
  2. ipodmail09

    ipodmail09 Member

    Joined:
    Mar 13, 2009
    Messages:
    269
    Likes Received:
    0
    Device:
    2G iPod touch
    are you following a tutorial ?
  3. 0r4ngB41k

    0r4ngB41k New Member

    Joined:
    Nov 9, 2009
    Messages:
    7
    Likes Received:
    0
    Device:
    iPod touch
    yes, i follow from tutorial
  4. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    Something is really messed up in this thread.
  5. 0r4ngB41k

    0r4ngB41k New Member

    Joined:
    Nov 9, 2009
    Messages:
    7
    Likes Received:
    0
    Device:
    iPod touch
    wew, where is my first posting?
    ------------------double post merged------------------
    if anyone can help me?
  6. Collateral

    Collateral Active Member

    Joined:
    Sep 23, 2007
    Messages:
    1,974
    Likes Received:
    6
    Device:
    iPhone 3GS (Black)
    post the link of the tut so i can see whats wrong.
  7. iPhoneSupport

    iPhoneSupport Banned

    Joined:
    Nov 8, 2009
    Messages:
    13
    Likes Received:
    0
    Device:
    2G iPod touch
    What..language do you speak?
  8. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Selamat Datang!

    Kamu keperluan membaca "Developer Documentation" dari "Help" menu. Saya akan bantuan anda kapan. Saya mengatakan Bahasa Indonesia sedikit.

    Terima Kasih,

    cocotutch

    P.S. Tanpa Tekanan sesuatu tidak kanan:

    [OBJC]NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    // url now is http://localhost/myserver/index.php?c=login&m=checklogin
    [request setURL:[NSURL URLWithString:url]];
    [request setHTTPMethod

    Please Register or Log in to view images

    "POST"];
    [request setValue

    Please Register or Log in to view images

    ostLength forHTTPHeaderField

    Please Register or Log in to view images

    "Content-Length"];
    [request setValue

    Please Register or Log in to view images

    "application/x-www-form-urlencoded" forHTTPHeaderField

    Please Register or Log in to view images

    "Content-Type"];
    [request setHTTPBody

    Please Register or Log in to view images

    ostData];
    [/OBJC]

    Tidak Depat membaca "Localhost".

    Mengirim URL pengajaran tambahan ke saya.

    Please Register or Log in to view images



    Oh and to answer a few of everyone elses questions, he/she speaks Indonesian (Bahasa Indonesia) and a small bit of English (Bahasa Inggris).

    Please Register or Log in to view images

  9. spoonforknife

    spoonforknife Retired Moderator

    Joined:
    Nov 15, 2007
    Messages:
    6,896
    Likes Received:
    317
    It shares some components with Bahasa Malaysia (i.e. Selamat Datang), etc.

    Back on topic.

    Please Register or Log in to view images

  10. 0r4ngB41k

    0r4ngB41k New Member

    Joined:
    Nov 9, 2009
    Messages:
    7
    Likes Received:
    0
    Device:
    iPod touch
    Thank you. My problem about connection to the server is resolved.

    Regards,
    Budi

Share This Page