0

ログインが必要なアプリを作成しています。JSON を使用しています。これまでのところ、ユーザー名とパスワードを使用して POST 要求を送信でき、トークンが返されました (コンソールに表示されます)。正しいユーザー名とパスワードの組み合わせを入力しないとトークンが返ってこない.トークンが返ってきたら次のView Controllerに進むようにしたい. if ステートメントを使用します (View Controller を切り替えるためのコードを挿入します) が、トークンが返されるかどうかを確認するために必要なパラメーターがわかりません。

実装ファイルで使用しているコードは次のとおりです。ボタンが押されたときに実行されるメソッドにあります。

@try {

    if([[usernameTextField text] isEqualToString:@""] || [[passTextField text] isEqualToString:@""] ) {
        [self alertStatus:@"Please enter both Username and Password" :@"Login Failed!"];
    } else {
        NSString *post =[[NSString alloc] initWithFormat:@"username=%@&password=%@",[usernameTextField text],[passTextField text]];
        NSLog(@"PostData: %@",post);

        NSURL *url=[NSURL URLWithString:@"https://beta.network360.com/tokens"];

        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];


        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." :@""];

            } 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" :@""];
        }
    }
}
@catch (NSException * e)
{
    NSLog(@"Exception: %@", e);
    [self alertStatus:@"Login Failed." :@""];
      //[[PSearchViewController new] performSegueWithIdentifier:@"loginCancel" sender:self];
}

また、正しいユーザー名とパスワードの組み合わせを入力したときにコンソール出力に表示されるものは次のとおりです(ところで、コンソールに表示された機密情報をすべて変更しようとしたため、一部の情報が完全に一致しない場合は、トークンが戻ってくることを示したかっただけです):

2013-07-28 13:23:21.607 Empyrean[28283:c07] PostData: username=username@gmail.com&password=password
2013-07-28 13:23:22.300 Empyrean[28283:c07] Response code: 200
2013-07-28 13:23:22.301 Empyrean[28283:c07] Response ==> {"token":"scFDzxSAVk2sxQBShEGS","user":{"id":300230,"username":"username@gmail.com","display_name":"FirstName LastName","unconfirmed_email":null,"email":"username@gmail.com","confirmation_email":"username@gmail.com","client_identifier":null,"client_id":138,"is_admin":false,"support_email":"support@supportemail.com","application_name":"AppName","show_project_vintage_date":false,"is_anonymous":false,"is_active":true,"is_confirmed":true,"pending_reconfirmation":false,"can_resend_confirmation":false,"client_name":"Broker","show_advertisements":true,"header_logo":"/foo/headerlogo.gif","report_footer_logo":"/stuff/foo/footerlogo.png","authorized_features":["find_stuff","do_stuff","stuff_stuff","settings","menu","manage_stuff","measure_stuff","export_stuff"],"url":"https://www.website.com/stuff/numbersdsjkflds"}}
2013-07-28 13:23:22.304 Empyrean[28283:c07] {
    token = dlsfkasdfDfdsklfdDsa;
    user =     {
        "application_name" = "Application Name";
        "authorized_features" =         (
            "find_stuff",
            "do_stuff",
            "stuff_stuff",
            settings,
            menu,
            "manage_stuff",
            "measure_stuff",
            "export_stuff"
        );
        "can_resend_confirmation" = 0;
        "client_id" = 138;
        "client_identifier" = "<null>";
        "client_name" = Broker;
        "confirmation_email" = "username@gmail.com";
        "display_name" = "FirstName LastName";
        email = "username@gmail.com";
        "url" = "https://www.website.com/stuff/numbersdsjkflds";
        "header_logo" = "/foo/headerlogo.gif";
        id = 300230;
        "is_active" = 1;
        "is_admin" = 0;
        "is_anonymous" = 0;
        "is_confirmed" = 1;
        "pending_reconfirmation" = 0;
        "report_footer_logo" = "/stuff/foo/footerlogo.png";
        "show_advertisements" = 1;
        "show_project_vintage_date" = 0;
        "support_email" = "support@supportemail.com";
        "unconfirmed_email" = "<null>";
        username = "username@gmail.com";
    };
}
4

1 に答える 1

0

NSDictionary *jsonData辞書です。したがって、tokenキーが存在するかどうかを確認できます。

if (jsonData[@"token"])
{
  // Token exists, so move on. 
  [self.navigationController pushViewController:nextController animated:YES];
}
else
{
  // Tell the user they messed it up. 
}
于 2013-07-29T01:02:58.217 に答える