简体   繁体   中英

Extracting values from the url in iOS

my url is https://photos.googleapis.com/data/upload/resumable/media/create-session/feed/api/user/111066158452258/albumid/60281009241807 i want to extract the value of user & albumid , i had tried to extract with different methods which i found in stack overflow ,but they didn't work. Please help me out. Thank you for your precious time.

I give you an example here, NSURL class is your friend. You can use eg pathComponents : to get an array of all components and then process this array as you need it:

NSURL *url = [NSURL URLWithString:@"https://photos.googleapis.com/data/upload/resumable/media/create-session/feed/api/user/111066158452258/albumid/60281009241807"];
NSArray *components = [url pathComponents];
NSLog(@"path components: %@", components);  
NSLog(@"user: %@", components[9]);  
NSLog(@"albumid: %@", components[11]);  

You can take your NSURL (or init one from the URL string), and use the method pathComponents which return an array of the words in the URL (separated from the slash / ), so:

pathComponents[0] == @"photos.googleapis.com"
pathComponents[1] == @"data"
...etc.

Here the snippet of code:

NSURL *url = [NSURL urlWithString:@"https://photos.googleapis.com/data/upload/resumable/media/create-session/feed/api/user/111066158452258/albumid/60281009241807"];

NSString *user = url.pathComponents[9];
NSString *album = url.pathComponents[11];
   NSURL *url = [NSURL URLWithString:@"https://photos.googleapis.com/data/upload/resumable/media/create-session/feed/api/user/111066158452258/albumid/60281009241807"];
    NSArray *pathComponentsArray = [url pathComponents];
    NSString*userValue;
    NSString*albumidValue;
    for(int i=0;i<[pathComponentsArray count];i++)
    {
        if([pathComponentsArray[i] isEqualToString:@"user"])
        {
            userValue = pathComponentsArray[i+1];
        }

        if([pathComponentsArray[i] isEqualToString:@"albumid"])
        {
            albumidValue = pathComponentsArray[i+1];
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM