Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I am calling this web-service for test purpose and it is working fine.
- <?php
- header('Content-type: application/json');
- if($_POST) {
- if($_POST['email'] == 'saad.bsit@hotmail.com' && $_POST['password'] == '12345') {
- echo '{"success":1}';
- } else {
- echo '{"success":0,"error_message":"Username and/or password is invalid."}';
- }
- }else { echo '{"success":0,"error_message":"Username and/or password is invalid."}';}
- ?>
- action on button in sign in page
- - (IBAction)btnSignIn:(id)sender {
- @try {
- if([[txtUserEmail text] isEqualToString:@""] || [[txtUserPassword text] isEqualToString:@""] ) {
- [self alertStatus:@"Please enter both Username and Password" :@"Login Failed!"];
- } else {
- NSString *post =[[NSString alloc] initWithFormat:@"email=%@&password=%@",[txtUserEmail text],[txtUserPassword text]];
- NSLog(@"PostData: %@",post);
- NSURL *url=[NSURL URLWithString:@"http://localhost/colorSearch/sample_code.php"];
- 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];
- //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
- NSError *error = [[NSError alloc] init];
- NSHTTPURLResponse *response = nil;
- NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
- NSLog(@"Response code: %d", [response statusCode]);
- if ([response statusCode] >=200 && [response statusCode] <300)
- {
- NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
- NSLog(@"Response ==> %@", responseData);
- SBJsonParser *jsonParser = [SBJsonParser new];
- NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
- NSLog(@"%@",jsonData);
- NSInteger success = [(NSNumber *) [jsonData objectForKey:@"success"] integerValue];
- NSLog(@"%d",success);
- if(success == 1)
- {
- NSLog(@"Login SUCCESS");
- [self alertStatus:@"Logged in Successfully." :@"Login Success!"];
- ColorPickerViewController *cpvc =[[ColorPickerViewController alloc] init];
- [self.navigationController pushViewController:cpvc animated:YES];
- } else {
- NSString *error_msg = (NSString *) [jsonData objectForKey:@"error_message"];
- [self alertStatus:error_msg :@"Login Failed!"];
- }
- } else {
- if (error) NSLog(@"Error: %@", error);
- [self alertStatus:@"Connection Failed" :@"Login Failed!"];
- }
- }
- }
- @catch (NSException * e) {
- NSLog(@"Exception: %@", e);
- [self alertStatus:@"Login Failed." :@"Login Failed!"];
- }
- }
- - (void) alertStatus:(NSString *)msg :(NSString *)title
- {
- UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
- message:msg
- delegate:self
- cancelButtonTitle:@"Ok"
- otherButtonTitles:nil, nil];
- [alertView show];
- }
- But if I try this which is the actual one code the code didn't execute. I think web service is not getting proper UrlRequest. This web service is working on tags, which I think is missing in my Request, My friend is using the same in Android and it is working fine for him, So in IOS how can we use this.
- In very beginning few lines you will get my point so I think there is no need to look all below code.
- Actual Web-service.
- <?php
- /**
- PHP API for Login, Register, Changepassword, Resetpassword Requests and for Email Notifications.
- **/
- if (isset($_POST['tag']) && $_POST['tag'] != '') {
- // Get tag
- $tag = $_POST['tag'];
- // Include Database handler
- require_once 'include/DB_Functions.php';
- $db = new DB_Functions();
- // response Array
- $response = array("tag" => $tag, "success" => 0, "error" => 0);
- // check for tag type
- if ($tag == 'login') {
- // Request type is check Login
- $email = $_POST['email'];
- $password = $_POST['password'];
- // check for user
- $user = $db->getUserByEmailAndPassword($email, $password);
- if ($user != false) {
- // user found
- // echo json with success = 1
- $response["success"] = 1;
- $response["user"]["fname"] = $user["firstname"];
- $response["user"]["lname"] = $user["lastname"];
- $response["user"]["email"] = $user["email"];
- $response["user"]["uname"] = $user["username"];
- $response["user"]["uid"] = $user["unique_id"];
- $response["user"]["created_at"] = $user["created_at"];
- echo json_encode($response);
- } else {
- // user not found
- // echo json with error = 1
- $response["error"] = 1;
- $response["error_msg"] = "Incorrect email or password!";
- echo json_encode($response);
- }
- }
- else if ($tag == 'chgpass'){
- $email = $_POST['email'];
- $newpassword = $_POST['newpas'];
- $hash = $db->hashSSHA($newpassword);
- $encrypted_password = $hash["encrypted"]; // encrypted password
- $salt = $hash["salt"];
- $subject = "Change Password Notification";
- $message = "Hello User,\n\nYour Password is sucessfully changed.\n\nRegards,\nDigitalnet Team.";
- $from = "contact@Digitalnet.com";
- $headers = "From:" . $from;
- if ($db->isUserExisted($email)) {
- $user = $db->forgotPassword($email, $encrypted_password, $salt);
- if ($user) {
- $response["success"] = 1;
- mail($email,$subject,$message,$headers);
- echo json_encode($response);
- }
- else {
- $response["error"] = 1;
- echo json_encode($response);
- }
- // user is already existed - error response
- }
- else {
- $response["error"] = 2;
- $response["error_msg"] = "User not exist";
- echo json_encode($response);
- }
- }
- else if ($tag == 'forpass'){
- $forgotpassword = $_POST['forgotpassword'];
- $randomcode = $db->random_string();
- $hash = $db->hashSSHA($randomcode);
- $encrypted_password = $hash["encrypted"]; // encrypted password
- $salt = $hash["salt"];
- $subject = "Password Recovery";
- $message = "Hello User,\n\nYour Password is sucessfully changed. Your new Password is $randomcode . Login with your new Password and change it in the User Panel.\n\nRegards,\nLearn2Crack Team.";
- $from = "contact@Digitalnet.com";
- $headers = "From:" . $from;
- if ($db->isUserExisted($forgotpassword)) {
- $user = $db->forgotPassword($forgotpassword, $encrypted_password, $salt);
- if ($user) {
- $response["success"] = 1;
- mail($forgotpassword,$subject,$message,$headers);
- echo json_encode($response);
- }
- else {
- $response["error"] = 1;
- echo json_encode($response);
- }
- // user is already existed - error response
- }
- else {
- $response["error"] = 2;
- $response["error_msg"] = "User not exist";
- echo json_encode($response);
- echo '{"success":0,"error_message":"Username and/or password is invalid."}';
- }
- }
- else if ($tag == 'register') {
- // Request type is Register new user
- $fname = $_POST['fname'];
- $lname = $_POST['lname'];
- $email = $_POST['email'];
- $uname = $_POST['uname'];
- $password = $_POST['password'];
- $subject = "Registration";
- $message = "Hello $fname,\n\nYou have sucessfully registered to our service.\n\nRegards,\nAdmin.";
- $from = "contact@Digitalnet.com";
- $headers = "From:" . $from;
- // check if user is already existed
- if ($db->isUserExisted($email)) {
- // user is already existed - error response
- $response["error"] = 2;
- $response["error_msg"] = "User already existed";
- echo json_encode($response);
- }
- else if(!$db->validEmail($email)){
- $response["error"] = 3;
- $response["error_msg"] = "Invalid Email Id";
- echo json_encode($response);
- }
- else {
- // store user
- $user = $db->storeUser($fname, $lname, $email, $uname, $password);
- if ($user) {
- // user stored successfully
- $response["success"] = 1;
- $response["user"]["fname"] = $user["firstname"];
- $response["user"]["lname"] = $user["lastname"];
- $response["user"]["email"] = $user["email"];
- $response["user"]["uname"] = $user["username"];
- $response["user"]["uid"] = $user["unique_id"];
- $response["user"]["created_at"] = $user["created_at"];
- mail($email,$subject,$message,$headers);
- echo json_encode($response);
- } else {
- // user failed to store
- $response["error"] = 1;
- $response["error_msg"] = "JSON Error occured in Registartion";
- echo json_encode($response);
- }
- }
- } else {
- $response["error"] = 3;
- $response["error_msg"] = "JSON ERROR";
- echo json_encode($response);
- }
- } else {
- echo "Digitalnet API";
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement