You want to use NSURLSession
:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *stringURL = @"https://(path to my text file)/nowplaying.txt";
NSURL *url = [NSURL URLWithString:stringURL];
NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (data) {
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
dispatch_async(dispatch_get_main_queue(), ^{
self.lblNowPlaying.text = string;
});
}
}];
[task resume];
}
Note, NSURLSession
runs its completion handler on a background serial queue, so you have to dispatch the update of the label to the main queue.
It is probably prudent to add some error checking, to check for NSError
object and to check the status code returned by the web server:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *stringURL = @"https://(path to my text file)/nowplaying.txt";
NSURL *url = [NSURL URLWithString:stringURL];
if (!url) {
NSLog(@"%s URL error: %@", __FUNCTION__, stringURL);
return;
}
NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
NSLog(@"%s error: %@", __FUNCTION__, error);
return;
}
if (![response isKindOfClass:[NSHTTPURLResponse class]]) {
NSLog(@"%s response: %@", __FUNCTION__, response);
return;
}
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.statusCode < 200 || httpResponse.statusCode >= 300) {
NSLog(@"%s statusCode %ld; response: %@", __FUNCTION__, httpResponse.statusCode, response);
return;
}
if (data) {
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
dispatch_async(dispatch_get_main_queue(), ^{
self.lblNowPlaying.text = string;
});
}
}];
[task resume];
}