私は、目標 c で Web サーバーに値を送信する方法に取り組んできました。
送信された電子メールに私の値が表示されないという事実を除いて、私は両方の端ですべてが機能していると思います。
これが私のコードです。誰かが私が間違っていることを指摘できれば、それは素晴らしいことです。
(dvalは表示されていないものです)
NSMutableString *mmessage = [[NSMutableString alloc]initWithString:@""];
[mmessage appendString:[NSString stringWithFormat:@"<p><b>Drink Name:</b> %@</p>", dval]];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://test.com/api/stuff/send"]];
NSMutableDictionary *postInfo = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObjectsAndKeys:mmessage, @"message", nil] forKey:@"form"];
NSError *e = nil;
NSData *postData = [NSJSONSerialization dataWithJSONObject:postInfo options:0 error:&e ];
__block int resp_code = 0;
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:postData];
[NSURLConnection sendAsynchronousRequest:urlRequest
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
resp_code = httpResponse.statusCode;
if(resp_code == 200){
NSDictionary *jsonObjects = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
if ([[jsonObjects objectForKey:@"status"] isEqualToString:@"success"]) {
UIAlertView *emailsuccess = [[UIAlertView alloc] initWithTitle:@"Email Successfully sent." message:nil delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[emailsuccess show];
}
else if ([[jsonObjects objectForKey:@"status"] isEqualToString:@"failed"]) {
UIAlertView *emailfailed = [[UIAlertView alloc] initWithTitle:@"Email Failed." message:[jsonObjects objectForKey:@"messages"] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[emailfailed show];
}
else {
UIAlertView *emailfailed = [[UIAlertView alloc] initWithTitle:@"Email Response." message:[jsonObjects objectForKey:@"messages"] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[emailfailed show];
}
}
}];