最初はメソッドを使用してサーバーにデータを送信するだけGET
で、以下のような応答を受け取ります
2015-11-16 14:21:42.168 smartschool[1963:348015] Item actcode: ZQRTNN68
2015-11-16 14:21:42.169 smartschool[1963:348015] Item parentid: 8
次のラベルにアクティベーション コードを表示するにはどうすればよいですかviewController
。
これが私のコードです:
#import "RegistrationViewController.h"
@interface RegistrationViewController ()
@end
@implementation RegistrationViewController
{
NSMutableData *mutableData;
#define URL @"http://192.168.1.166/bustracking/activation/requestActivationCode"
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(IBAction)sendDataUsingGet:(id)sender{
[self sendDataToServer : @"GET"];
}
-(void) sendDataToServer : (NSString *) method
{
NSString *parentName = parent_name.text;
NSString *contactNumber = contact_number.text;
NSString *beaconid = @"145801000095";
//NSString *beaconMacAdd = @"14:58:01:00:00:95";
if(parentName.length > 0 && contactNumber.length > 0){
NSLog (@"Getting response from server...");
NSMutableURLRequest *request = nil;
// Only Difference between POST and GET is only in the way they send parameters
if([method isEqualToString:@"GET"]){
NSString *getURL = [NSString stringWithFormat:@"%@?parent_name=%@&contact_number=%@&beacon_id=%@", URL, parentName, contactNumber, beaconid];
//url = [NSURL URLWithString: getURL];
getURL = [getURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString: getURL];
request = [NSMutableURLRequest requestWithURL:url];
NSLog(@"urlinfo: %@", url);
NSLog(@"link: %@", getURL);
}
[request setHTTPMethod:method];
[request addValue: @"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(@"connection: %@", connection);
if( connection )
{
mutableData = [NSMutableData new];
}
else
{
NSLog (@"NO_CONNECTION");
return;
}
}
else
{
NSLog(@"NO_VALUES");
}
}
#pragma mark NSURLConnection delegates
-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response
{
[mutableData setLength:0];
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[mutableData appendData:data];
}
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog (@"NO_CONNECTION");
return;
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *error = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:mutableData options:kNilOptions error:&error];
NSArray *fetchedArr = [json objectForKey:@"response"];
NSString *responseActCode;
for (NSDictionary *user in fetchedArr)
{
responseActCode = [user objectForKey:@"activation_code"];
NSLog(@"Item actcode: %@", responseActCode);
NSLog(@"Item parentid: %@", [user objectForKey:@"parent_id"]);
//NSLog(@"Item actcode: %@", [user objectForKey:@"activation_code"]);
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:responseActCode forKey:@"HighScore"];
[defaults synchronize];
NSLog(@"from data: %@", [defaults objectForKey:@"HighScore"]);
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Activation Code"
message:(@"%@", responseActCode)
delegate:nil //or self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
@end