Search code examples
iosnsstringnsurl

parsing a URL link into segments


http://180.160.1.140/webapp/camera?id=fksmf84-8493-45u3

How to get this url part fksmf84-8493-45u3?? -- this part is keep changing on run time.

This is what i have tried so far.

NSString *url = @"http://180.160.1.140/webapp/camera?id=";
NSArray *parts = [url componentsSeparatedByString:@"="];
NSString *personID = [parts lastObject];
NSLog(@"My ID: %@", personID);

Solution

  • see:

    NSString *url = @"http://180.160.1.140/webapp/camera?id=fksmf84-8493-45u3"; // you forgot to add the id in your test case
    NSArray *parts = [url componentsSeparatedByString:@"="];
    NSString *personID = [parts lastObject];
    NSLog(@"My ID: %@", personID);
    

    The output: My ID: fksmf84-8493-45u3

    EDIT:

    To Store the data:

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    [defaults setObject:personID forKey:@"personID"];
    

    To read the data:

     NSString *personID = [defaults objectForKey:@"personID"];
    

    With this method you can easily get the value of the last run. Hope this will help you :) .