If you're looking for the most trivial example of iOS code, it would be
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSError *error;
NSString *string = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.myserver.com/getlevel?uid=johnsmith"]
encoding:NSUTF8StringEncoding
error:&error];
[self doSomethingWithString:string];
});
Note, if that doSomethingWithString
is going to update the user interface, you'd do:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSError *error;
NSString *string = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.myserver.com/getlevel?uid=johnsmith"]
encoding:NSUTF8StringEncoding
error:&error];
dispatch_async(dispatch_get_main_queue(), ^{
[self doSomethingWithString:string];
});
});
If you can make your server generate JSON data, that might be a better approach, though (that way the server can formulate a proper response, can report errors, your client can detect 404 errors and the like, etc.):
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSError *error;
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.myserver.com/getlevel?uid=johnsmith"]
options:kNilOptions
error:&error];
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
dispatch_async(dispatch_get_main_queue(), ^{
[self doSomethingWithJsonObject:dictionary];
});
});