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

时间:2022-09-02 10:45:59

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 urlbase is a url to a REST site.

urlbase是REST站点的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位置?

5 个解决方案

#1


31  

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身份验证。

#2


10  

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 AFNetworking or Apple's own NSURLConnection.

我是iOS开发的新手,在过去一个月左右的时间里,我一直在与本页面列出的一些大框架作斗争。这是一场噩梦。老实说,我建议您坚持使用AFNetworking或Apple自己的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 homepage alone 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 NSDictionary to 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 NSURL or string to your custom enum.

更新:Mantle框架(由Github Inc.开源)设计精良且易于使用。它处理对象映射:将JSON NSDictionary转换为您自己的自定义Objective-C模型类。它可以合理地处理默认情况,并且很容易推出自己的价值变换器,例如:从字符串到NSURL或字符串到您的自定义枚举。

#3


6  

I have a couple of apps using a framework called Objective Resource which 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基本身份验证。

#4


2  

Just stumbled on this question - you might find LRResty pretty 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,块等,请参阅:GitHub for source(MIT许可证)。我现在正在尝试它 - 它也有一个示例应用程序。

#5


1  

I've used ASIHTTP in 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 Basic Auth。尝试从标准浏览器中访问相同的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 addRequestHeader method on ASIHTTPRequest to add any specific headers the server expects.

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

#1


31  

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身份验证。

#2


10  

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 AFNetworking or Apple's own NSURLConnection.

我是iOS开发的新手,在过去一个月左右的时间里,我一直在与本页面列出的一些大框架作斗争。这是一场噩梦。老实说,我建议您坚持使用AFNetworking或Apple自己的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 homepage alone 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 NSDictionary to 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 NSURL or string to your custom enum.

更新:Mantle框架(由Github Inc.开源)设计精良且易于使用。它处理对象映射:将JSON NSDictionary转换为您自己的自定义Objective-C模型类。它可以合理地处理默认情况,并且很容易推出自己的价值变换器,例如:从字符串到NSURL或字符串到您的自定义枚举。

#3


6  

I have a couple of apps using a framework called Objective Resource which 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基本身份验证。

#4


2  

Just stumbled on this question - you might find LRResty pretty 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,块等,请参阅:GitHub for source(MIT许可证)。我现在正在尝试它 - 它也有一个示例应用程序。

#5


1  

I've used ASIHTTP in 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 Basic Auth。尝试从标准浏览器中访问相同的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 addRequestHeader method on ASIHTTPRequest to add any specific headers the server expects.

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