Searching an API in Xcode - iphone

I am developing an iPhone application that is using an API to find recipes from http://www.recipepuppy.com/about/api/.
There are three parameters that must be put into return results. There are i,q, and p, Where i are the ingredients, q in a normal search query and p is the page #. I am able to specifically add these parameters and then load the results into a table view in Xcode.
I also want to implement a search that allows the users to search for recipes based on whatever they feel like and return the results. Could someone point me in the correct direction on how to do this. I know I will have to take what ever the user inputs and place it into a string but how do I implement that string into the parameters of the URL?

To answer your question:
I know I will have to take what ever the user inputs and place it into a string but how do I implement that string into the parameters of the URL?
You can use the stringWithFormat method of NSString. For example:
NSString *ingredients = #"ingredients";
NSString *query = #"soups";
NSString *page = #"1";
NSString *url = [NSString stringWithFormat:#"http://www.recipepuppy.com/api/?i=%#&q=%#&p=%#",ingredients,query,page];
Before using this URL, it's recommended that you URL encode it.
NSString *encodedURL = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Now that you have your URL, just start a connection to the web (NSURLConnection, AFNetworking, the choice is yours), parse the data returned, and load that into an array to display in the table view.

To build a solid app that involves JSON communication over the network, you can use the JSONModel library. It provides you with data models, and takes care of sending and receiving JSON forth and back to your API service.
You can have a look at the GitHub page:
https://github.com/icanzilb/JSONModel/
Or also to this example on how to use the YouTube JSON API with JSONModel:
http://www.touch-code-magazine.com/how-to-make-a-youtube-app-using-mgbox-and-jsonmodel/
On the GitHub page there are more code samples. Good luck :)
(Disclosure: I'm JSONModel's author)

