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 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)