0

I'm trying to retrieve a line of code from my server and implement it in my NSUserDefaults.

Right now, this is what my appdelegate.m looks like:

NSDictionary* defaults = @{@"server_addr": @"http://156.92.15.802"};
    [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];

The http://156.92.15.802 url is the part that I need to GET from my server.

If my server has a file named Example.txt on it and within that file is a single line that like http://156.92.15.802, how can I use AFNetworking to check the file on my server and then add it to the NSUserDefaults?

Creagen
  • 478
  • 5
  • 17

2 Answers2

2

Here's the steps:

  1. Download the Example.txt file
  2. Read the downloaded txt file into a NSString*
  3. Save this NSString* into your NSUserDefaults

- (void)requestTextFile {
  NSString *urlString = @"http://156.92.15.802/Example.txt";

  NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

  NSURL *URL = [NSURL URLWithString:urlString];
  NSURLRequest *request = [NSURLRequest requestWithURL:URL];

  NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
    NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
    return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
  } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
    NSString* content = [NSString stringWithContentsOfFile:filePath.relativePath encoding:NSUTF8StringEncoding error:NULL];
    NSLog(@"content = %@", content);
    //save this content to your NSUserDefaults
    //...
  }];
  [downloadTask resume];
}
Hoang Tran
  • 296
  • 1
  • 6
  • What would i replace the http://156.92.15.802 with in NSDictionary* defaults = @{@"server_addr": @"http://156.92.15.802"}; [[NSUserDefaults standardUserDefaults] registerDefaults:defaults]; – Creagen Aug 04 '16 at 03:22
  • Why would you want to use the `registerDefaults` method? What you want is to save the content into `NSUserDefaults`, right? If that's the case, you can simply call `[[NSUserDefaults standardUserDefaults] setObject:content forKey:@"text_content"];`. If you want to also store the server address, call `[[NSUserDefaults standardUserDefaults] setObject:@"http://156.92.15.802" forKey:@"server_addr"];` – Hoang Tran Aug 04 '16 at 03:26
  • And if "forKey" is "server_addr" then whatever line is in the .txt file will be read as the server_addr? – Creagen Aug 04 '16 at 03:29
  • If you want to store the txt content into the `server_addr` key, then call `[[NSUserDefaults standardUserDefaults] setObject:content forKey:@"server_addr"];` – Hoang Tran Aug 04 '16 at 03:30
  • You may want to call `[[NSUserDefaults standardUserDefaults] synchronize];` after you finish setting values to persist all changes into `NSUserDefaults` – Hoang Tran Aug 04 '16 at 03:31
  • Thank you very much! I believe this is doing what I wanted to do. Very much appreciated! – Creagen Aug 04 '16 at 03:41
  • Quick question, when I change the http:// in the .txt file and reload the app, content = doesn't grab the new http:// url. It shows the original one but when I delete the app and reinstall the app, it grabs the new one. Is there any way to make it so that it checks the .txt file on every open? – Creagen Aug 04 '16 at 03:44
  • You can remove the `test.txt` file after finish setting into the `NSUserDefaults` like so: `[[NSFileManager defaultManager] removeItemAtURL:filePath error:NULL];`. When `AFNetworking` completes the download, it then saves the txt file to the `Documents` path. But if there is already an existing txt file with the same name there, it doesn't overwrite, it simply just ignores it and do nothing. So what we can do is to remove the txt file when we are done with it. – Hoang Tran Aug 04 '16 at 03:53
  • That totally did it! Thank you very much! :) – Creagen Aug 04 '16 at 04:13
  • Quick question, do you know how I could setup an if statement saying that if there is no file at the location for the server_addr then I can throw an error and do something else? – Creagen Aug 04 '16 at 21:04
1

I'm using the same to POST onto server. But are you sure you want to work with AFNetworking, i think NSURLConnection will also work.

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:stringURL]];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];

[manager POST:stringURL parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
    //do not put image inside parameters dictionary as I did, but append it!

}
success:^(NSURLSessionDataTask *task, id responseObject)
 {
     //here is place for code executed in success case
 }
failure:^(NSURLSessionDataTask *task, NSError *error)
{
     //here is place for code executed in error case
 }];
gurmandeep
  • 1,227
  • 1
  • 14
  • 30