0

I have the following NSSURL:

myURL - myPlatform://sdk/functionName?key1=value1&key2=value2

How can I get the URL components?

NSString *myPlatform = [myURL...?
NSString *functionName = [myURL...?
NSString *key1 = [myURL...?
NSString *value1 = [myURL...?
NSString *key2 = [myURL...?
NSString *value2 = [myURL...?

I have this code:

- (BOOL)webView:(UIWebView *)webView2 shouldStartLoadWithRequest:(NSURLRequest *)request     navigationType:(UIWebViewNavigationType)navigationType {
    NSURL *url= [request URL];
    Log(@"url: %@", url);
    NSArray *components = [url pathComponents];  
    for (NSString *c in components)
        Log(@"URL component: %@", c);
}

And this in the log: url:

url: myPlatform://sdk/functionName?key1=value1&key2=value2

component: /

component: functionName

Thanks in advance

Community
  • 1
  • 1
Luda
  • 7,282
  • 12
  • 79
  • 139

1 Answers1

1

It's in the documentation:

NSString *myPlatfrom = [myURL scheme];
NSString *sdk = [myURL host];
NSString *functionName = [[myURL path] substringFromIndex:1];
NSString *query = [myURL query];
for (NSString *arg in [query componentsSeparatedByString:"&"]) {
    NSArray argComponents = [arg componentsSeparatedByString:"="];
    NSString key = [argComponents[0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString value = [argComponents[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200