Search code examples
iosobjective-cjsonflickr

Flickr Search Photo objective-c nothing return


I have a problem with Flickr API. I have created URLString

+ (NSString *)URLForSearchString:(NSString *)searchString {
NSString *APIKey = @"*****";
NSString *search = [searchString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
return [NSString stringWithFormat:@"https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=%@&tags=%@&per_page=25&format=json&nojsoncallback=1", APIKey, search];}

And then, When I touched search button I called the request with NSURLSession.

- (void)searchFlickrPhotos:(NSString *)text {
NSString *urlString = [FlickrHelper URLForSearchString:@"Nature"];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString: urlString]];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:[UIDevice currentDevice].name forHTTPHeaderField:@"device"];
[request setTimeoutInterval:15];

NSURLSession *session;
session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

NSURLSessionDataTask * sessionDataTask = [session dataTaskWithRequest: request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    NSDictionary *temp = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    dispatch_async(dispatch_get_main_queue(), ^{
        //For UI updates in main thread
    });
}];
[sessionDataTask resume];}

I can't get the response from the server. My temp dictionary is always nil... Would you write some detail solution? I would be very grateful for the help!


Solution

  • In order to solve this, first

    1. Check whether data is nil.
    2. If it's not nil, then use the following LOC to see the output on the console

      NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
      

      NSLog(@"RESPONSE: %@", str);

    Now, you should be able to get another insight on what's actually happening.