AppDelegate.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 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 "AppDelegate.h"
  19. #import <Realm/Realm.h>
  20. #import "DrawView.h"
  21. #import "Constants.h"
  22. @interface AppDelegate ()
  23. @property (nonatomic, strong) UIActivityIndicatorView *activityIndicatorView;
  24. @end
  25. @implementation AppDelegate
  26. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  27. {
  28. application.applicationSupportsShakeToEdit = YES;
  29. application.idleTimerDisabled = YES;
  30. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  31. self.window.rootViewController = [[UIViewController alloc] init];
  32. // Setup Global Error Handler
  33. [RLMSyncManager sharedManager].errorHandler = ^(NSError *error, RLMSyncSession *session) {
  34. NSLog(@"A global error has occurred! %@", error);
  35. };
  36. if ([RLMSyncUser currentUser]) {
  37. NSURL *syncURL = [NSURL URLWithString:[NSString stringWithFormat:@"realm://%@:9080/~/Draw", kIPAddress]];
  38. RLMRealmConfiguration.defaultConfiguration = [RLMSyncUser.currentUser configurationWithURL:syncURL fullSynchronization:YES];
  39. self.window.rootViewController.view = [DrawView new];
  40. }
  41. else {
  42. [self showActivityIndicator];
  43. [self logIn];
  44. }
  45. [self.window makeKeyAndVisible];
  46. return YES;
  47. }
  48. - (void)logIn
  49. {
  50. // The base server path
  51. // Set to connect to local or online host
  52. NSURL *authURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@:9080", kIPAddress]];
  53. // Creating a debug credential since this demo is just using the generated access token
  54. // produced when running the Realm Object Server via the `start-object-server.command`
  55. RLMSyncCredentials *credential = [RLMSyncCredentials credentialsWithUsername:@"demo@realm.io"
  56. password:@"password"
  57. register:NO];
  58. // Log the user in (async, the Realm will start syncing once the user is logged in automatically)
  59. [RLMSyncUser logInWithCredentials:credential
  60. authServerURL:authURL
  61. onCompletion:^(RLMSyncUser *user, NSError *error) {
  62. if (error) {
  63. self.activityIndicatorView.hidden = YES;
  64. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Login Failed" message:error.localizedDescription preferredStyle:UIAlertControllerStyleAlert];
  65. [alertController addAction:[UIAlertAction actionWithTitle:@"Retry" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  66. [self logIn];
  67. self.activityIndicatorView.hidden = NO;
  68. }]];
  69. [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
  70. }
  71. else { // Logged in setup the default Realm
  72. // The Realm virtual path on the server.
  73. // The `~` represents the Realm user ID. Since the user ID is not known until you
  74. // log in, the ~ is used as short-hand to represent this.
  75. NSURL *syncURL = [NSURL URLWithString:[NSString stringWithFormat:@"realm://%@:9080/~/Draw", kIPAddress]];
  76. RLMRealmConfiguration.defaultConfiguration = [RLMSyncUser.currentUser configurationWithURL:syncURL fullSynchronization:YES];
  77. self.window.rootViewController.view = [DrawView new];
  78. }
  79. }];
  80. }
  81. - (void)showActivityIndicator
  82. {
  83. if (self.activityIndicatorView == nil) {
  84. self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  85. self.activityIndicatorView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin |
  86. UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
  87. }
  88. [self.window.rootViewController.view addSubview:self.activityIndicatorView];
  89. self.activityIndicatorView.center = self.window.center;
  90. [self.activityIndicatorView startAnimating];
  91. }
  92. @end