UISearchBar accepts only one word for searching youTube videos - objective-c

I have an app that basically search youTube videos.
This all youTube search and the result videos are in one view controller.
Now, everything works fine and i do get the wanted videos (using JSON) displayed on my view using this API: "https://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&q=%#"
The problems are:
1. UISearchBar accepts only one word to search with. if i put on the search bar two or more words, the app crashes. (if i don't want that the two words will make my app crash,
then i need to write on the searchBar, i.e "new%20songs" instead of "new songs").
so the question for here is: do i need to detect every space on the seachBar.text and replace it with "%20"?
or there is something more appropriate for that..?
another thing: language- my UISearchBar accepts only english! if i put another language on it-CRASH!
Do anyone have an explanation? i tried to change some of the API rules, but unsuccessfully.
EDIT: i'm not leaving here code, because i don't think it's the problem. the code works fine.. i think it's more like API problem, but of course, i'll post some code if needed.
Thanks to all !!!

To obtain the correct URL string you need to use stringByAddingPercentEscapesUsingEncoding method. So all spaces and characters (other than English) will be replaced by special codes (eg space will be replaced %20, etc.). Example:
NSString *searchStr = #"string with space";
NSString *youtubeURLString = [NSString stringWithFormat:#"https://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&q=%#", searchStr];
NSString *URLString = [youtubeURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Related

Mapkit Searchbar Autocomplete Ios8

I'm trying to implement an Autocomplete on a search bar on a map using Mapkit. I found this :
https://github.com/chenyuan/SPGooglePlacesAutocomplete
Works and is totally perfect except that it uses UISearchDisplayViewController which has been deprecated in ios8 and replaced by UISearchViewController. Is there a way around it or a simpler way than the one mentioned above?
Thanks in Advance
Please try this new repo: https://github.com/hkellaway/HNKGooglePlacesAutocomplete, which is being actively maintained.
Apple provides a full autocomplete for the entire English language (and others) but if you want to implement your own autocomplete it's not too difficult, you simply need the range of words or phrases that you want to suggest and a way of ranking them in order of frequency used.
I've implemented a simple autocomplete in one of my projects that centers around a PredictionString class and an AutopredictCoordinator class.
The PredictionString has a NSString property and a float property which relates to the strings frequency of use by the user. The AutopredictCoordinator then holds an array of prediction strings and responds to requests for the most likely completion of any given string.

Htmlcharacters in Textlabel

i have a UITableview, in the tableview i'm grabbing a JSON feed. But sometimes it's displaying htmlcharacters like “ instead of “ does anyone know how i can convert this in xcode?
Can i set a encoding for my textlabel?
[[cell textLabel] setText:[item objectForKey:#"title" ]];
When I'm fetching strings from a web server and if they're destined for any User Interface, I usually de-louse (errr, de-percent-escape them) via this "NSString" method:
stringByReplacingPercentEscapesUsingEncoding (documentation linked for you).
This gets rid of the URL encoded pieces of strings.
Now, as to the HTML characters like "&#8220", that requires an additional NSString category to do some work.
And other people have already written this category for you. Take a look at the various answers here and one of them should work well.

How to validate a textField in objective C (ios)

I am starting objective C and i want to make a form with text inputs. I have many Text Fields on my view and i want to check if the values written are good (only numbers, only text).
How can i do that? I found some documentation but not a lot... If anyone has a good ressource to help me to make forms.
I also want to know how to get all values in an array.
Thank you for help and sorry if my questions look terrible and easy.
I know ruby, php, javascript :)
Given a textfield called textfield1, you can access its string content via textfield1.text. This will yield a NSString-object which you can then analyze. You may use NSRegularExpression for this, or loop through 1-char-substrings or whatever you like, read up on NSString for this. A Google Search will also quickly yield you examples on how to use NSRegularExpressions.
Seeing that you're working for iOS, you might want to consider using special controls for your specific needs. For example, rather than using a textfield to request a number, and complain when the user inputs a non-number, present him a slider instead of a textfield.
For your second question, given an array array1 with NSString objects in it, you can loop through them by fast enumeration:
for (NSString* string in array1)
{
// do something with each string in array1
}

Parse Image and Extension From URL Objective C NSScanner

I have a program that will be grabbing a image from our server for the app, however we want to save the image onto the iOS app for caching purposes.
The url would be similar to this.
http://www.example.com/image/app_name/mypicture.png
I need a way to get the image name (mypicture) and the extension (.png) into 2 different strings to save it.
How would I accomplish this using a NSscanner?
Thanks
You wouldn't use a scanner. Use the NSURl method absoluteString to get the path and then use NSString methods pathExtension, stringByDeletingPathExtension and finally lastPathComponent to get the extension and name. You could also use the NSURL method resourceValuesForKeys:error: to get the name with the NSURLLocalizedNameKey.

In iOS, how can you programmatically fill out a pdf form field?

I need to take an existing pdf file and programmatically fill in a list of form fields with text and then save the pdf without ever displaying it to the user.
For instance, if the pdf file contains fields named "LastName", and "FirstName" I would like to set the value of "FirstName" to "Louis" and then save the file.
I've been searching for a long time and can't find any guidance on even where to start since the iOS documentation (and most of the questions on here) seem geared towards displaying or creating pdf content instead of modifying it.
EDIT:
My main question is: Is it possible to open a pdf stream (I know how to do this) and copy each existing pdf dictionary item into a new pdf? I have not been able to find a way to write the dictionary items to a pdf.
I doubt that kind of functionality will ever be in the iOS frameworks. The reason most of the related info you can find "seem[s] geared towards displaying or creating pdf content instead of modifying it" is because that's what the vast majority of use cases will want or need for PDF functionality.
You'll need to find a 3rd party library that can open up PDFs, fill out the AcroForm fields, and then stamp out a PDF. I use iText on Java (there is also iTextSharp for C#) but I don't know of anything for Objective-C.
Once you find that library, you'll need to integrate it into your project. There are undoubtedly several related questions/answers here on SO for whatever version of the SDK you're using.
This would be easier to do with a HTML page. If you wish to use a HTML page instead of a .pdf form then thius is how you would go about doing it:
[field1 setText:#"Field 1 Text"];
[field2 setText:#"Field 2 Text"];
NSString *result;
result = [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"$('#field1').val('%#');", field1.text]];
result = [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"$('#pfield2').val('%#');", field2.text]];
result = [webView stringByEvaluatingJavaScriptFromString:#"$('#submitbutton').click();"];
You would need to create two UILabels or UITextFields and call them "field2" and "field2" in your .h file. You can then find the ID of the field you need to replace e.g. #field1 and then put it where I have put "#field1" and again for the second field where I have put "#field2". There also needs to be a UIWebView with the page already loaded. This code is to be used after the UIWebView page has been loaded. Maybe do the following:
-(void)webViewDidFinishLoad:(UIWebView *)webView {
// Insert above code here
}
You probably need a full understanding of Javascript if you want to do this for the whole form, but this should get you started.
Hope that helps you.