openfrog
openfrog

Reputation: 40735

How to get the ID portion of this NSURL?

I have an NSURL that looks like this when printed to console:

<AVURLAsset: 0x170232800, URL = assets-library://asset/asset.MOV?id=426C5913-86CF-4476-2DB6-6A9AAC626AF4&ext=MOV>

What is the proper way to get to the id portion of this URL? Goal is to have a string with 426C5913-86CF-4476-2DB6-6A9AAC626AF4.

Upvotes: 0

Views: 291

Answers (2)

RFG
RFG

Reputation: 2902

Begin with:

NSURL *urlAddress = [NSURL URLWithString:yourAddressString];

Then:

[urlAddress scheme] gives the Scheme;
[urlAddress host] gives the host
[urlAddress path], [urlAddress relativePath], [urlAddress query], [urlAddress fragment] or [urlAddress parameterString] gives the correspondent parts of your urlAddress

Upvotes: 0

LorikMalorik
LorikMalorik

Reputation: 2019

You may use something like this:

NSURL *url = [[NSURL alloc] initWithString:@"assets-library://asset/asset.MOV?id=426C5913-86CF-4476-2DB6-6A9AAC626AF4&ext=MOV"];
NSArray *components = [[url query] componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"=&"]];
NSMutableDictionary *params = [NSMutableDictionary dictionary];
for (NSUInteger idx = 1; idx < components.count; idx+=2) {
    params[components[idx - 1]] = components[idx];
}

NSLog(@"params[@id] = %@", params[@"id"]);

It is a good idea to create a category of NSURL for this purpose.

Upvotes: 3

Related Questions