ios - 发送帖子请求IOS 7
<p><p>好的,我一直在努力适应 objective-c ,这对我来说就像过山车一样,无论如何我只是想向服务器发送一个简单的 post 请求,但它看起来并不像原来那么简单安卓。目前我有以下方法:</p>
<pre><code>-(void)postMethod
{
;
//initialize new mutable data
NSMutableData *data = [ init];
self.receivedData = data;
//initialize url that is going to be fetched.
NSURL *url = ;
//initialize a request from url
NSMutableURLRequest *request = ];
//set http method
;
//initialize a post data
NSString *postData = @"username=postTest&displayname=testingPoster&password=postest&passwordc=postest&email=posttesting%40posttest.com&bio=I+am+tesring+the+post+test&skills=I+am+a+hacka&interests=hacing+and+cracking&skills_needed=some+php+skills&company=Post+testing+the+script&dob=naaaaaa&occupation=Qoekkk";
//Here you can give your parameters value
//set request content type we MUST set this value.
;
//set post data of request
];
//initialize a connection from request
NSURLConnection *connection = [ initWithRequest:request delegate:self];
self.connection = connection;
//start the connection
;
}
</code></pre>
<p>我有一个按钮可以调用该函数:</p>
<pre><code>- (IBAction)register:(id)sender {
;
}
</code></pre>
<p>我确实确保在我的头文件中包含了 NSURLConnectionDataDelegate,但是当我单击按钮时尝试发送请求时,它没有通过,它只是停止程序,它没有任何错误。任何建议,我一直在网上搜索只是为了学习如何发送帖子请求,我认为这应该不是很困难。 </p>
<p>只是以防万一,我正在使用最新的 iphone sdk for ios 7。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>请注意,对于 iOS 7 <a href="https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSession_class/Introduction/Introduction.html" rel="noreferrer noopener nofollow">NSURLSession</a>是推荐的方式。 NSURLConnection 可能会在未来的版本中被弃用。您可以使用 <a href="https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSessionTask_class/Reference/Reference.html" rel="noreferrer noopener nofollow">URL session tasks</a> 的三种类型之一发出您的 POST 请求。 . <a href="https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSessionDataTask_class/Reference/Reference.html" rel="noreferrer noopener nofollow">NSURLSessionDataTask</a>几乎等同于 NSURLConnection。 <a href="https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSessionUploadTask_class/Reference/Reference.html" rel="noreferrer noopener nofollow">NSURLSessionUploadTask</a>和 <a href="https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSessionDownloadTask_class/Reference/Reference.html" rel="noreferrer noopener nofollow">NSURLSessionDownloadTask</a> (将接收到的数据写入磁盘)可以在后台 session 中使用并继续(在另一个进程中),即使您的应用程序被挂起或崩溃。更多内容请阅读 <a href="https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/UsingNSURLSession.html" rel="noreferrer noopener nofollow">this guide</a> .</p></p>
<p style="font-size: 20px;">关于ios - 发送帖子请求IOS 7,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/21213994/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/21213994/
</a>
</p>
页:
[1]