0

Hey folkks I'm working with an api that search about movies now the problem is when i type single word in UISearchBar it works but when I type space for another word it dose'nt work.Near query=%@ when i type single word it works but when I type another word with space it won't

NSString *movieName=searchBar.text;
NSString *movieString = [NSString stringWithFormat:@"https://api.themoviedb.org/3/search/movie?query=%@&api_key=c4bd81709e87b1209433c49",movieName];
NSURL *url=[NSURL URLWithString:movieString];
Rajat
  • 10,977
  • 3
  • 38
  • 55
  • 1
    I think you should replace space with %20 or + – Jageen Nov 04 '16 at 07:30
  • 1
    Possible duplicate of [String replacement in Objective-C](http://stackoverflow.com/questions/668228/string-replacement-in-objective-c) – l'L'l Nov 04 '16 at 07:31

5 Answers5

2

You don't need to replace Space with Dash you new to URL Encode your string

Below is an example of encoding your string to URL Encode

NSString *movieName=searchBar.text;
movieName = [movieName stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLUserAllowedCharacterSet]];
NSString *movieString = [NSString stringWithFormat:@"https://api.themoviedb.org/3/search/movie?query=%@&api_key=c4bd81709e87b1209433c49",movieName];
NSURL *url=[NSURL URLWithString:movieString];
Rajat
  • 10,977
  • 3
  • 38
  • 55
  • @Jageen Yes Previous code will also encode https://, updated my answer, now it will encode only Movie Name – Rajat Nov 04 '16 at 07:39
  • 1
    Important: You should do that for *any* parameters you add to your URL. Even if they can't contain spaces, there are other characters that may not occur in a URL. – uliwitness Nov 04 '16 at 08:58
0

Use

formattedString = [originalString stringByReplacingOccurrencesOfString:@" " withString:@"-"];
Windindi
  • 412
  • 4
  • 11
0

Please refer this code.

NSString *movieName=searchBar.text;
NSString* replacedMovieName = [movieName stringByReplacingOccurrencesOfString:@" " withString:@"_"];

Hope this helps.

nishith Singh
  • 2,968
  • 1
  • 15
  • 25
0

No need replace space with dash, need to add Percent Escapes in string before creating NSURL.

Use NSString class method:

-(NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding

Like,

NSString *movieName=searchBar.text;

//This is what you need
movieName=[movieName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSString *movieString = [NSString stringWithFormat:@"https://api.themoviedb.org/3/search/movie?query=%@&api_key=c4bd81709e87b1209433c49",movieName];

NSURL *url=[NSURL URLWithString:movieString];

OR

Alternately you can do following(Not best approach):

NSString *movieName=searchBar.text;

//This is what you need
movieName = [movieName stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

NSString *movieString = [NSString stringWithFormat:@"https://api.themoviedb.org/3/search/movie?query=%@&api_key=c4bd81709e87b1209433c49",movieName];

NSURL *url=[NSURL URLWithString:movieString];
Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51
0
NSString *movieName=searchBar.text;
movieName = [movieName stringByReplacingOccurrencesOfString:@" " withString:@"-"];//use vice-versa
NSString *movieString = [NSString stringWithFormat:@"https://api.themoviedb.org/3/search/movie?query=%@&api_key=c4bd81709e87b1209433c49",movieName];
NSURL *url=[NSURL URLWithString:movieString];