xcode Xcode通过php中的post函数从文本输入发送数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3746890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode send data from text input through a post function in php
提问by user372671
Is it possible to send text from an xcode input and have the data sent through a php function?
是否可以从 xcode 输入发送文本并通过 php 函数发送数据?
回答by Alex Nichol
If you actually want to do this using simple Objective-C (Foundation) classes, you should use NSURLConnection. Example:
如果你真的想使用简单的 Objective-C(基础)类来做到这一点,你应该使用 NSURLConnection。例子:
NSString * post = [[NSString alloc] initWithFormat:@"&myvariable=%@", myString];
NSData * postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
NSString * postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest * request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.yoursite.com/file.php"]]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn) NSLog(@"Connection Successful");
回答by Brad
You can use this to post data from your xCode app to a web server.
您可以使用它来将数据从 xCode 应用程序发布到 Web 服务器。