Search code examples
iosnsurlnsurlrequest

Change a absolute URL to relative URL use system API quick and clean?


Can I use the system API to quickly do this job? I know I can use regex or combination path and query manually. I know NSURLComponents,but can it change an absolute URL to a relative one?

I'm just curious, can we use the system API to do this quickly and clean?


Solution

  • Currently I use the follow snippet:

    NSURL *url;
    
    NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
    urlComponents.scheme = nil;
    urlComponents.host = nil;
    
    NSURL *relativeURL = urlComponents.URL;