如何在iphone(ios)服务器上上传和下载数据
我正在开发名为“Messenger”的应用程序。
我的任务是从服务器上传和下载数据.. 我对此很陌生,所以我不知道该怎么做。
- 我想与服务器共享数据(上传)
- 我想从服务器获取数据(下载)
谁能帮我解决这个问题? 我需要这方面的代码。
提前致谢 ..
I am working on application called "Messanger".
My task is to upload and download data from and to a server ..
I am new to this, so I don't know how to do it.
- I want to share data with server (upload)
- I want to get data from server (download)
Can anyone help me with this ??
I need code for this.
Thanks in Advance ..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以使用官方sdk中提供的类。
0) 公共部分。
首先,您应该创建
NSURLRequest
。例如,NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"apple.com"]];
1) 上传数据。
当您想要发送一些数据时,您可以通过以下方式使用它(例如,发送xml):
2)下载数据。
现在您应该开始连接:
3)实现必要的委托方法。最后,您应该实现您需要的委托方法。例如,在方法
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
中,您将合并接收到的数据,在方法- (void)connectionDidFinishLoading:(NSURLConnection * )connection
和- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
您应该处理收到的数据。有关您可以实施以接收有关互联网连接的更多信息并处理不同流程(如身份验证、响应代码等)的所有方法的完整列表,请阅读官方文档:NSURLConnection
You can use classes provided in official sdk.
0) Common part.
First of all you should create
NSURLRequest
. For example,NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"apple.com"]];
1) Uploading data.
When you want to send some data you can use this in following way (for example, sending xml):
2) Downloading data.
Now you should start connection:
3) implement needful delegate methods. And at last you should implement that delegate methods that you would need. For example, in method
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
you will merge received data, in methods- (void)connectionDidFinishLoading:(NSURLConnection *)connection
and- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
you should process received data.For complete list of all methods that you could implement to receive more information about your internet connection and handle different process (like authentication, response code and other) read official documentation : NSURLConnection
您可以使用 ASIHttpRequest 库。例子很多,希望对你有帮助
You can use the ASIHttpRequest library. There are a lot of examples, hope it will help you