# iOS Integration (Español)

## iOS integration

**(A)** Cuando el usuario esta authenticado, hay que llamar esto

```
[BChatSDK.auth authenticate].thenOnMain(^id(id success) {
    // Chat SDK esta listo        
    return Nil;
}, ^id(NSError * error) {
    return Nil;
});
```

**(B)** Comenzar una conversacion:

```
id<PUser> otherUser = [BChatSDK.core userForEntityID:@"userEntityID"];
[BChatSDK.core createThreadWithUsers:@[otherUser] threadCreated:^(NSError * error, id<PThread> thread) {
    if (!error) {
        UIViewController * vc = [BChatSDK.ui chatViewControllerWithThread:thread];
        [self.navigationController pushViewController:vc animated:YES];
    }
}];
```

Los Pasos:

1. Conectar el Proyecto con Firebase [Info](https://github.com/chat-sdk/chat-sdk-ios/#firebase-setup)
2. Configurar Chat SDK en el App Delegate [Info](https://github.com/chat-sdk/chat-sdk-ios/#adding-the-chat-sdk-to-your-project)
3. Authenticar el usuario con Firebase
4. Llamar el codigo arriba (A)
5. Cuando el proceso ha cumplido, llamar el codigo (B)
6. Tendrias que tener 2 usuarios porque el `userEntityID` tiene que existir en Firebase

Lo primero que hago es esto: estos datos fueron probado en tu demo chat y funcionan.

```
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //chat sdk
    // Create a network adapter to communicate with Firebase
    // The network adapter handles network traffic

    BConfiguration * config = [BConfiguration configuration];
    config.rootPath = @"/v1";
    config.allowUsersToCreatePublicChats = NO;
    config.showEmptyChats = NO;
    config.googleMapsApiKey = @"AIzaSyBUMCx0vctiADu6WbgSpkEDrEf9Ov4naxA";
    config.clearDataWhenRootPathChanges = YES;
    config.loginUsernamePlaceholder = @"Email";
    config.allowUsersToCreatePublicChats = YES;

    config.disablePresence = YES;
    config.disableProfileUpdateOnAuthentication = NO;
    config.developmentModeEnabled = YES;
    config.disablePublicThreads = NO;
```

Luego agrego los siguientes metodos: el metodo sourceApplication depecrated, y el didReceiveRemoteNotification comento el que vos decis que hay que registrar y lo agrego al metodo que esta en el juego.

```
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [BChatSDK application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
}

-(BOOL) application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    return [BChatSDK application: app openURL: url options: options];
}

-(void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [BChatSDK application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

/*-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    [BChatSDK application:application didReceiveRemoteNotification:userInfo];
}
*/

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void  (^)(UIBackgroundFetchResult result))completionHandler {
  _fetchCompletionHandler = completionHandler;

    //sdkchat
    [BChatSDK application:application didReceiveRemoteNotification:userInfo];

    [self startBackgroundProcessing];

}
```

En tu ejemplo vos lo primero que haces es:

```
 UIViewController * rootViewController = BChatSDK.ui.splashScreenNavigationController;
 [self.window setRootViewController:rootViewController];
```

Lo primero que tengo yo es:

```
 _windowInitialized = NO;
  if (!EgoIsRunningInDebugger()) {
    EgoStartLogRedirection();
  }

  godot::StartInstabug("f59d39bdf61e15c7caf71c65b3c681b4", godot::InstabugApi::InvocationEventShake);
  [FIRApp configure];

  NSArray<NSString *> *commandLineParams = [self _additionalCommandlineParameters];
  _godotRuntime = [[GodotRuntime <GLViewDelegate> alloc] initWithAdditionalCommandlineParameters:commandLineParams];
  _godotRuntime.delegate = self;

  [_godotRuntime preinitializeEngine];
  EgoRegisterGodotClasses();

  _godotViewController = [[GodotViewController alloc] init];
  _godotRuntime.activeViewController = _godotViewController;

  UIStoryboard *splashScreenStoryboard = [UIStoryboard storyboardWithName:@"LaunchScreen" bundle:nil];
  _splashViewController = [splashScreenStoryboard instantiateInitialViewController];
  [_mainViewController addOverlay:_splashViewController];


  if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
    // This will start background processing when the app starts in background
    [self startBackgroundProcessing];
    return YES;
  }
  // App is in foreground
  EgoApplication::get_singleton()->enter_foreground();
  return [self initializeWindow];
}

- (BOOL)initializeWindow {
  if (_windowInitialized) {
    return NO;
  }
  _windowInitialized = YES;

  [self _initializeSplashScreenCallback];

  _mainViewController = [EgoMainViewController new];
  _mainViewController.view.frame = [[UIScreen mainScreen] bounds];
  [_mainViewController addOverlay:_godotViewController];

  _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  _window.rootViewController = _mainViewController;

  [_window makeKeyAndVisible];

  [_godotRuntime start];

  return YES;
```

Aqui es donde yo debiera hacer el overlay, para ellos pensaba crearme otra viewcontroller, y con botones desde el juego y chat cambiar el rootViewController

Una vez aunteticado en ego-app, lo que tengo que hacer es registrar esos datos en firebase, para ello puedo usar:

```
BAccountDetails * accountDetails = [BAccountDetails username: @"some.email@domain.com" password:@"some password"];
[BChatSDK.auth authenticate: accountDetails].thenOnMain(...);
```

Actualmente para crear una cuenta en ego-app no se usa el email, si no el telefono.

Luego para quedar logeado en el chat puedo usar

```
[BChatSDK.auth authenticate].thenOnMain(^id(id success) {
    ...
    return Nil;
}, Nil);
```

Es cierto esto?
