开发者

How to check username and password from server database and display a new page in iPhone?

I have an application in which I have a login form which consist of username and password. When user enters username and password his username and password should be validated from the server api database and if he is a valid user he should be made to login.

I have done the following code to post the login information through server api database:

 -(void)sendRequest
    {

        UIDevice *device = [UIDevice currentDevice];
        NSString *udid = [device uniqueIdentifier];
        NSString *sysname = [device systemName];
        NSString *sysver = [device systemVersion];
        NSString *model = [device model];
        NSLog(@"idis:%@",[device uniqueIdentifier]);
        NSLog(@"system nameis :%@",[device systemName]);
        NSLog(@"System version is:%@",[device systemVersion]);
        NSLog(@"System model is:%@",[device model]);
        NSLog(@"device orientation is:%d",[device orientation]);
        NSString *post = [NSString stringWithFormat:@"Loginkey=%@&Password=%@&DeviceCode=%@&Firmware=%@&IMEI=%@",txtUserName.text,txtPassword.text,model,sysver,udid];
        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
        NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 
        NSLog(@"%@",postLength);
        NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
        [request setURL:[NSURL URLWithString:@"http://192.168.0.68:91/JourneyMapperAPI?RequestType=Login"]]; 
        [request setHTTPMethod:@"POST"]; 
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
        [request setHTTPBody:postData];

        NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

        if (theConnection) {
            webData = [[NSMutableData data] retain];
            NSLog(@"%@",webData);
        }
        else 
        {

        }

    }

In this method I am parsing the JSON response received from api server and binding and inserting into SQLite database:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
    {      
        NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; 
        NSLog(@"%@",loginStatus); 

        NSString *json_string = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding]; 

        NSDictionary *result = [json_string JSONValue];
        NSArray *values = [result objectForKey:@"Result"];
        NSMutableArray *results = [[NSMutableArray alloc] init];

        for (int index = 0; index<[values count]; index++) {
            NSMutableDictionary * value = [values objectAtIndex:index];
            Result * result = [[Result alloc] init];
            result.UserID = [value objectForKey:@"UserId"];
            result.FirstName = [value objectForKey:@"FirstName"];
            result.LastName =[value objectForKey:@"LastName"];
            result.Email =[value objectForKey:@"Email"];
            result.ProfileImage =[value objectForKey:@"ProfileImage"];
            result.ThumbnailImage =[value objectForKey:@"ThumbnailImage"];
            result.DeviceInfoId =[value objectForKey:@"DeviceInfoId"];
            NSLog(@"%@",result.UserID);


            [results addObject:result];
            [result release]; 
        }



        for (int index = 0; index<[results count]; index++) {
            Result * result = [results objectAtIndex:index];
            //save the object variables to database here


            [self createEditableCopyOfDatabaseIfNeeded];

            NSString *filePath = [self getWritableDBPath];

            sqlite3 *database;
            NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970]; 
            NSNumber *timeStampObj = [NSNumber numberWithInt: timeStamp];
            NSLog(@"%@",timeStampObj);
            NSString *journeyid = [NSString stringWithFormat:@"%@_%@_%@", result.UserID, result.DeviceInfoId, timeStampObj];
            if(sqlite3_open([filePath UTF8String], &database) == SQ开发者_如何学编程LITE_OK) {
                const char *sqlStatement = "insert into UserInformation(journeyid,UserID,DeviceId,Username,Password,FirstName,Email) VALUES (?,?,?,?,?,?,?)";
                sqlite3_stmt *compiledStatement;
                if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)    {
                    sqlite3_bind_text( compiledStatement, 1, [journeyid UTF8String],-1,SQLITE_TRANSIENT);
                    sqlite3_bind_text( compiledStatement, 2, [result.UserID UTF8String],-1,SQLITE_TRANSIENT);
                    sqlite3_bind_text(compiledStatement, 3, [result.DeviceInfoId UTF8String],-1,SQLITE_TRANSIENT);
                    sqlite3_bind_text(compiledStatement, 4, [txtUserName.text UTF8String],-1,SQLITE_TRANSIENT);
                    sqlite3_bind_text(compiledStatement, 5, [txtPassword.text UTF8String],-1,SQLITE_TRANSIENT);
                    sqlite3_bind_text (compiledStatement, 6, [result.FirstName UTF8String],-1,SQLITE_TRANSIENT);
                    sqlite3_bind_text (compiledStatement, 7, [result.Email UTF8String],-1,SQLITE_TRANSIENT);

                }
                if(sqlite3_step(compiledStatement) != SQLITE_DONE ) {
                    NSLog( @"Save Error: %s", sqlite3_errmsg(database) );
                }
                else {
                    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"Record added" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                    [alert show];
                    [alert release];
                    alert = nil;
                }

                sqlite3_finalize(compiledStatement);
            }
            sqlite3_close(database);    
        }
        [loginStatus release];           
        [connection release];  
        [webData release]; 
    }  

The problem is I want when a user enters his/her username and password the username and password should be validated from the server database and if the user is valid he should be allowed to login.


Change your JSON response, Use some other key to send the response for login success/Fail. Check using the

NSString *loginres=[result valueForKey:@"LoginResponse"];

Then validate using

if([loginres isEqualToString:@"Success"]
{
  //Valid user
}
else
{
  //InValid user
}

OR

If the authentication fail let the NSArray *values = [result objectForKey:@"Result"]; be NULL

Then check

if(values!=NULL)
{
      //Valid user
}
else
{
      //InValid user
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