我遇到UITextField问题并向我的服务器发送请求。这是我的情况:我的APP和服务器之间通过JSON协议连接。我通过POST方法发送请求,并且我发送此消息:消息JSON。我必须以这种形式发送JSON: {“pin”:“xxxx”,“uuid”:“yyyy”}。现在我的问题是:我在UITextField中编写了密码,但我不知道如何将我在UITextField中编写的密码输入到我的JSON'消息中。我把我的代码放在下面:
ViewController.h:
@interface ViewController : UIViewController<UITextFieldDelegate, NSURLConnectionDataDelegate>
@property (retain, nonatomic) NSURLConnection *connection;
@property (retain, nonatomic) NSMutableData *receivedData;
@property (retain, nonatomic) NSString *uuid;
@property (retain, nonatomic) NSString *htmlSTR;
@property (strong, nonatomic) IBOutlet UITextField *pinField;
-(IBAction)loginButton:(id)sender;
-(IBAction)ReturnKeyButton:(id)sender;
@property(strong, nonatomic) NSData *json;
@end
ViewController.m:
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// [self uid];
}
-(void) uid{
NSString *uuid =[[NSUUID UUID] UUIDString];
NSLog(@"aaaa",uuid);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)ReturnKeyButton:(id)sender{
[sender resignFirstResponder];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
_receivedData =[[NSMutableData alloc] init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[_receivedData appendData:data];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse*)cachedResponse {
// Return nil to indicate not necessary to store a cached response for this connection
return 0;
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"Succeeded! Received %d bytes of data",[_receivedData length]);
//NSString *htmlSTR = [[NSString alloc] initWithData:_receivedData encoding:NSUTF8StringEncoding];
// NSLog(@"%@" , htmlSTR);
NSError *e = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: self.receivedData options: NSJSONReadingMutableContainers error: &e];
NSLog(@"data %@",jsonArray);
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
//NSLog(@"%@" , error);
}
- (IBAction)loginButton:(id)sender {
NSDictionary *dict =@{@"pin" : @"",
@"uuid" : @""};
NSError *error = nil;
NSData *json;
if ([NSJSONSerialization isValidJSONObject:dict])
{
//Serializowanie słownika
json = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
//sprawdzenie czy nie ma żadnych błędów, następnie wypisanie zserializowanego jsona w konsoli
if (json != nil && error == nil)
{
NSString *jsonArray = [[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding];
NSLog(@"JSON: %@", jsonArray);
}
}
[self.connection cancel];
NSMutableData *data = [[NSMutableData alloc] init];
self.receivedData = data;
[self uid];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://bottari.digitaltouch.pl/v1/login"]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:json];
//NSLog(@"%@",json);
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue: [NSString stringWithFormat:@"%d", [json length]] forHTTPHeaderField:@"Content-Length"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
self.connection = connection;
[connection start];