ios 接网易云聊天-登录-注册

Claire ·
更新时间:2024-11-14
· 732 次阅读

1:Appdelegate,注册appkey

#import
#define WANG_YI_YUN_APPKEY @“xxxxxxxxxxxxxx”

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions :(NSDictionary *)launchOptions{ NSString *appKey = WANG_YI_YUN_APPKEY; NIMSDKOption *option = [NIMSDKOption optionWithAppKey:appKey]; option.apnsCername = @"your APNs cer name"; option.pkCername = @"your pushkit cer name"; [[NIMSDK sharedSDK] registerWithOption:option]; } 2.封装登录模型 WangYiYunModel XMDataSavingTools是保存模型用的工具

XMDataSavingTools

#import #import "XMDataSavingTools.h" @interface WangYiYunModel : NSObject @property (nonatomic,strong) NSString *accid; @property (nonatomic,strong) NSString *token; + (void) Save:(WangYiYunModel *) model; + (WangYiYunModel *) load; @end #import "WangYiYunModel.h" @implementation WangYiYunModel + (void) Save:(WangYiYunModel *) model{ [XMDataSavingTools save:model forKey:@"XMDataSavingTools"]; } + (WangYiYunModel *) load{ return [XMDataSavingTools load:@"XMDataSavingTools"]; } @end 3.找后台要 account,token 登录 WangYiYunModel *model = [[WangYiYunModel alloc] init]; model.accid = @""; model.token = @""; [[[NIMSDK sharedSDK] loginManager] login:model.accid token:model.token completion:^(NSError * _Nullable error) { if (error != nil) { NSLog(@"登录失败!"); }else{ NSLog(@"登录成功!"); ///保存到WangYiYunModel 自动登录需要 [WangYiYunModel Save:model]; } }]; 4.先添加代理 - (void)viewDidAppear:(BOOL)animated{ /// 添加代理 [[[NIMSDK sharedSDK] loginManager] addDelegate:self]; ///读取WangYiYunModel WangYiYunModel *wangyiyun = [WangYiYunModel load]; ///不为空自动登录 if ( wangyiyun != nil ) { NIMAutoLoginData *loginData = [[NIMAutoLoginData alloc] init]; loginData.account = wangyiyun.accid; loginData.token = wangyiyun.token; [[[NIMSDK sharedSDK] loginManager] autoLogin:loginData]; } } ///代理方法:登录 - (void)onLogin:(NIMLoginStep)step{ if (step == NIMLoginStepLoginOK) { NSLog(@"登录成功"); } if (step == NIMLoginStepLinking) { NSLog(@"连接服务器"); } if (step == NIMLoginStepLinkOK) { NSLog(@"连接服务器成功"); } if (step == NIMLoginStepLinkFailed) { NSLog(@"连接服务器失败"); } if (step == NIMLoginStepLogining) { NSLog(@"登录"); } if (step == NIMLoginStepLoginFailed) { NSLog(@"登录失败"); } if (step == NIMLoginStepSyncing) { NSLog(@"开始同步数据"); } if (step == NIMLoginStepSyncOK) { NSLog(@"同步数据完成"); } if (step == NIMLoginStepLoseConnection) { NSLog(@"连接断开"); } if (step == NIMLoginStepNetChanged) { NSLog(@"网络切换"); } } ///代理方法:失败 - (void)onAutoLoginFailed:(NSError *)error{ NSLog(@"登录失败"); } 登录成功
作者:zyw658000



易云 网易 IOS

需要 登录 后方可回复, 如果你还没有账号请 注册新账号