Comments (218)

  1. Sajedur Rahman

    When i run this code, show username password is invalid.And show json data is null.why?

  2. Hey,

    If you are saving the login info and status in user defaults, then clear them and send the user to the login page.

    [[NSUserDefaults standardUserDefaults] setPersistentDomain:[NSDictionary dictionary] forName:[[NSBundle mainBundle] bundleIdentifier]];
    [[NSUserDefaults standardUserDefaults] synchronize];

    If your website/server uses cookies, you may even clear that too.
    Example:

    for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies])
     {
       if([[cookie domain] rangeOfString:@"dipinkrishna.com"].location != NSNotFound)
       {
         [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
       }
     }
  3. marcopolo

    Hello bro,

    Thank you for this tutorial, i have a follow up question.. how will i end logging out the application? thank u so much

  4. Hello,

    thank you for this informative tutorial , I had one problem after adding the json code, when I ran the code and i was reading the console message, i noticed it was always taking username value as null , so I deleted the field and added it again and then it displayed the username correctly in the console, but when i did that the server in which i’m uploading the php code at got a 500 error, I deleted that php code and added it again and still facing the same error code.

    can you help?
    thank you!

  5. This should do:

    NSURL *url = [NSURL URLWithString: @"http://www.mysite.co.uk/testpost.php"];
    NSString *postData =[[NSString alloc] initWithFormat:@"username=%@&password=%@",[_txtUsername text],[_txtPassword text]];
    // Create the request object
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
    [request setHTTPMethod: @"POST"];
    [request setHTTPBody: [postData dataUsingEncoding: NSUTF8StringEncoding]];
     
    // create the web view
    UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    //Change Frame
    webView.frame = CGRectMake(webView.frame.origin.x, webView.frame.origin.y + 20, webView.frame.size.width, webView.frame.size.height);
     
    // Load the request
    [webView loadRequest: request];
     
    //show the webview
    [self.view addSubview:webView];
  6. Paul

    Basically what I am trying to achieve is when your login script gives a success and I send it into a webview, I am trying to get the same information (username and password from the text inputs) into the webview also. Is their any way to do this?

  7. try using the ‘POST’ version.

    NSURL *url = [NSURL URLWithString: @"https://dipinkrishna.com/jsonlogin.php"];
    NSString *postData =[[NSString alloc] initWithFormat:@"username=%@&password=%@",[txtUsername text],[txtPassword text]];
     
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
    [request setHTTPMethod: @"POST"];
    [request setHTTPBody: [postData dataUsingEncoding: NSUTF8StringEncoding]];
     
    [webView loadRequest: request];
  8. Paul

    Yes that is what im trying to do. Ime using this but doesn’t seem to be passing any data through

    UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    //Change Frame
    webView.frame = CGRectMake(webView.frame.origin.x, webView.frame.origin.y + 20, webView.frame.size.width, webView.frame.size.height);
    //Create a URL object.
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@”http://www.mysite.co.uk/testpost.php?username=%@&password=%@”, [_txtUsername text],[_txtPassword text]]];
    //URL Requst Object
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    //Load the request in the UIWebView.
    [webView loadRequest:requestObj];
    //show the webview
    [self.view addSubview:webView];

    The contents of the testpost.php is

  9. If you are planning to ‘POST’ the data to the web view, see the below example:

    NSURL *url = [NSURL URLWithString: @"https://dipinkrishna.com/jsonlogin.php"];
    NSString *postData =[[NSString alloc] initWithFormat:@"username=%@&password=%@",[txtUsername text],[txtPassword text]];
     
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
    [request setHTTPMethod: @"POST"];
    [request setHTTPBody: [postData dataUsingEncoding: NSUTF8StringEncoding]];
     
    [webView loadRequest: request];
  10. Use [txtUsername text] and [txtPassword text]

    eg:

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://dipinkrishna.com?username=%@&password=%@", [txtUsername text],[txtPassword text]]];
  11. Paul

    Thanks that works like a charm, just one more.

    Is their any way to pass the post data used to verify the username and pass into the web view?

  12. This is a quick fix:

    webView.frame = CGRectMake(webView.frame.origin.x, webView.frame.origin.y + 20, webView.frame.size.width, webView.frame.size.height);
    UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    //Change Frame
    webView.frame = CGRectMake(webView.frame.origin.x, webView.frame.origin.y + 20, webView.frame.size.width, webView.frame.size.height);
    //Create a URL object.
    NSURL *url = [NSURL URLWithString:@"https://dipinkrishna.com"];
    //URL Requst Object
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    //Load the request in the UIWebView.
    [webView loadRequest:requestObj];
    //show the webview
    [self.view addSubview:webView];
  13. Paul

    Scratch that last question I manage to figure it out, I used the second method you gave me. just one more question, my app is sitting over the navigation bar, is there any way I can set the size of the uiwebview or just get it to nudge down a little so its not under the clock? Thanks for all your help!

  14. Hi,

    If you want to go to another view/screen then use:

    HomeViewController *homeView = [[HomeViewController alloc] init];
    [self.navigationController pushViewController:homeView animated:YES];

    Or if you just want to show a uiwebview then use:

    UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds]; 
    //Create a URL object.
    NSURL *url = [NSURL URLWithString:@"https://dipinkrishna.com"];
    //URL Requst Object
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    //Load the request in the UIWebView.
    [webView loadRequest:requestObj];
    //show the webview
    [self.view addSubview:webView];
  15. Paul

    Also how do I get the code to point to a UIWebView I have created if the login is a success?

  16. Paul

    Great tutorial, How would I add a device id into this submission so I can store it for the purpose of push notifications?

  17. Ramesh Kumar

    Thank you sir, for this tutorial
    this tutorial is provide good platform for bigeners.

    thanks allot…..

  18. jose

    in adittion I am receiving this messages in console
    2014-01-14 17:11:46.829 LoginJSon[1067:a0b] Response ==>
    2014-01-14 17:11:46.830 LoginJSon[1067:a0b] (null)
    2014-01-14 17:11:46.830 LoginJSon[1067:a0b] 0

  19. jose

    Hi guys well structure tutotial. I am having a problem. When I put the original url NSURL *url=[NSURL URLWithString:@”https://dipinkrishna.com/jsonlogin.php”];
    the programs work perfectly. But when I put there my local addres it does not work I am putting http://localhost/jose/jsonlogin.php“. has anyone had the same proble any suggestions?. I tried restarting apache but it still not working. When i write in my browser localhost it shows “it works”.
    thanks

  20. Jai

    Sir, Its really help for me. Is the perfect tutorial for beginers. I need to know the code of the login page.

  21. narasimha

    can u send me the same tutorial which is developed in xcode 5??It’ll be very helpful to me. Thanks in advance…

  22. jon

    Hi Dipin,
    Any idea when an Xcode 5/IOS 7 version will be available?
    Thanks
    Jon

  23. Use:

    HomeViewController *homeView = [[HomeViewController alloc] init];
    [self.navigationController pushViewController:homeView animated:YES];

    instead of the alertView when the login is success.

  24. Hi, I’ve successfully launched your program, great work thanks.

    I would just like to know if how can I go to the next page after login in

    if(success == 1)
    {
    NSLog(@”Login SUCCESS”);
    [self alertStatus:@”Logged in Successfully.” :@”Login Success!”];

    //enter code to next page here. (what code do I enter here)

    } else {

    thanks and regards

  25. kumar

    Sorry about it
    Im logged in
    but I’m getting output in html format..
    what should i do??

  26. kumar

    hi dipin , i really appreciate your work
    i have changed the parameters n also I’m providing right credentials but still it gives error “Login failed”.
    the response code is 200 and success=0
    what should be the problem?
    Plz guide me I’m new to iphone development
    thanks in advance.. 🙂

  27. Arch

    ok that error got cleared but now I get response code 415
    my code looks like this:NSString *post =[[NSString alloc] initWithFormat:@”{\”username\”:\”%@\”,\”password\”:\”%@\”}”,[self.txtUserName text],[self.txtPaswd text]];

    NSURL *url=[NSURL URLWithString:@”http://112.23.82.110:9080/SSR/webapi/pager1/login”];

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@”%d”, [postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:url];
    [request setHTTPMethod:@”POST”];
    [request setValue:postLength forHTTPHeaderField:@”Content-Length”];
    [request setValue:@”application/json” forHTTPHeaderField:@”Accept”];
    [request setValue:@”application/x-www-form-urlencoded” forHTTPHeaderField:@”Content-Type”];
    [request setHTTPBody:postData];

  28. Arch

    yes, my backend application has published the restfult web service which I consume to interact with that app.

  29. Arch

    Thanks for this great tutorial.
    I ma new to iOS/JSON programming and need help here. I send my data to the restful servce url in this format {“username:”myuser”,”password”:”mypassword”} but I get an error -JSONRepresentation failed, not a valid type for JSON,
    Please help

Leave a comment

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.