Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-22-2010, 09:12 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 6
fodderkonoko is on a distinguished road
Question newbie question

Hi everybody..
I am a total newbie to iphone development, and i wanna do an application on a test project on remote database server communication.
I know a little bit of iphone sdk and cocoa framework.
My application will contain :
A view with two textfields, one button and one for displaying data.
On launch of the application i want to show
two textfields which will ask the user to enter the userID and password.
The button name will be send and on click of the button, data will be send to the database server and again fetch data from the server and display in the display area.
It will be a simple urlconnection...
I want a simple example code to start up ..
Can anybody help me..plz...
fodderkonoko is offline   Reply With Quote
Old 03-22-2010, 11:00 AM   #2 (permalink)
Code before baby wakes up
 
RobotWoods's Avatar
 
Join Date: Sep 2009
Posts: 189
RobotWoods is on a distinguished road
Default

I would suggest having a basic php on your server that takes userID and password as variables, and just echoes the data you want, then in the iphone code, you can do

PHP Code:
NSURL *myURL = [NSURL URLWithString:@"http://www.yourserver.com/file.php?userID=%@&password=%@"userIDField.textpasswordField.text];
NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString:myURL]];
NSString *Str = [[NSString allocinitWithBytes: [data byteslength:[data lengthencodingNSUTF8StringEncoding];
displayArea.text=Str
is that enough to get you on track?

Last edited by RobotWoods; 03-23-2010 at 10:16 PM.
RobotWoods is offline   Reply With Quote
Old 03-22-2010, 11:39 PM   #3 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 6
fodderkonoko is on a distinguished road
Default

Hi Robotwoods,
Thanks for ur response..
yeah i can start with , can u tell me how can i get the data back from the sever..
I mean i just want to know that how the data is coming to the variable "data"
plz can u just resolve my query..
thanks..
Edit:
But how will i distinguish between the data, i.e, username & password..

Last edited by fodderkonoko; 03-22-2010 at 11:44 PM.
fodderkonoko is offline   Reply With Quote
Old 03-22-2010, 11:52 PM   #4 (permalink)
Code before baby wakes up
 
RobotWoods's Avatar
 
Join Date: Sep 2009
Posts: 189
RobotWoods is on a distinguished road
Default

Well, the first line there says "here is a file" (the file in the example is a php file, that's accepting the variables of 'userID', and 'password' with the values from the UITextFields you said you wanted to have.

The second line says 'data' is what is in the file.
The third line converts 'data' to a string.
The last takes your UITextField and populates it with the string.

Going back to the php file, that was just a proposal...do you have any idea what you [are doing/want to do] at the server? Is it a PHP/MySQL situation? Since you are the only one that knows what your system does, you are the only one that can answer.

When you tell it username and password, what are you expecting it to tell you?

Last edited by RobotWoods; 03-23-2010 at 10:17 PM.
RobotWoods is offline   Reply With Quote
Old 03-23-2010, 12:40 AM   #5 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 6
fodderkonoko is on a distinguished road
Default

i have a php/mysql connection.
The database will have a table with two columns respectively.
Now in the front end i will have the textarea and two textfields and a button.
Once i write in the textfields and press the button, the strings needs to be sent to the database, through the url as u said, i suppose, and also at the same time fetch the data from the database and put it in the textarea.
but my question is: how will i send the response back from the php/mysql
for the http response.
i mean how will the server side response to my query?
is there any particular strategy i need to follow?
i need to keep it simple for a demo today..
So can u plz give me a short overview on this..
plz..Thanks and many thanks..
fodderkonoko is offline   Reply With Quote
Old 03-23-2010, 09:06 AM   #6 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 6
fodderkonoko is on a distinguished road
Default

anybody!!!
i am really stuck here..
i have not enough knowledge in php/mysql.
and also i dont know enough on iphone api functions related to remote server access.
please guys help me..

Thanks....
fodderkonoko is offline   Reply With Quote
Old 03-23-2010, 09:52 AM   #7 (permalink)
Code before baby wakes up
 
RobotWoods's Avatar
 
Join Date: Sep 2009
Posts: 189
RobotWoods is on a distinguished road
Default

Sorry, the time change worked against you. If you look at the URL I proposed, the variables are tacked onto the end (after the .php) and then they take their values from the textfields you mentioned.

Within the php file, you'll need to do whatever you want, so maybe it will be something like (and this is being written on the fly, little security/hashing in mind and no error check)

PHP Code:
<?php
//info.php has your db, server, usr, and pwd variables
require("info.php");
$con=mysql_connect($server,$usr,$pwd);
$db=mysql_select_db($db,$con);
$user=mysql_real_escape_string($_GET["userID"]);
$password=mysql_real_escape_string($_GET["password"]);
//assuming there is a table called users in your db
$checkPwdQuery=mysql_query("SELECT password FROM users WHERE user='$user'");
$storedPwd=mysql_fetch_row($checkPwdQuery);
if (
$password==$storedPwd[0]){
die (
'Password match');}
else{
die (
'Wrong password');}
?>
RobotWoods is offline   Reply With Quote
Old 03-23-2010, 10:03 AM   #8 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 6
fodderkonoko is on a distinguished road
Default

Thanks Woods..
for the php code..
but the iphone code u posted i giving an error in this line..
Code:
NSURL *myURL = [NSURL URLWithString:@"http://www.yourserver.com/file.php?userID=%@&password=%@", userIDField.text, passwordField.text];
its saying : too many values for "URLWithString".
So what can i do with that..
ca u plz tell me..
fodderkonoko is offline   Reply With Quote
Old 03-23-2010, 10:15 AM   #9 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 6
fodderkonoko is on a distinguished road
Default

Thanks for the response..
But i am facing an error in this line :
NSURL *myURL = [NSURL URLWithString:@"http://www.yourserver.com/file.php?userID=%@&password=%@", userIDField.text, passwordField.text];

error: too many parameters for the function..

can u plz tell me what to do..
fodderkonoko is offline   Reply With Quote
Old 04-06-2010, 07:43 PM   #10 (permalink)
Code before baby wakes up
 
RobotWoods's Avatar
 
Join Date: Sep 2009
Posts: 189
RobotWoods is on a distinguished road
Default

sorry, use this instead:

PHP Code:
NSString *myURL = [ NSString stringWithFormat: @"http://www.yourserver.com/file.php?userID=%@&password=%@"userIDField.textpasswordField.text];
NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString:myURL]];
NSString *Str = [[NSString allocinitWithBytes: [data byteslength:[data lengthencodingNSUTF8StringEncoding];
displayArea.text=Str
RobotWoods is offline   Reply With Quote
Old 09-12-2011, 08:25 PM   #11 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 2
back2front is on a distinguished road
Default Great Example

great example, have been learning php for a bit, and already have a couple apps in the app store...

my question is this. I am trying to do a basic if statement to confirm login, based off the string (Str) but the code does not seem to be working, it always says Login Incorrect, and I know the login is correct, as before this code I have a NSLog displaying Str, and I also have it in a UILabel so I can test with the correct login info and wrong login info

Code:
 if (Str ==@"Wrong password") {
        NSLog(@"LOGIN INCORRECT");
           } else {
             NSLog(@"Login SUCCESS !!!");
    }


any help would be appreciated!
back2front is offline   Reply With Quote
Old 09-12-2011, 09:09 PM   #12 (permalink)
Code before baby wakes up
 
RobotWoods's Avatar
 
Join Date: Sep 2009
Posts: 189
RobotWoods is on a distinguished road
Default

for string comparison, try

Code:
if([Str isEqualToString:@"Wrong Password"]){
NSLog(@"LOGIN INCORRECT");}
else {
NSLog(@"Login SUCCESS !!!");}
RobotWoods is offline   Reply With Quote
Old 09-12-2011, 09:24 PM   #13 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 2
back2front is on a distinguished road
Default

Thanks, totally had a brain fart that should make sense I just learned that like 2 days ago *doh*

code works, thank you!
back2front is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 351
11 members and 340 guests
akacaj, c2matrix, cgokey, esoteric, givensur, HemiMG, Mirotion22, mjnafjke, Pudding, SLIC, Techgirl-52
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,651
Threads: 94,115
Posts: 402,887
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Mirotion22
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:16 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0