# Log In

This guide introduces how to initialize JC SDK and log in.

# Initialize

Call the JCClient create (opens new window) interface on the main thread to create a JCClient (opens new window) instance object. Input the obtained appKey to initialize JCClient (opens new window) .

TIP

Since JC SDK functions are based on modules and module instances will be frequently used, it is recommended to use a singleton to manage the classes in the JC SDK.

//Initialize
-(bool)initialize {
   JCClient *client = [JCClient create:"AppKey" callback:self creatParam:nil];
   return client.state == JCClientStateIdle;
}

Among them, callback is the proxy object of the JCClientCallback protocol, which is used to notify the upper layer of the client’s state changes. Therefore, you need to specify the proxy object of callback first, and then implement the method of JCClientCallback in the proxy object.

The main methods in JCClientCallback are as follows:

//Login result callback
-(void)onLogin:(bool)result reason:(JCClientReason)reason;

//Logout callback
-(void)onLogout:(JCClientReason)reason;

//Login status change notification
-(void)onClientStateChange:(JCClientState)state oldState:(JCClientState)oldState;

After successful initialization, JCClientState state changes from JCClientStateNotInit (not initialized) to JCClientStateIdle (not logged in).

# Initiate login

After the SDK is initialized, login integration is possible.

The call flow of the login interface is as follows:

../../../../_images_en/ios_login.jpg

First create a JCClientLoginParam (opens new window) instance to adjust the login parameters. Then call login (opens new window) to initiate login:

JCClientLoginParam* loginParam = [[JCClientLoginParam alloc] init];
// Initiate login
[client login:userID password:password loginParam:loginParam];

TIP

  1. userID is English, numbers and + - _ . , case-insensitive, the length should not exceed 64 characters, - _ . cannot be the first character.

  2. password the length should not exceed 128 characters.

  3. When calling this interface returns true, it only means that the interface is called successfully, not that the login is successful. The result of the login will be reported through the onLogin callback.

  4. The default login address is http:cn.router.justalkcloud.com:8080. If you need to change to a foreign environment, you need to set it to http:intl.router.justalkcloud.com:8080 through the serverAddressproperty of JCClient before logging in.

After the call to initiate the login interface is successful, the method in the JCClientCallback callback will be triggered to notify the upper layer of the status of the client. The specific logic is as follows:

First, the login state change callback onClientStateChange (opens new window) in JCClientCallback will be triggered. You can implement the onClientStateChange (opens new window) method in the upper layer and handle the related logic.

-(void)onClientStateChange:(JCClientState)state oldState:(JCClientState)oldState
{
    if (state == JCClientStateIdle) { // Not logged in
    ...
    } else if (state == JCClientStateLogining) { // Logging in
    ...
    } else if (state == JCClientStateLogined) {  // Login successful
    ...
    } else if (state == JCClientStateLogouting) {
    ...
    }
}

Then the onLogin (opens new window) callback in JCClientCallback will be triggered. You can implement the onLogin (opens new window) method in the upper layer and handle the related logic:

-(void)onLogin:(bool)result reason:(JCClientReason)reason {
    if (result) {// Login successful
        ...
    }
    if (reason == JCClientReasonAuth) {// Account password is wrong
        ...
    }
}

After successful login, the JCClientState status changes from JCClientStateIdle (not logged in) to JCClientStateLogined (successful login). The SDK will automatically maintain the connection status with the server until the user actively calls the logout interface, or the device is logged out, or the account is logged in on another device. Login success/failure reason reference JCClientReason (opens new window) .

# Log out

The call flow of the logout interface is as follows:

../../../../_images_en/ios_logout.jpg

Call logout (opens new window) to initiate logout, and you cannot carry out various business operations on the platform after logging out:

[client logout];

Logging out will also trigger a login state change (onClientStateChange) callback, after which the logout result will be reported via the onLogout callback:

-(void)onLogout:(JCClientReason)reason {
    if (reason == JCClientReasonServerLogout) {// Force logout
        ...
    }
}

More logout reason reference: JCClientReason (opens new window)

After logging out successfully, the JCClientState status changes from JCClientStateLogined (successful login) to JCClientStateIdle (not logged in).

最后更新时间: 1/17/2023, 5:17:17 PM