これは私が使用しているものです。ニーズに合わせて自由に使用してください。
-(void) loginWithUserID:(NSString*)usrID User:(NSString*)usr Password:(NSString *)psw Sender:(id)sender
{
if ([sender respondsToSelector:@selector(startLoading)]){
[sender performSelector:@selector(startLoading)];
}
baseURL = [NSString stringWithFormat:
@"http://xxx.xxx.xxx.xxx/test/service.svc/weblogin"];
NSString* serviceURL = [NSString stringWithFormat:
@"%@?userID=%@&user=%@&password=%@",baseURL,usrID,usr,psw;
NSURL *url = [NSURL URLWithString:[serviceURL stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
self.LoginOK = [JSON valueForKeyPath:@"LoginOK"];
if (LoginOK.intValue == 1) {
NSDictionary *usrData = [JSON valueForKeyPath:@"userData"];
userData = [[UserData alloc]init];
[userData readFromJSONDictionary:usrData];
NSLog(@"Userdata: %@",userData);
if ([sender respondsToSelector:@selector(loginSuccessful:)])
{
[sender performSelector:@selector(loginSuccessful:)];
}
}else{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Error" message:@"No Correct Login Credentials" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alertView show];
;}
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
if ([sender respondsToSelector:@selector(stopLoading)]){
[sender performSelector:@selector(stopLoading)];
}
[[[UIAlertView alloc] initWithTitle:@"Error"
message:@"Loginservice not available! Check your connection!"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil] show];
NSLog(@"Error: %@", error);
}];
[operation start];
};
そして、
-(void)readFromJSONDictionary:(NSDictionary *)d
{
[self setProperty1:[d objectForKey:@"property1"]];
}
メソッドを使用して、そのディクショナリからすべてのプロパティをカスタム クラスに取得します。
あなたの場合、それは使用するでしょう
NSDictionary *data = [JSON valueForKeyPath:@"data"];