Search code examples
iosnsurlconnectionnsurlnsurlrequestnsmutableurlrequest

NSMUtableURLRequest requestWithURL returns null whenever URL contains Japanese parameters


Whenever the URL contains Japanese Parameters, requestWithURL function returns null.

urlString = https://translate.google.co.in/#ja/en/はははは
NSMutableURLRequest *Request= [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];

Whenever the Parameters include EN, requestWithURL works fine.


Solution

  • You have to encode your path, since it contains characters not allowed in an URL:

    NSString *base = @"https://translate.google.co.in";
    NSString *path = @"/#ja/en/はははは";
    
    NSURLComponents *urlComponents = [NSURLComponents componentsWithString:base];
    urlComponents.path = path;
    
    NSMutableURLRequest *request= [NSMutableURLRequest requestWithURL:urlComponents.URL];
    

    Note that the request still won't give you the desired result, since in a browser you do a request to https://translate.google.co.in/ with ja/en/はははは as a fragment identifier for JavaScript, while in the code you make an request to https://translate.google.co.in/#..., which doesn't exist.