Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从服务器数据库中检查用户名和密码,并在iPhone中显示新页面?_Iphone_Objective C_Api_Sqlite - Fatal编程技术网

如何从服务器数据库中检查用户名和密码,并在iPhone中显示新页面?

如何从服务器数据库中检查用户名和密码,并在iPhone中显示新页面?,iphone,objective-c,api,sqlite,Iphone,Objective C,Api,Sqlite,我有一个应用程序,其中我有一个由用户名和密码组成的登录表单。当用户输入用户名和密码时,应通过服务器api数据库验证其用户名和密码,如果他是有效用户,则应让他登录 我通过服务器api数据库发布登录信息的代码如下: -(void)sendRequest { UIDevice *device = [UIDevice currentDevice]; NSString *udid = [device uniqueIdentifier]; NSSt

我有一个应用程序,其中我有一个由用户名和密码组成的登录表单。当用户输入用户名和密码时,应通过服务器api数据库验证其用户名和密码,如果他是有效用户,则应让他登录

我通过服务器api数据库发布登录信息的代码如下:

 -(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 
        {

        }

    }
-(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) == SQLITE_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]; 
    }  
在此方法中,我解析从api服务器接收的JSON响应,并绑定并插入到SQLite数据库中:

 -(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 
        {

        }

    }
-(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) == SQLITE_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]; 
    }  

问题是,当用户输入其用户名和密码时,用户名和密码应通过服务器数据库验证,如果用户有效,则应允许其登录。

更改JSON响应,使用其他键发送登录成功/失败的响应。使用

NSString *loginres=[result valueForKey:@"LoginResponse"];
然后使用

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

如果身份验证失败,则让NSArray*值=[result objectForKey:@result];无效

然后检查

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

您在这段代码中遇到了什么问题?如果用户有效或无效,则服务器应在JSON或您从JSON创建的字典中返回消息。你能告诉我们,如果用户无效,服务器会返回什么吗?Hi@iAmitWagh,我在这段代码中没有遇到任何问题。实际上,在这段代码中,我将登录信息发布到我的api服务器,并保存到我的sqlite数据库中,但实际上,当用户输入用户名和密码字段时,我希望,用户名和密码应该直接从我的api服务器数据库验证,而不是从sqlite数据库验证。ThanksHi@WTP,我在这段代码中没有遇到任何问题。实际上,在这段代码中,我将登录信息发布到我的api服务器并保存到我的sqlite数据库中,但实际上我希望在用户输入用户名和密码字段时,用户名和密码应该直接从我的api服务器数据库验证,而不是从sqlite数据库验证。谢谢您应该更改api,以便它可以返回用户是否有效的响应。Hi@KingofBliss,我尝试了你的代码,但是他们的代码有问题。当我输入错误的用户名和密码时,也表明它是一个有效的用户,然后你的Web服务出现问题。请换一下,你能告诉我哪里出了问题吗。我已经尝试过这样的代码NSString*json_string=[[NSString alloc]initWithData:webData编码:NSUTF8StringEncoding];NSDictionary*结果=[json_字符串JSONValue];NSArray*值=[result objectForKey:@result];ifvalues=空{NSLog@Valid用户;}其他{NSLog@InvalidUser;}Hi@kingoffliss,我现在不知道该更改什么。更改的JSON响应是什么