Simple Login App for Iphone Tutorial

I just wanted to make some notes on this new project for mine while I work on it. I am trying to build an iPhone App for my bookkeeping website Cakeio.com. This would be my first iPhone app, so some stuff may take a while to figure out. So far I got the login part working. The app connects to a PHP file on the server and checks the username and password.

The PHP/MySQL

So to start, I created a php script on my server that checks the login info and responds with a Yes or a No.

<?
session_start();
require("dbconnection.php");

$u = $_POST['u'];
$pw = $_POST['pw'];

$check = "select ui, pw from users where ui ='" . mysql_real_escape_string(strtolower($u)) . "' and pw ='" . $pw . "'";
$login = mysql_query($check, $connection) or die(mysql_error());

if (mysql_num_rows($login) == 1) {
    $row = mysql_fetch_assoc($login);
    echo 'Yes';exit;

} else {

    echo 'No';exit;

}

mysql_close($connect);

?>

Then in the Xcode I created a new View-Based Application project. Before I did anything I loaded up some extra files into the Classes directory. The ones we need is the ASIHTTPRequest and SBJson files. First lets take a look at the ASIHTTPRequest.

ASIHTTPRequest

You can download the files here: http://allseeing-i.com/ASIHTTPRequest/

Once unzipped, you will find the files you need inside the Classes folder and then some in a couple other folders. Here is a list of the files you will need:

Inside Classes folder:
ASIAuthenticationDialog.h
ASIAuthenticationDialog.m
ASIHTTPRequestConfig.h
ASIHTTPRequestDelegate.h
ASIProgressDelegate.h
ASICacheDelegate.h
ASIHTTPRequest.h
ASIHTTPRequest.m
ASIDataCompressor.h
ASIDataCompressor.m
ASIDataDecompressor.h
ASIDataDecompressor.m
ASIFormDataRequest.h
ASIInputStream.h
ASIInputStream.m
ASIFormDataRequest.m
ASINetworkQueue.h
ASINetworkQueue.m
ASIDownloadCache.h
ASIDownloadCache.m

Inside Classes/done folder:

ASIHTTPRequestConfig.h
ASIHTTPRequestDelegate.h

Inside External/Reachability folder:

Reachability.h
Reachability.m

Just drag these files right into the Classes folder in the Groups & Files box in your Xcode.

Adding SBJson Files

Download the SBJson files here: https://github.com/stig/json-framework

Drag all the files inside the Classes folder to the Classes folder in the Groups & Files box in your Xcode.

Adding Frameworks

Next I added some necessary Frameworks. To do this follow Step 2 instructions on this page: http://allseeing-i.com/ASIHTTPRequest/Setup-instructions. I also added libxml2.2.7.3.dylib in addition to what they listed there.

Header Search Paths

I had an issue with Xcode trying to include a libxml source by that name but on my Mac I have it named libxml2 so it could not find it. To fix that I went to Xcode at the top tool bar and clicked Preferences. There click the Source Tree tab and add a new setting by clicking the + button at bottom left. There add this:

Setting Name: HEADER_SEARCH_PATHS
Path: /usr/include/libxml2

This might not be the same for you, so use your Terminal and cd over to /usr/include and see if you have a libxml or libxml2… maybe even some other one, so set it to whatever you have there.

ViewController.h

Ok, here is the fun part, here is what I did in the ViewController.h file:

#import <UIKit/UIKit.h>

@interface AppViewController : UIViewController {

IBOutlet UITextField *usernameField;
IBOutlet UITextField *passwordField;

}

@property (nonatomic, retain) UITextField *usernameField;
@property (nonatomic, retain) UITextField *passwordField;

- (IBAction) btnClicked:(id) sender;

@end

ViewController.m

And here is what I added into the ViewController.m:

#import "CakeIOViewController.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"

@implementation CakeIOViewController

@synthesize usernameField;
@synthesize passwordField;

- (IBAction) btnClicked:(id) sender {

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://www.domain.com/app/login.php"]];

[request setPostValue:[self.usernameField text] forKey:@"u"];
[request setPostValue:[self.passwordField text] forKey:@"pw"];
[request setDelegate:self];
[request startAsynchronous];

}

- (void)requestFailed:(ASIHTTPRequest *)request {
NSLog(@"Request failed: %@",[request error]);
}

- (void)requestFinished:(ASIHTTPRequest *)request {
NSLog(@"Submitted form successfully");
NSLog(@"Response was:");
NSLog(@"%@",[request responseString]);
}

- (void)didReceiveMemoryWarning {
// Releases the view if it doesnt have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that arent in use.
}

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

- (void)dealloc {
[super dealloc];
}

@end

ViewController.xib

In the ViewController.xib file I added 2 text fields and a button, then Ctr+Dragged From File’s Owner box to each text box and assigned each the appropriate textfield variable. Then I Ctr+Dragged from the Button to the File’s Owner box and assigned the btnClicked event to it.

That’s that, save and Command+R it to see if it worked. I also open up the Debugger window by doing Command+Shift+R.

Good luck.