2

I'm looking for an extremely lightweight way to request a single piece of data from a web server on an iOS device. Put together a request to a web page e.g. http://www.myserver.com/getlevel?uid=johnsmith; asynchronously send the request, then retrieve the contents of the response (which will be a text file containing just a single integer) and do something with the result as soon as it arrives.

The goals are to minimize bandwidth, maximize speed, and keep the code as simple as possible.

Thanks!

4

2 に答える 2

1

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];
    });
});
于 2013-02-08T15:09:50.453 に答える
1

Depending upon whether your application will expand to include additional webservice calls, you might want to consider AFNetworking - https://github.com/AFNetworking/AFNetworking. Yes, you do have to install the AFNetworking library in your project, but it is easy to do and then you can enjoy something like:

NSURL *url = [NSURL URLWithString:@"https://alpha-api.app.net/stream/0/posts/stream/global"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSLog(@"App.net Global Stream: %@", JSON);
} failure:nil];
[operation start];

(Code taken from AFNetworking github documentation page).

于 2013-02-08T15:12:19.037 に答える