1

我正在开发一个要求用户登录以访问他的一些信息的应用程序。我有一个存储在服务器上的 login.jsp 文件以及数据库上的所有用户名和密码。

在我的应用程序中,我有 2 个 uitextfields 一个用于用户名,一个用于密码。我知道我可以使用 POST 方法将输入传递到 Web 服务器并检查用户名和密码是否匹配,然后加载其余数据。这就是我遇到麻烦的地方,谁能帮我解决这个问题,我如何将输入从 uitextfield 传递到该 Web 服务以运行 login.jsp 脚本?提前致谢。

4

1 回答 1

0

// JDURLConnection.h

typedef void (^successBlock)(NSDictionary *dict);
typedef void (^errorBlock)(NSString *error);


@interface JDURLConnection : NSObject


+(void) urlRequestWithURL:(NSString *)urlStr
              usingMethod:(NSString *)verb
            andParameters:(NSDictionary *) param
          completionBlock:(successBlock)completed
               errorBlock:(errorBlock)errored;
@end

// JDURLConnection.m

+ (void) showMessageString:(NSString *)message{

    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"App Name!" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];

}

#pragma mark - RequestMethods

+ (NSMutableURLRequest *) URLRequestFrom:(NSURL *)url verb:(NSString *)verb parameters:(NSDictionary*)parameters{


    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                                       timeoutInterval:30];

    [request setHTTPMethod:verb];

    if (parameters != nil) {


        NSData *jsonData = [[NSString stringWithFormat:@"data=%@",[parameters JSONRepresentation]] dataUsingEncoding:NSUTF8StringEncoding];

        [request setHTTPBody:jsonData];
    }

    return request;
}



+(void) urlRequestWithURL:(NSString *)urlStr usingMethod:(NSString *)verb andParameters:(NSDictionary *) param completionBlock:(successBlock)completed errorBlock:(errorBlock)errored{

  //  [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];

    NSURL *url = [NSURL URLWithString:urlStr];

    NSMutableURLRequest *request = [self URLRequestFrom:url verb:verb parameters:param];

    NSOperationQueue *queue = [NSOperationQueue mainQueue];

    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

      //  [SVProgressHUD dismiss];

        if ([data length] > 0 && error == nil){

            id responseData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];

            NSLog(@"%@",responseData);

            NSDictionary *responseDict = nil;

            if ([responseData isKindOfClass:[NSDictionary class]])
                responseDict = (NSDictionary *)responseData;

            else if ([responseData isKindOfClass:[NSArray class]])
                responseDict = [NSDictionary dictionaryWithObject:responseData forKey:@"result"];

            else
                responseDict = [NSDictionary dictionaryWithObject:@"" forKey:@"error"];

            completed(responseDict);

        }
        else if ([data length] == 0 && error == nil){

            [self performSelectorOnMainThread:@selector(showMessageString:) withObject:@"Error! unable to receive the data from Server." waitUntilDone:YES];

            errored(@"Error! unable to receive the data from Server.");

        }
        else if (error != nil){

            [self performSelectorOnMainThread:@selector(showMessageString:) withObject:[error localizedDescription] waitUntilDone:YES];

            errored([error localizedDescription]);

        }
    }];

}

将其实现为:

    NSArray *keys = [NSArray arrayWithObjects:@"username",@"password", nil];
    NSArray *values =  [NSArray arrayWithObjects:self.tfUserName.text,self.tfUserPass.text,nil];

    NSDictionary *param = [NSDictionary dictionaryWithObjects:values forKeys:keys];

    [JDURLConnection urlRequestWithURL:[NSString stringWithFormat:@"http://yourwebURL/API/user-login"] usingMethod:@"POST" andParameters:param completionBlock:^(NSDictionary *dict)
     {

         if ([dict objectForKey:@"result"]) {

                 NSLog(@"%@",dict);
         }

     }
         errorBlock:^(NSString *error)
     {
         NSLog(@"%@",error);
//         errored(error);
     }];
于 2013-08-26T06:59:13.393 回答