I'd use AFNetworking for all your network requests:
https://github.com/AFNetworking/AFNetworking
To make the request just construct an NSString and send it off
[NSString stringWithFormat:#"http://www.recipepuppy.com/api/?i=%#&q=%#&p=%d", self.ingredients, self.query, self.page]

Related

Post Image and profile details to server from IPhone

As per requirement I need to post user profile details like user name, first name, last name and profile picture from IPhone device. How can we send these details including “profile picture” together? Do I need to send these details as a JSON object? So I need to convert the image as byte array. Right?
If you can provide any IPhone code snippet then it would be better.
Also if possible please provide the code for WCF Rest service Part.
Thanks in advance.
To send data to the server in JSON format:
ADD:
JSON.h (Header)
JSON.m (Implementation)
Use
https://github.com/stig/json-framework/download
To convert your profile details, you can refer following sample
NSMutableDictionary *jsonEncode = [NSMutableDictionary dictionary];
[jsonEncode setValue:#"" forKey:#""];
NSString *jsonString = [jsonEncode JSONString];
To send Image to server you need to convert image into Base64 format.
Refer link below:
UIImage to base64 String Encoding

iOS Twitter Entities on search API

I'm integrating some Twitter functions on my app. So basically what i want to do is to retrieve tweets based on some keywords (#hashtags) and from those extract if present pictures and present them to the user.
The problem is that on "GET Search" query such as "http://search.twitter.com/search.json?q=Twitter%20API&result_type=mixed&count=5" entities are not supported so I'm wondering how can I achieve my task.
I thought to get all the tweets with that query and then for each tweet do this call "GET statuses/show/:id" so i can get the entities for each tweet- get the link in the tweet text - and then make another call to a service where tweet pictures are stored and shared but it seems inefficient, it needs a lot of connection.
Just % escape the #. In a URL %23 is a #.
This query will look for tweets tagged #iphone.
http://search.twitter.com/search.json?q=%23iphone&result_type=mixed&count=5
To escape the string do this
NSString url = #"url with #hashtag";
NSString *escapedString = [url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
That will replace the # with %23.
At that time Twitter Search API didn't support entities, now it works properly

iPhone: How do I securely transfer data from device to server

could someone please assist me, Im a newbie and haven't done this before.
I have an iPhone app which has a "cart" object. Once the user has made his selections, I want to send that info in an XML file to the server. I read that the iPhone libraries let you convert data into XML easily. But from what I understand the data has to be stored in an array or a dictionary. Is this true? For example, my "Cart" is an object that uses a array to store data internally but the "cart" itself has variables which are not stored in an array or dictionary.
Q) How can I convert the entire "cart" into something that can be transported as XML to my server?
Also, I am asking the customers to create a profile for each order but this will be done by Launching UIWebview directly on the web through an https connection. So no credit card info will be on the device.
Q) Whats the best way to link the profile on the web and the order which is on the device?
Could someone who has come across this issue please give me tips or links?
Thanks
Sending your data securely could be done with ssl.
Converting your cart to XML could be done with an XML Framework/lib, still you'll have to write the code for that. For example touchXML:
TouchXML is a lightweight replacement
for Cocoa's NSXML* cluster of classes.
It is based on the commonly available
Open Source libxml2 library.
Here is a nice tutorial.
Source has moved a bit, can be found here
To your second question: This is tricky and may get hacky. However if you don't want to switch to an API based way to login/create the account I'll have these ideas.
Check the "result" of webView with the UIWebViewDelegate protocol with the webViewDidFinishLoad: method. A "result" may be: successful creation of an account or successful login.
So you can access the body of the page with the NSURLRequest property of the webview. Or use something like this, using javascript:
NSString *html = [webView stringByEvaluatingJavaScriptFromString: #"document.body.innerHTML"];
You will have to do parsing though to look for something, for example a token which you can connect with the order for final checkout.
You also could have generated a unique order string on the device in the first place and sent it over initially for login/creation of profile to increase security a bit and pass it back for a check.
Part one: create a dictionary that represents the cart. For each variable, add a key with its variable name as the key, and the cart object's value for that variable as the object for that key.
Part two: that's a very open-ended question :-). That depends on how you identify the user at both ends; though of course that will depend on your security requirements. One option is to require your user logs in via the web site when they first launch the device, then store their user ID on the app on a particular device (preferably in a confidential fashion). Don't use that for any reason other than to track which user you think is at the device: authenticate again before each purchase, or other sensitive actions like viewing or changing account details. By the way depending on the way your ordering system works, you may prefer (or be required by Apple) to implement in-app purchase. That would actually take a lot of the complexity away from the problem, at the cost of Apple's 30% processing fee.
If you have specific questions about the security concerns of such a system, you would do well asking at security.stackexchange.com (I'm one of the pro tem moderators over there).
If you send an encrypted string to a URL like in the code below, then handle it in an ASPX page (.NET example) or you can use other languages on the server side. Then, for security, simply ignore anything that doesn't decrypt (basic encryption-decryption not shown)
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL
URLWithString:#"http://myzuresite.azurewebsites.net/test3.aspx?bigstring=dgrbthymgk"]];
[request setHTTPMethod:#"POST"];
[NSURLConnection connectionWithRequest:request delegate:self];}
Then, in the http://myazuresite.azurewebsites.net/test3.aspx page:
<%# Page Language="C#"%>
<html>
<head>
<title>Query Strings in ASP.NET: Page 2</title>
<script language="C#" runat="server">
</script>
</head>
<body>
<%
// retrieves query string values
string bigstring = Page.Request.QueryString["bigstring"];
System.Data.SqlClient.SqlConnection sqlConnection1 =
new System.Data.SqlClient.SqlConnection("Data Source=myazuresite.database.windows.net;Initial Catalog=db_name;Integrated Security=False;Persist Security info=False;User ID=your_id;Password=your_Password”);
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "INSERT Scores (col1) VALUES (' "+name+" ')";
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
%>
</body>
</html>
I handle the decryption in an insert trigger in SQL Server, ignore bad URL's, and this is VERY secure. I use encryption that includes time in some way.

how to pass function name in url while using some web service in objective c?

I am a beginner in i phone development.I am,right now, using web service to fetch data on my page.now the thing is that i have 2 functions in the same webpage. now i want to fetch data from only 1 function at the time when my url is passed. so how can i pass the argument? note that,my main purpose is to convert data into json format.my URL that i run in web browser is like :
http://localhost/abc/webservices/mainPage.asmx?op=GetEmpDetail
how can i change its format,so that i can use it in objective c?my function name is getEmpDetail.
Thank you in advance.
Not Sure but try this out.
nsurl *url =http://localhost/abc/webservices/whatever/GetEmpDetail?Param1=value1&Param2=value2
url = [url stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

How to get my last TWEET into an NSString in my iPhone app?

There are a bunch of tutorials online about how to use xmlparsers or what not to bring an entire twitter feed into a UITableView. Thats not what I need. I only want ONE tweet. The most recent twitter update.
So, would some of you geniuses please show me in detail how to get my last (most recent) TWEET into an NSString in my iPhone app?
In short: exactly the same as all those tutorials that you've read except you pass the count parameter to the statuses/user_timeline REST method.
Ohh, this is complicated, not so easy, I show you the steps:
Make the request to the api (synchronous or asynchonous)
Handle the Authentication Challenge for authenticated request
Get the data, call the parse method of the parser
Handle the delegation methods of the nsxmlparser
Manually handle the DidStartElement, DidFoundCharacters, DidEndElement to get the first status you want, assign the string value to a variable when it founds the characters.
That's all you need to do ;)
Good luck.