Objective-C:在 iPhone 上访问 REST API 的最佳方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1557040/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:20:43  来源:igfitidea点击:

Objective-C: Best way to access REST API on your iphone

iphoneobjective-cweb-servicesrest

提问by Doz

been wrestling with this for some time. I am trying to access a REST api on my iphone and came across the ASIHTTP framework that would assist me. So i did something like

一直在努力解决这个问题。我试图在我的 iphone 上访问 REST api 并遇到了可以帮助我的 ASIHTTP 框架。所以我做了类似的事情

//call sites, so we can confirm username and password and site/sites
NSURL *url = [NSURL URLWithString: urlbase];
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setUsername:@"doronkatz%40xx.com" ];
[request setPassword:@"xxx"];

Where urlbaseis a url to a REST site.

urlbaseREST 站点的 url在哪里。

Now, a developer has told me there might be an issue or bug with this framework, and its not passing headers correctly. Is there another way of testing or accessing with authentication a network REST location?

现在,一位开发人员告诉我这个框架可能存在问题或错误,并且它没有正确传递标头。是否有另一种测试或通过身份验证访问网络 REST 位置的方法?

回答by Blake Watters

I would recommend checking out RestKit: http://restkit.org/It provides an excellent API for accessing RESTful web services and representing the remote resources as local objects, including persisting them to Core Data. HTTP authentication is supported out of the box.

我建议查看 RestKit:http://restkit.org/它提供了一个出色的 API,用于访问 RESTful Web 服务并将远程资源表示为本地对象,包括将它们持久化到 Core Data。开箱即用地支持 HTTP 身份验证。

回答by bcattle

I'm new to iOS development and I've been battling with some of the big frameworks listed on this page for the past month or so. It has been a nightmare. I'd honestly recommend you just stick to the basics and do it yourself using AFNetworkingor Apple's own NSURLConnection.

我是 iOS 开发的新手,在过去一个月左右的时间里,我一直在与此页面上列出的一些大型框架作斗争。这是一场噩梦。老实说,我建议您坚持基础知识并使用AFNetworkingApple 自己的NSURLConnection.

One of the libraries listed is no longer maintained. Another underwent huge code-breaking API changes recently and now half of the tutorials describing its use no longer work. Others are massively bloated.

不再维护列出的库之一。另一个最近经历了巨大的代码破坏 API 更改,现在描述其使用的教程中有一半不再有效。其他人则严重臃肿。

It's easier than you'd think. Some resources that helped me:

这比你想象的要容易。一些对我有帮助的资源:

The examples on the AFNetworking homepagealone may get you 80% of the way there.

AFNetworking 主页上的示例就可以帮助您完成 80% 的工作。

UPDATE: The Mantle Framework(open sourced by Github Inc.) is well-designed and easy to use. It handles object mapping: converting a JSON NSDictionaryto your own custom Objective-C model classes. It handles default cases sensibly and it's pretty easy to roll your own value transformers, e.g. from string to NSURLor string to your custom enum.

更新Mantle Framework(由 Github Inc. 开源)设计精良且易于使用。它处理对象映射:将 JSON 转换NSDictionary为您自己的自定义 Objective-C 模型类。它明智地处理默认情况,并且很容易滚动您自己的值转换器,例如从字符串到NSURL或字符串到您的自定义enum.

回答by paulthenerd

I have a couple of apps using a framework called Objective Resourcewhich provides a wrapper for accessing remote REST based api's. It is aimed primarily at Ruby on Rails based applications so it's XML/JSON parsing may be tuned to handle some Rails defaults but it is worth looking at. It supports http basic authentication by default.

我有几个应用程序使用名为Objective Resource的框架,它提供了一个用于访问基于 REST 的远程 api 的包装器。它主要针对基于 Ruby on Rails 的应用程序,因此它的 XML/JSON 解析可以调整为处理一些 Rails 默认值,但值得一看。默认支持http基本认证。

回答by petert

Just stumbled on this question - you might find LRRestypretty interesting as it uses NSOperation, blocks etc., see: GitHub for source (MIT license). I'm experimenting with it now - it has a sample app too.

刚刚偶然发现了这个问题 - 您可能会发现LRResty非常有趣,因为它使用 NSOperation、块等,请参阅:GitHubfor source(MIT 许可证)。我现在正在试验它 - 它也有一个示例应用程序。

回答by Ramin

I've used ASIHTTPin two apps so far and have had no problems.

到目前为止,我已经在两个应用程序中使用了ASIHTTP,并且没有出现任何问题。

Looks like you're doing HTTP Basic Auth with the remote site. Try hitting the same REST URL from a standard browser and pass the params you need down to it. It should prompt you for username/password. If it makes it through, then at least you know the server-side is set up to handle requests. If it doesn't, then you need to have a talk with the dev.

看起来您正在对远程站点进行 HTTP 基本身份验证。尝试从标准浏览器访问相同的 REST URL,并将您需要的参数传递给它。它应该提示您输入用户名/密码。如果它通过,那么至少您知道服务器端已设置为处理请求。如果没有,那么您需要与开发人员交谈。

The next thing to try is put a Mac-based network sniffer and see what headers are going back and forth. Any of HTTPScoop, Wireshark, or Charles should work. Run the sniffer as a network proxy then run your app in the simulator and watch what goes across. Try it again with the browser. Once you see the differences, you can use the addRequestHeadermethod on ASIHTTPRequestto add any specific headers the server expects.

接下来要尝试的是放置一个基于 Mac 的网络嗅探器,看看有哪些标头来回传输。任何 HTTPScoop、Wireshark 或 Charles 都应该可以工作。将嗅探器作为网络代理运行,然后在模拟器中运行您的应用程序并观察发生了什么。用浏览器再试一次。一旦看到差异,就可以使用addRequestHeaderon 方法ASIHTTPRequest添加服务器期望的任何特定标头。