0

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.

Binarian
  • 12,296
  • 8
  • 53
  • 84
openfrog
  • 40,201
  • 65
  • 225
  • 373
  • http://stackoverflow.com/questions/3692947/get-parts-of-a-nsurl-in-objective-c – Maul Oct 19 '13 at 11:09
  • Why do you actually want the ID of this URL? The `assets-library` scheme is private to Apple, and free for them to change as they go along. Trying to extract the ID is fragile – Mike Abdullah Oct 21 '13 at 11:22

2 Answers2

3

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.

LorikMalorik
  • 2,001
  • 1
  • 14
  • 14
0

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
RFG
  • 2,880
  • 3
  • 28
  • 44