我试过用我的按钮从两个文本区域发送一些数据,一旦我按下按钮,我收到一封电子邮件,没有我在iOS模拟器中输入的文本字段的内容。我找不到问题,请帮帮我。
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
UITextField * firstname;
UITextField *lastname;
UIButton *sendPost;
}
@property (strong, nonatomic) IBOutlet UITextField *firstname;
@property (strong, nonatomic) IBOutlet UITextField *lastname;
@property (strong, nonatomic) IBOutlet UIButton *postButtonTapped;
-(IBAction)postButtonTapped:(id)sender;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize firstname;
@synthesize lastname;
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)postButtonTapped:(id)sender
{
NSString *myRequestString = [NSString stringWithFormat:@"firstname=%@&lastname=%@",firstname.text,lastname.text];
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://textfake.com/test/mail.php"]];
[request setHTTPMethod: @"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPBody: myRequestData];
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",response);
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.firstname resignFirstResponder];
[self.lastname resignFirstResponder];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
PHP代码
<?php
header("Content-type: text/html; charset=UTF-8");
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$text = "not set";
if(!empty($_POST["text"])){
$text = $_POST["text"];
}
$lat = $_POST["lat"];
$lon = $_POST["lon"];
$dieMessage =$firstname." ".$lastname."\n".$text."\n";
$dieMessage .="Lat:".$lat."\n";
$dieMessage .="Lon:".$lon;
mail("fortesing@gmail.com", "gps daten", $dieMessage);
//echo 'Email abgeschickt!';
echo $dieMessage;
?>
答案 0 :(得分:0)
- (IBAction)postButtonTapped:(id)sender
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = @{@"firstname": firstname.text,
@"lastname": lastname.text};
[manager POST:@"http://textfake.com/test/mail.php" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
希望这有效。
答案 1 :(得分:0)
对于iOS部分,试试这个:
- (IBAction)postButtonTapped:(id)sender {
NSURL *url = [NSURL URLWithString:@"http://textfake.com/test/mail.php"];
NSString *post = [NSString stringWithFormat:@"firstname=%@&lastname=%@",firstname.text,lastname.text];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
// set POST method
[urlRequest setHTTPMethod:@"POST"];
// adding keys with values
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
[urlRequest addValue:postLength forHTTPHeaderField:@"Content-Length"];
[urlRequest setHTTPBody:postData];
NSURLSession *urlSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration]];
NSURLSessionDownloadTask *urlSessionDownloadTask = [urlSession downloadTaskWithRequest:urlRequest completionHandler:^(NSURL *localfile, NSURLResponse *response, NSError *error) {
NSData *data = [NSData dataWithContentsOfURL:localfile];
//do what you want with your data here. PS your are not on main thread !
}];
[urlSessionDownloadTask resume];
}
对于PHP部分,您无法获得 text,lat,lon ,因为您从未发布过它!
答案 2 :(得分:0)
我解决了这个问题,好像它遇到了一些问题:
[NSURL URLWithString: @"http://textfake.com/test/mail.php"]];
它应该是www。代替:
[NSURL URLWithString: @"http://www.textfake.com/test/mail.php"]];