1

Bear with me, I am very much new. XCode macOS Objective-C, I want to create a very simple app version check. This is outside of the apple app store ecosystem, so I do not mean checking version against the app store - I was thinking something even easier.

I simply want to have a defined value in my app, maybe an string or bool. I want my app to check this value against a value in a text file on a web server (something like) http://example.com/check/version.txt - Then, just a simple if/then check to notify if there is a new version.

I have seen some examples like:

NSString *path = [[NSBundle mainBundle] pathForResource:@"File" ofType:@"txt"];
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

And a lot of swift examples, but not from reading from the web in obj-c, just local.

Update:

I have tried this, but NSLOG always says running current version even if text file on server has the text in it: 11

And the code is comparing 11 to 10 so I assume should say update available?

- (void)windowDidLoad {
    [super windowDidLoad];
    NSURL *url = [NSURL URLWithString:@"https://example.com/versioncheck.txt"];
    NSError* error;
    NSString *serverversion = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];
    NSString *localversion = @"10";

    if(localversion = serverversion) {
        NSLog(@"Running Current Version of Software");
    } else {
        NSLog(@"An Update is Available");
    }
}
BinDev
  • 191
  • 13
  • 2
    Look for `NSURLSession`: https://stackoverflow.com/questions/40016361/nsurlsession-request-and-response Also, you should have your build number/version with your info plist: https://stackoverflow.com/questions/10015304/refer-to-build-number-or-version-number-in-code – Larme Oct 14 '20 at 17:17
  • Thank you Larme I have an NSURL example in my question but I still think I have something wrong - I realize the version is in plist, but just trying to get a basic number compare with a text file down first. – BinDev Oct 14 '20 at 17:49
  • 1
    Hi - you are comparing two strings, you should use something like ```[localversion isEqualToString:serverversion]```. In fact, you are *setting* localversion equal to serverversion there and then you test if localversion (= serverversion) is nil or not. Since it is NOT nit, you always get running current version OK. Check the difference between assign, checking for equals and checking if strings are equal in C and Objective-C – skaak Oct 14 '20 at 17:58
  • Differents steps: What's the format of your build/version? How to compare them (exemple: "1.10.1" vs "1.1.1" in the tests)? How to do the web fetch? How to extract the version from the web fetch (for instance, is it directly inside the file, etc.). Each of these steps is important. Look for each one, and if you need help on a specific one, we can help. – Larme Oct 14 '20 at 18:51
  • just told.. the project is still active and well working. [Sparkle Update Framework - SDK](https://sparkle-project.org) – Ol Sen Oct 16 '20 at 15:13

1 Answers1

0

Thanks to skaak - I just had the if statement wrong. Changing to an isEqualToString worked.

- (void)windowDidLoad {
    [super windowDidLoad];
    NSURL *url = [NSURL URLWithString:@"https:/example.com/versioncheck.txt"];
    NSError* error;
    NSString *serverversion = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];
    NSString *localversion = @"10";
    
    if([localversion isEqualToString:serverversion]) {
        NSLog(@"Running Current Version of Software");
    } else {
        NSLog(@"An Update is Available");
    }
}
BinDev
  • 191
  • 13