2
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);
BangOperator
  • 4,377
  • 2
  • 24
  • 38
DDDDD
  • 55
  • 7

3 Answers3

5

With URL Components:

ObjC:

NSString *urlString = "http://180.160.1.140/webapp/camera?id=fksmf84-8493-45u3";
NSURLComponents *components = [NSURLComponents componentsWithString:urlString];
NSArray *queryItems = components.queryItems;

for (NSURLQueryItem *queryItem in queryItems) {
    if ([queryItem.name isEqualToString:@"id"]) {
        NSLog(@"%@", queryItem.value);
        break;
    }
}

Swift:

let urlComponents = NSURLComponents(string: "http://180.160.1.140/webapp/camera?id=fksmf84-8493-45u3")
let queryItems = urlComponents?.queryItems
if let id = queryItems?.filter({$0.name == "id"}).first?.value {
    print(id)
}

Swift 3 (Basically the same - NS):

let urlComponents = URLComponents(string: "http://180.160.1.140/webapp/camera?
id=fksmf84-8493-45u3")
let queryItems = urlComponents?.queryItems
if let id = queryItems?.filter({$0.name == "id"}).first?.value {
    print(id)
}
Richmond Watkins
  • 1,342
  • 9
  • 17
3

To parse all the key value pairs and get them as Dictionary you can use -parseQueryString: method.

You can parse your the query string like this...

   {
    //...
    NSString * urlQuery              = [url query];
    NSDictionary *dict = [self parseQueryString:urlQuery];
    NSString * valueForID = dict["id"];
    //...
    }
    // Waring : Its just a prototype, at production this might need safety checks.
    - (NSDictionary *)parseQueryString:(NSString *)query
    {
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        NSArray *pairs = [query componentsSeparatedByString:@"&"];

        for (NSString *pair in pairs)
        {
            NSArray *elements = [pair componentsSeparatedByString:@"="];
            NSString *key = [[elements objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            NSString *val = [[elements objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

            [dict addObject:val forKey:key];
        }
        return dict;
    }

Update : If targeting iOS versions newer than 7, check Richmond Watkins answer. New api's have been added for the same purpose, Check NSURLQueryItem

BangOperator
  • 4,377
  • 2
  • 24
  • 38
0

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 :) .

Masterfego
  • 2,648
  • 1
  • 26
  • 32
  • but fksmf84-8493-45u3 that part is keep changing. is it okay to use for an initial anyway? @Masterfego – DDDDD Jun 06 '16 at 12:35
  • So what do you want? just take the id or another thing? – Masterfego Jun 06 '16 at 12:38
  • Just wanna take this part `fksmf84-8493-45u3` but this letters and numbers are keep changing so its not stable. it can be `jdfsfmsf-0000-78h6` for the next time. @Masterfego Is this one still work? I wanna make it dynamic. – DDDDD Jun 06 '16 at 12:39
  • @Dicle It will be fine only until it does not contain the characters on which you split the string i.e '='. Like if the id is "fksmf84-8493=45u3", this would give you '45u3'. You must make sure this id does not contain '='. – BangOperator Jun 06 '16 at 12:44
  • I think you does not explain really what you want. Exlain it very well please. The subject is different that what you want. You want to save this id for anoteher use? – Masterfego Jun 06 '16 at 12:46
  • Whatever i type to url at first.. i am keep getting the same after `=`. Its not changing related to every person ID. @Masterfego @BangOperator – DDDDD Jun 06 '16 at 12:47
  • My first run `http://180.160.1.140/webapp/camera?id=fksmf84-8493-45u3` im getting this url.. My second run `http://180.160.1.140/webapp/camera?id=dfsfmsf-0000-78h6` i am getting this. So in my every run url part after `=` is changing how can get this changes for every run ? @Masterfego – DDDDD Jun 06 '16 at 12:48
  • Okay I understand, so if you want to say the value for each run to can use several ways: NSUserDefaults or CoreData. – Masterfego Jun 06 '16 at 12:51
  • any simple example you can give me?. i am trying but having issue.. @Masterfego – DDDDD Jun 06 '16 at 13:00
  • see changes in my response – Masterfego Jun 06 '16 at 13:07
  • Thank you so much @Masterfego – DDDDD Jun 06 '16 at 21:45