RLMUpdateChecker.mm 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2014 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. #import "RLMUpdateChecker.hpp"
  19. #import "RLMRealm.h"
  20. #import "RLMUtil.hpp"
  21. #if TARGET_IPHONE_SIMULATOR && !defined(REALM_COCOA_VERSION)
  22. #import "RLMVersion.h"
  23. #endif
  24. void RLMCheckForUpdates() {
  25. #if TARGET_IPHONE_SIMULATOR
  26. if (getenv("REALM_DISABLE_UPDATE_CHECKER") || RLMIsRunningInPlayground()) {
  27. return;
  28. }
  29. NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"alpha|beta|rc"
  30. options:(NSRegularExpressionOptions)0
  31. error:nil];
  32. NSUInteger numberOfMatches = [regex numberOfMatchesInString:REALM_COCOA_VERSION
  33. options:(NSMatchingOptions)0
  34. range:NSMakeRange(0, REALM_COCOA_VERSION.length)];
  35. if (numberOfMatches > 0) {
  36. // pre-release version, skip update checking
  37. return;
  38. }
  39. auto handler = ^(NSData *data, NSURLResponse *response, NSError *error) {
  40. if (error || ((NSHTTPURLResponse *)response).statusCode != 200) {
  41. return;
  42. }
  43. NSString *latestVersion = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  44. if (![REALM_COCOA_VERSION isEqualToString:latestVersion]) {
  45. NSLog(@"Version %@ of Realm is now available: https://github.com/realm/realm-cocoa/blob/v%@/CHANGELOG.md", latestVersion, latestVersion);
  46. }
  47. };
  48. NSString *url = [NSString stringWithFormat:@"https://static.realm.io/update/cocoa?%@", REALM_COCOA_VERSION];
  49. [[NSURLSession.sharedSession dataTaskWithURL:[NSURL URLWithString:url] completionHandler:handler] resume];
  50. #endif
  51. }