duanqianruan8448 2017-01-09 01:34
浏览 77

使用xcode 8(Objective-c)将Textfield值发送到PHP MySQL中是否有点击操作?

I just want to ask on how to send textfield value into mysql database in xcode using obj c without any click action or with click action?

If the code below can retrieve and display JSON data from web server into 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

How to post username, email value into mysql database? Is it the same technique applied on post method? Because I do not want to use SBJson classes (if possible).

  • 写回答

2条回答 默认 最新

  • duanhui4160 2017-01-09 02:29
    关注

    There are several ways to do it. First, it is important to notice that dataWithContentsOfURL is not an asynchronous request. Meaning that if you use it to transfer large data, there is a good chance that you will freeze the app. For async requests, you should use the NSURLRequest.

    Having said that, there are excellent API to upload/download data asynchronously. One which is very frequently used, and well documented is AFNetworking. This is coded on top of NSURLRequest.

    For example, in your PHP you can retrieve the fields from a POST statement like this:

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

    In your app, you can call the PHP script with a POST request in AFNetworking as follow:

    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];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型