Search code examples
iosafnetworkingnsurl

deal with special characters in NSURL


i have this code to url of image , i have little problem with my url it contain \u and xcode consider it as special character So i escape it by adding \u in my url but when i pass it

fullPath = [NSString stringWithFormat:@"http:\\www.school-link.net\\uploads\\%@",image_url];
    NSLog(@"file path  %@",fullPath);
    //i try escape space by this code it dosnt work 
   //fullPath = [fullPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [[NSURL alloc]initWithString:fullPath];
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];



 AFHTTPRequestOperation *posterOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    posterOperation.responseSerializer = [AFImageResponseSerializer serializer];
    [posterOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Response: %@", responseObject);
        image_view.image = responseObject;

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Image request failed with error: %@", error);
    }];
    [[NSOperationQueue mainQueue] addOperation:posterOperation];
    [posterOperation start];

it give me error , any ideas thank you all

image request failed with error: Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x8f9f900 {NSUnderlyingError=0x9a94cf0 "bad URL", NSLocalizedDescription=bad URL}


Solution

  • Your URL is malformed. Right now you have:

    http:\\www.school-link.net\\uploads\\%@
    

    Those backslashes should instead be forward slashes. Ex:

    http://www.school-link.net/uploads/%@
    

    Now you can append image_url to the URL, and presuming image_url doesn't cause the URL to be malformed, your request will go through.