iOS开发接入腾讯云通信简略笔记

本文只记录一些关键点,初始配置查看官方文档:

TIMChat源码导读:https://cloud.tencent.com/document/product/269/3890

文档概述:https://cloud.tencent.com/document/product/269/9147gate.m :

iIM登录:(由于界面由h5编写,url为h5传值)

_loginParam = [[IMALoginParam alloc] init];
    [IMAPlatform configWith:_loginParam.config];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSArray *params =[url.query componentsSeparatedByString:@"&"];
        NSMutableDictionary *tempDic = [NSMutableDictionary dictionary];
        for (NSString *paramStr in params) {
            NSArray *dicArray = [paramStr componentsSeparatedByString:@"="];
            if (dicArray.count > 1) {
            NSString *decodeValue = [dicArray[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [tempDic setObject:decodeValue forKey:dicArray[0]];
            }
        }
        NSString *appid = [tempDic objectForKey:@"appid"];
        NSString *identifier = [tempDic objectForKey:@"identifier"];
        NSString *usersig = [tempDic objectForKey:@"usersig"];
        _loginParam.identifier = identifier;
        _loginParam.userSig = usersig;
        _loginParam.tokenTime = [[NSDate date] timeIntervalSince1970];
    
        //直接登录
        __weak IMALoginController *weakSelf = self;
        [[HUDHelper sharedInstance] syncLoading:@"正在登录"];
        [[IMAPlatform sharedInstance] login:_loginParam succ:^{
        [[HUDHelper sharedInstance] syncStopLoadingMessage:@"登录成功"];
        [weakSelf registNotification];
        [weakSelf enterMainUI];
        } fail:^(int code, NSString *msg) {
            [[HUDHelper sharedInstance] syncStopLoadingMessage:IMALocalizedError(code, msg) delay:2 completion:^{
            [weakSelf loginWith:url];
            }];
        }];
    });
//必须在登录之后上传token.在登录之后注册通知,保证通知回调也在登录之后,在通知的回调中上传的token。(回调在IMAAppDelegate的didRegisterForRemoteNotificationsWithDeviceToken中)
- (void)registNotification
{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
    else
    {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
    }
}

IM登出:

    [[HUDHelper sharedInstance] syncLoading:@"正在退出"];
    [[IMAPlatform sharedInstance] logout:^{
        [[HUDHelper sharedInstance] syncStopLoadingMessage:@"退出成功" delay:2 completion:^{

        }];
        NSLog(@"退出登录成功");
        
    } fail:^(int code, NSString *err) {
        [[HUDHelper sharedInstance] syncStopLoadingMessage:IMALocalizedError(code, err) delay:2 completion:^{

        }];
         NSLog(@"退出登录失败: code=%d err=%@", code, err);
    }];

IM消息接收:需要注册新消息通知回调-><TIMMessageListener>

- (void)onNewMessage:(NSArray*) msgs {
    for (TIMMessage *msg in msgs)
    {
        int elemCount = [msg elemCount];
        for (int i = 0; i < elemCount; i++) {
            TIMElem * elem = [msg getElem:i];
            if ([elem isKindOfClass:[TIMTextElem class]]) {
                TIMTextElem * text_elem = (TIMTextElem * )elem;
                NSString *message = text_elem.text;
                [_delegate sendNewMsg:message];
            }else if ([elem isKindOfClass:[TIMCustomElem class]])
            {
                TIMCustomElem *elem = (TIMCustomElem *)[msg getElem:i];
                NSString *message = [[NSString alloc] initWithData:elem.data encoding:NSUTF8StringEncoding];
                [_delegate sendNewMsg:message];
            }
            
            TIMAPNSConfig *config = [[TIMAPNSConfig alloc]init];
            [config setOpenPush:1];
            config.c2cSound = @"01.caf";
            [config setGroupSound:@"00.caf"];
            [config setVideoSound:@"11.caf"];
            
            [[TIMManager sharedInstance] setAPNS:config succ:^{
                NSLog(@"设置成功!");
            } fail:^(int code, NSString *err){
                NSLog(@"设置失败!");
            }];
        }
        
    }
}

IM推送通知

