php - 使用 xcode 8 (Objective-c) 将 Textfield 值发送到 PHP MySQL,有或没有点击操作?

标签 php ios mysql objective-c xcode8

我只是想问一下如何在xcode中使用obj c将文本字段值发送到mysql数据库没有任何点击操作有点击操作

如果下面的代码可以从 Web 服务器检索 JSON 数据并将其显示到 xcode 中:

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController

@synthesize username,email;

- (void)viewDidLoad {
    [super viewDidLoad];

    NSError *error;
    NSString *url_string    = [NSString stringWithFormat: @"http://localhost/test.php"];
    NSData *data            = [NSData dataWithContentsOfURL: [NSURL URLWithString:url_string]];
    NSMutableArray *json    = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    NSDictionary *dict      = [json firstObject];
    NSString *data1         = [dict valueForKey:@"username"];
    NSString *data2         = [dict valueForKey:@"email"];
    email.text              = data1;
    pw.text                 = data2;   
}
@end

如何将用户名、电子邮件值发布到mysql数据库中?与 post 方法应用的技术相同吗?因为我不想使用 SBJson 类(如果可能的话)。

最佳答案

有几种方法可以做到这一点。首先,请务必注意 dataWithContentsOfURL 不是异步请求。这意味着,如果您使用它来传输大数据,则很有可能会卡住应用程序。对于异步请求,您应该使用 NSURLRequest。

话虽如此,有优秀的 API 可以异步上传/下载数据。 AFNetworking 是非常常用且有据可查的一个。 。这是在 NSURLRequest 之上编码的。

例如,在 PHP 中,您可以从 POST 语句中检索字段,如下所示:

<?php
  $username = $_POST["username"];
  $email = $_POST["email"];
?>

在您的应用中,您可以在 AFNetworking 中使用 POST 请求调用 PHP 脚本,如下所示:

NSString *username = @"username";
NSString *email = @"email";
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"yourUrl" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    NSLog(@"Sending POST request to server");

    [formData appendPartWithFormData:[username dataUsingEncoding:NSUTF8StringEncoding] name:@"username"];
    [formData appendPartWithFormData:[email dataUsingEncoding:NSUTF8StringEncoding] name:@"email"];

} error:nil];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];

NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress) {

    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"SERVER UPLOAD FRACTION COMPLETED: %f", uploadProgress.fractionCompleted);
    });

} completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {

    NSLog(@"responseObject %@", responseObject);
    NSString *responseString = [[[NSString alloc] initWithData:responseObject encoding:NSASCIIStringEncoding] mutableCopy];
    NSLog(@"The respose is: %@", responseString);

    if(error) {
        NSLog(@"Error: %@", error);

    } else {
        NSLog(@"The response is: %@", responseString);
        // Do something with the response
    }
}];
[uploadTask resume];

关于php - 使用 xcode 8 (Objective-c) 将 Textfield 值发送到 PHP MySQL,有或没有点击操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41539809/

相关文章:

mysql - 在查询时将变量设置为 true 或 false mysql

mysql - 按特定顺序从 MySQL 中检索记录

php - Wordpress - wp-query 按 PHP/MySQL 查询数组中的自定义字段列出帖子

php - 如何返回字符串是属性子字符串的所有行

php - 无法显示存储在数据库中的 BLOB

ios - UICollectionView contentSize.width 错误

mysql - 在 Join 中测试空子查询

php - 使用 SELECT 选项从 PHP 插入外键值

ios - Swift 3/如何将 Root 的 UINavigationBar 与 UIViewController 的 UINavigationBar 重叠

android - Cordova - 检查 WIFI 连接到互联网