智慧服务,成就美好体验 项目咨询

主页 > 服务与支持 > 开发平台 > 客户端SDK参考 > iOS Native SDK > 附录 HelloWorld文件源码

入门使用

HelloWorld文件源码

更新时间:2019-11-20

ViewController.mm

#import "ViewController.h"

#import "tsdk_login_def.h"
#import "tsdk_login_interface.h"
#import "tsdk_error_def.h"
#import "tsdk_manager_interface.h"
#import "tsdk_manager_def.h"
#include <netdb.h>
#include <net/if.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
#include <dlfcn.h>
#include <sys/sysctl.h>
@interface ViewController ()
@property (strong, nonatomic)  UITextField *account_textField;
@property (strong, nonatomic)  UITextField *password_textField;
@property (strong, nonatomic)  UITextField *serverAddress_textField;
@property (strong, nonatomic)  UITextField *serverport_textField;
@property (strong, nonatomic)  UIButton *login_button;
@end
@implementation ViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self configView];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initUportalLoginService];
}
-(void)configView
{
    _account_textField = [[UITextField alloc] init];
    _account_textField.borderStyle = UITextBorderStyleRoundedRect;
    _account_textField.center = CGPointMake(self.view.bounds.size.width/2, 60);
    _account_textField.bounds = CGRectMake(0, 0, self.view.bounds.size.width - 20, 30);
    _account_textField.placeholder = @"Account";
    [self.view addSubview:_account_textField];

    _password_textField = [[UITextField alloc] init];
    _password_textField.borderStyle = UITextBorderStyleRoundedRect;
    _password_textField.center = CGPointMake(self.view.bounds.size.width/2, CGRectGetMaxY(_account_textField.frame)+30);
    _password_textField.bounds = CGRectMake(0, 0, self.view.bounds.size.width - 20, 30);
    _password_textField.placeholder = @"Password";
    [self.view addSubview:_password_textField];

    _serverAddress_textField = [[UITextField alloc] init];
    _serverAddress_textField.borderStyle = UITextBorderStyleRoundedRect;
    _serverAddress_textField.center = CGPointMake(self.view.bounds.size.width/2, CGRectGetMaxY(_password_textField.frame)+ 30);
    _serverAddress_textField.bounds = CGRectMake(0, 0, self.view.bounds.size.width - 20, 30);
    _serverAddress_textField.placeholder = @"Server Address";
    [self.view addSubview:_serverAddress_textField];

    _serverport_textField = [[UITextField alloc] init];
    _serverport_textField.borderStyle = UITextBorderStyleRoundedRect;
    _serverport_textField.center = CGPointMake(self.view.bounds.size.width/2, CGRectGetMaxY(_serverAddress_textField.frame)+30);
    _serverport_textField.bounds = CGRectMake(0, 0, self.view.bounds.size.width - 20, 30);
    _serverport_textField.placeholder = @"Server Port";
    [self.view addSubview:_serverport_textField];

    _login_button = [[UIButton alloc] init];
    _login_button.center = CGPointMake(self.view.bounds.size.width/2, CGRectGetMaxY(_serverport_textField.frame)+30);
    _login_button.bounds = CGRectMake(0, 0, self.view.bounds.size.width - 20, 30);
    [_login_button setBackgroundColor:[UIColor redColor]];
    [_login_button setTitle:@"Login" forState:UIControlStateNormal];
    [_login_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_login_button addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_login_button];
}
- (void)loginAction
{
    [self.view endEditing:YES];
    BOOL result = [self loginAuthorizeWithServerAddress:_serverAddress_textField.text port:[_serverport_textField.text intValue] account:_account_textField.text password:_password_textField.text];
    if (!result)
    {
        [self showMessage:@"Uportal login fail!"];
    }
}
#pragma mark - Service
TSDK_VOID onTSDKNotifications(TSDK_UINT32 msgid, TSDK_UINT32 param1, TSDK_UINT32 param2, TSDK_VOID *data)
{
    NSLog(@"onTUPLoginNotifications : %#x",msgid);
    dispatch_async(dispatch_get_main_queue(), ^{
        switch (msgid)
        {
            case TSDK_E_LOGIN_EVT_AUTH_SUCCESS:
            {
                NSLog(@"Uportal login success !");
                [ViewController showMessages:@"Uportal login success"];
            }
                break;
            case TSDK_E_LOGIN_EVT_AUTH_FAILED:
            {
                NSLog(@"Uportal login fail !");
                [ViewController showMessages:@"Uportal login fail"];
            }
                break;
            default:
                break;
        }
    });
}

-(BOOL)initUportalLoginService
{
    TSDK_S_LOG_PARAM logParam;
    memset(&logParam, 0, sizeof(TSDK_S_LOG_PARAM));
    NSString *logPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingString:@"/TUPC60log"];
    NSString *path = [logPath stringByAppendingString:@"/tsdk"];
    logParam.level = TSDK_E_LOG_DEBUG;
    logParam.file_count = 1;
    logParam.max_size_kb = 4*1024;
    strcpy(logParam.path, [path UTF8String]);
    TSDK_RESULT result = tsdk_set_config_param(TSDK_E_CONFIG_LOG_PARAM, &logParam);

    TSDK_S_APP_INFO_PARAM app_info;
    memset(&app_info, 0, sizeof(TSDK_S_APP_INFO_PARAM));
    app_info.client_type = TSDK_E_CLIENT_MOBILE;
    strcpy(app_info.product_name, "SoftClient on Mobile");
    app_info.support_audio_and_video_call = TSDK_TRUE;
    app_info.support_ctd = TSDK_TRUE;
    app_info.support_audio_and_video_conf = TSDK_TRUE;
    app_info.support_enterprise_address_book = TSDK_TRUE;
    result = tsdk_init(&app_info ,&onTSDKNotifications);
    return result == TSDK_SUCCESS ? YES : NO;

}
-(BOOL)loginAuthorizeWithServerAddress:(NSString *)serverAddress port:(int)port account:(NSString *)account password:(NSString *)password
{
    TSDK_S_LOGIN_PARAM loginParam;
    memset(&loginParam, 0, sizeof(TSDK_S_LOGIN_PARAM));
    loginParam.user_id = 1;
    loginParam.auth_type = TSDK_E_AUTH_NORMAL;
    strcpy(loginParam.user_name, [account UTF8String]);
    strcpy(loginParam.password, [password UTF8String]);
    loginParam.server_type = TSDK_E_SERVER_TYPE_PORTAL;
    strcpy(loginParam.server_addr, [serverAddress UTF8String]);
    loginParam.server_port = (TSDK_UINT16)port;
    TSDK_RESULT result = tsdk_login(&loginParam);

    return result == TSDK_SUCCESS ? YES : NO;
}
-(void)showMessage:(NSString *)msg
{
    [[[UIAlertView alloc] initWithTitle:nil message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil] show];
}
+(void)showMessages:(NSString *)msg
{
    [[[UIAlertView alloc] initWithTitle:nil message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil] show];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end