[futurebasic] Re: [FB] Software version check

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : October 2014 : Group Archive : Group : All Groups

From: Ken Shmidheiser <kshmidheiser@...>
Date: Sat, 11 Oct 2014 19:56:09 -0400
Brian wrote:

> Maybe someone will accept the challenge to upgrade the demo!


Okay, it's been a few days so I'll give it a try.

Ken

include "ConsoleWindow"
compile as "Objective-C"
include "NSLog.incl"

BeginCDeclaration
CFStringRef checkVersion( CFStringRef urlStr );
EndC

BeginCFunction
CFStringRef checkVersion( CFStringRef urlStr ) {

NSURL *url = [NSURL URLWithString:(NSString *)urlStr];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];

[NSURLConnection
         sendAsynchronousRequest:urlRequest
         queue:[NSOperationQueue mainQueue]
         completionHandler:^(NSURLResponse *response,
                             NSData *data,
                             NSError *error) 
         {
          NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

              // A good code is 200 which means data is found; a 404 code indicates a page error
              NSLog(@"NSURL response code = %i", [httpResponse statusCode]);

         // Check for data, error and 404 error page status
         if ([data length] > 0 && error == nil && [httpResponse statusCode] != 404 )
            {
           // Compare web version with your app's local version
           NSString *newestVersion = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

               NSLog(@"Newest version via URL: %@", newestVersion);

           NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)@"CFBundleShortVersionString"];

               NSLog(@"My application's version (in this case this test app built by FB): %@", currentVersion);
               
             if (newestVersion && [currentVersion compare:newestVersion] != NSOrderedSame)
             {
                // Inform user new version exists
                NSLog(@"New version available for download.");
             }

         }
         // Handle missing data
         else if ([data length] == 0 && error == nil)
         {
           NSLog(@"Nothing was downloaded.");
         }
         // Handle error
         else if (error != nil)
         {
           NSLog(@"%@", [error localizedDescription]);
         }
         // Handle 404 page load error
         else if ([httpResponse statusCode] == 404)
         {
           NSLog(@"Page error 404.");
         }
    }];
 
return (CFStringRef)@"Done";

}
EndC
toolbox fn checkVersion( CFStringRef url ) = CFStringRef

dim as CFStringRef s

s = fn checkVersion( @"http://www.brilorsoftware.com/demo_version.txt" )

fn HIViewSetText( sConsoleHITextView, s )