added iOS source code
[wl-app.git] / iOS / Pods / Realm / Realm / RLMUpdateChecker.mm
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
19 #import "RLMUpdateChecker.hpp"
20
21 #import "RLMRealm.h"
22 #import "RLMUtil.hpp"
23
24 #if TARGET_IPHONE_SIMULATOR && !defined(REALM_COCOA_VERSION)
25 #import "RLMVersion.h"
26 #endif
27
28 void RLMCheckForUpdates() {
29 #if TARGET_IPHONE_SIMULATOR
30     if (getenv("REALM_DISABLE_UPDATE_CHECKER") || RLMIsRunningInPlayground()) {
31         return;
32     }
33
34     NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"alpha|beta|rc"
35                                                                            options:(NSRegularExpressionOptions)0
36                                                                              error:nil];
37     NSUInteger numberOfMatches = [regex numberOfMatchesInString:REALM_COCOA_VERSION
38                                                         options:(NSMatchingOptions)0
39                                                           range:NSMakeRange(0, REALM_COCOA_VERSION.length)];
40
41     if (numberOfMatches > 0) {
42         // pre-release version, skip update checking
43         return;
44     }
45
46     auto handler = ^(NSData *data, NSURLResponse *response, NSError *error) {
47         if (error || ((NSHTTPURLResponse *)response).statusCode != 200) {
48             return;
49         }
50
51         NSString *latestVersion = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
52         if (![REALM_COCOA_VERSION isEqualToString:latestVersion]) {
53             NSLog(@"Version %@ of Realm is now available: https://github.com/realm/realm-cocoa/blob/v%@/CHANGELOG.md", latestVersion, latestVersion);
54         }
55     };
56
57     NSString *url = [NSString stringWithFormat:@"https://static.realm.io/update/cocoa?%@", REALM_COCOA_VERSION];
58     [[NSURLSession.sharedSession dataTaskWithURL:[NSURL URLWithString:url] completionHandler:handler] resume];
59 #endif
60 }