离线推送额外配置:https://cloud.tencent.com/document/product/269/9154

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    // 处理推送消息
    DebugLog(@"userinfo:%@",userInfo);
    DebugLog(@"收到推送消息:%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [[IMAPlatform sharedInstance] configOnAppEnterForeground];
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    __block UIBackgroundTaskIdentifier bgTaskID;
    bgTaskID = [application beginBackgroundTaskWithExpirationHandler:^ {
        
        //不管有没有完成,结束background_task任务
        [application endBackgroundTask: bgTaskID];
        bgTaskID = UIBackgroundTaskInvalid;
    }];
}
// app 进入后台时配置
- (void)configOnAppEnterBackground:(NSInteger)readCount withUnReadCount:(NSInteger)unReadCount;
{
    // 将相关的配置缓存至本地
    [[IMAPlatform sharedInstance] saveToLocal];
    NSInteger unReadCount_end = unReadCount-readCount;
    [UIApplication sharedApplication].applicationIconBadgeNumber = unReadCount_end;
    TIMBackgroundParam  *param = [[TIMBackgroundParam alloc] init];
    [[TIMManager sharedInstance] doBackground:param succ:^() {
        DebugLog(@"doBackgroud Succ");
    } fail:^(int code, NSString * err) {
        DebugLog(@"Fail: %d->%@", code, err);
    }];
}
// app 进前台时配置
- (void)configOnAppEnterForeground
{
    [UIApplication.sharedApplication.windows enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(UIWindow *w, NSUInteger idx, BOOL *stop) {
        if (!w.opaque && [NSStringFromClass(w.class) hasPrefix:@"UIText"]) {
            // The keyboard sometimes disables interaction. This brings it back to normal.
            BOOL wasHidden = w.hidden;
            w.hidden = YES;
            w.hidden = wasHidden;
            *stop = YES;
        }
    }];
    //清空通知栏消息
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    
    [[TIMManager sharedInstance] removeMessageListener:_conversationMgr];
    _conversationMgr = nil;
}
// app become active
- (void)configOnAppDidBecomeActive
{
    [[TIMManager sharedInstance] doForeground:^{
        DebugLog(@"doForegroud Succ");
    } fail:^(int code, NSString *msg) {
         DebugLog(@"Fail: %d->%@", code, msg);
    }];
}

// app 注册APNS成功后
- (void)configOnAppRegistAPNSWithDeviceToken:(NSData *)deviceToken
{
    DebugLog(@"didRegisterForRemoteNotificationsWithDeviceToken:%ld", (unsigned long)deviceToken.length);
    NSString *token = [NSString stringWithFormat:@"%@", deviceToken];
    [[TIMManager sharedInstance] log:TIM_LOG_INFO tag:@"SetToken" msg:[NSString stringWithFormat:@"My Token is :%@", token]];
    TIMTokenParam *param = [[TIMTokenParam alloc] init];

#if kAppStoreVersion

// AppStore版本
#if DEBUG
    param.busiId = 8447;
#else
    param.busiId = 8447;
#endif
    
#else
    //企业证书id
    param.busiId = 8448;
#endif
    
    [param setToken:deviceToken];
    
//    [[TIMManager sharedInstance] setToken:param];
    [[TIMManager sharedInstance] setToken:param succ:^{
       
        NSLog(@"-----> 上传token成功 ");
    } fail:^(int code, NSString *msg) {
        NSLog(@"-----> 上传token失败 ");
    }];
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
彩云 版本:mCloud2.2.0 | 大小:11 MB| 语言:中文 适用固件:Android 2.0及以上 分享日期:2013-12-13 游戏题材:商业 开发商:中国移动通信集团公司 内容介绍 彩云是中国移动旗下的个人网盘产品,面向所有用户提供安全、便捷、高效的个人云存储服务,帮助用户统一存储和管理不同终端的个人信息资产。用户首次注册即可获得16GB的网盘空间,通过官方推出的系列活动可免费领取T级空间。同时,依托运营商的资源优势,为用户提供业内速度最优的上传、下载体验。 【功能特点】 * 账号信息,安全无虞!——彩云向所有用户提供运营商级别的安全防护,除了免费登陆提醒,还有文件加密,全方位保障用户数字资产安全。 * T级空间,瞬间领取!——用户首次注册即可获得16GB的网盘空间,通过官方推出的系列活动可免费升级T级空间。 *精彩资源,短信分享!——直接转存他/她的彩云里,也可给他/她发短信,分享短信彩云无限量,免费发! * 跨端使用,就用彩云!——彩云为手机、个人电脑、平板电脑等提供的客户端覆盖所有操作系统,文件存彩云,手机、电脑,哪里想用点哪里。 * 手机信息,一键备份!——手机端可以实现通讯录、短彩信、手机应用、手机图片、手机视频和日历所有信息的全备份,信息丢失后只需一键恢复,即可找回所有数据

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值