I needed an easy authentication for my first iPhone app. There didnt seem to be any info on the internet about using PHP/MySQL, so Ive posted this. Im not sure if Apple would even allow it onto the appstore, as I have no plans to submit the app. The code is probably very messy and inefficient as it was my first attempt at Objective-C.
Ill assume you know how to set up a MySQL Database and connect to it using PHP.
Your PHP should look something like:
<?
session_start();
require(“iphoneconnection.php”);$u = $_GET[‘username’];
$pw = $_GET[‘password’];$check = “SELECT username, password FROM iphoneusers WHERE username=’$u’ AND password= ‘$pw’”;
$login = mysql_query($check, $connect) 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);
?>
Where iPhoneconnection.php contains all your database connection info.
Onto the iPhone side.
I reccommend you follow this guide for creating the interface, its better written than my efforts:
http://www.riccomini.name/Topics/Mobile/iPhone/SimpleLoginScreen/
1. Open XCode, create a new project. Use a View Based Application. Ive called mine Login.
In your LoginViewControler.h put:
#import <UIKit/UIKit.h>
@interface loginViewController : UIViewController {
IBOutlet UITextField *usernameField;
IBOutlet UITextField *passwordField;
IBOutlet UIButton *loginButton;
}@property (nonatomic, retain) UITextField *usernameField;
@property (nonatomic, retain) UITextField *passwordField;
@property (nonatomic, retain) UIButton *loginButton;– (IBAction) login: (id) sender;
@end
This initialises the UI elements such as the Username and Login Button which we will be creating in IB in a min.
In your LoginViewController.m put:
#import “loginViewController.h”
@implementation loginViewController
@synthesize usernameField;
@synthesize passwordField;
@synthesize loginButton;– (IBAction) login: (id) sender
{
NSString *post =[NSString stringWithFormat:@”username=%@&password=%@”,usernameField.text, passwordField.text];
NSString *hostStr = @”THIS IS MY URL/iphonelogin-do.php?”;
hostStr = [hostStr stringByAppendingString:post];
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
if([serverOutput isEqualToString:@”Yes”]){
UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@”Congrats” message:@”You are authorized “
delegate:self cancelButtonTitle:@”OK” otherButtonTitles:nil, nil];
[alertsuccess show];
[alertsuccess release];
} else {
UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@”Error” message:@”Username or Password Incorrect”
delegate:self cancelButtonTitle:@”OK” otherButtonTitles:nil, nil];
[alertsuccess show];
[alertsuccess release];
}
}– (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 {
[super dealloc];
}@end
To explain:
This part :
NSString *post =[NSString stringWithFormat:@”username=%@&password=%@”,usernameField.text, passwordField.text];
NSString *hostStr = @”THIS IS MY URL/iphonelogin-do.php?”;
hostStr = [hostStr stringByAppendingString:post];
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
Adds the data entered into the username and password field to the end of the iphonelogin-do.php? as a POST, which is then checked in the PHP. It then returns a “Yes” or “No” which is checked here:
if([serverOutput isEqualToString:@”Yes”]){
Obviously you’ll need to change “This is my URL” to your own.
Next:
3. Open up Interface Builder
Create 2 text fields and a button. Link your 2 fields to the actions by alt clicking and drag to the Files Owner.
Do the same for the Login button:
Now run the app and you should get an error message for a incorrect entry:
And success for a correct one:
Boom.
Source is now available here:
http://www.megaupload.com/?d=FNC0716V
UPDATE:
re-uploaded source:
http://rapidshare.com/files/453292790/Kiksy-iphone-login.zip
Recent Comments