-1

如何将一些数据从 iOS 应用程序中的字符串发送到我的服务器,PHP 脚本将这些数据写入数据库?

4

5 回答 5

10

所以最后,我有一个代码,它有效!

NSString *content = @"field1=42&field2=Hello";

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.example.com/form.php"]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[content dataUsingEncoding:NSUTF8StringEncoding]];

// generates an autoreleased NSURLConnection
[NSURLConnection connectionWithRequest:request delegate:self];
于 2013-05-05T12:58:34.480 回答
1

By converting this string in UTF8Encoding you can send it.

于 2013-04-29T09:36:17.433 回答
0

试试这个代码

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody: jsonData];
    [request setValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];  
于 2013-04-29T08:41:24.937 回答
0

看看 Apple 的 NSURLConnection 教程:

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html

于 2013-04-29T08:42:06.353 回答
0
[self deviceCheck:@"123" Completetion:^(NSArray *result, NSError *error) {
   //Here use result,and check the error
}];

//Method
-(void)deviceCheck:(NSString *)device Completetion:(void (^) (NSArray * result,NSError * error))completion{

NSString *deviceRequestString = [NSString stringWithFormat:@"%@?device=%@",webservice,device];

NSURL *JSONURL = [NSURL URLWithString:deviceRequestString];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:JSONURL];
NSURLSessionDataTask * dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request
                                completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                                    if(data == nil){
                                        completion(nil,error);
                                        return;
                                    }
                                    NSError *myError;
                                    NSArray *tableArray = [[NSArray alloc]initWithArray:[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&myError]];
                                    completion(tableArray,myError);
                                }];
[dataTask resume];
}
于 2015-12-08T07:37:00.390 回答