CNLiveUserManager.m 2.41 KB
//
//  CNLiveUserManager.m
//  CNLiveUserSystemDemo
//
//  Created by iOS on 16/9/18.
//  Copyright © 2016年 zhulin. All rights reserved.
//

#import "CNLiveUserManager.h"
#import "CNLiveUserSystemTools.h"
#import "Common.h"

@interface CNLiveUserManager ()
{
    NSDate *_startDate;
}

@end

@implementation CNLiveUserManager

+ (CNLiveUserManager *)manager
{
    static id instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    
    return instance;
}

- (void)addObserver
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationEnterBackground) name:UIApplicationWillResignActiveNotification object:nil];
}

- (void)startUserStat
{
    NSDate *date = [NSDate date];
    _startDate = date;
    
    NSString *stamp = [NSString stringWithFormat:@"%.3f", [date timeIntervalSince1970]];
    stamp = [stamp stringByReplacingOccurrencesOfString:@"." withString:@""];

    NSString *vvUrl = [NSString stringWithFormat:@"http://app.sta.cnlive.com/app.js?sver=%@&uid=%@&tm=%@&from=&spid=&appid=%@&version=1005_%@&eventid=1300&uri=&sid=&plat=i&objid=",kSVer,[CNLiveUserSystemTools uidForStat:date],stamp,self.appId,SDKVersion];
    
    [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:vvUrl]] delegate:nil];
}

- (void)endUserStat
{
    NSDate *date = [NSDate date];
    NSString *stamp = [NSString stringWithFormat:@"%.3f", [date timeIntervalSince1970]];
    stamp = [stamp stringByReplacingOccurrencesOfString:@"." withString:@""];
    
    NSString *endDate = [NSString stringWithFormat:@"%.3f",[date timeIntervalSinceDate:_startDate]];
    endDate = [endDate stringByReplacingOccurrencesOfString:@"." withString:@""];
    
    NSString *vvUrl = [NSString stringWithFormat:@"http://app.sta.cnlive.com/app.js?sver=%@&uid=%@&tm=%@&from=&spid=&appid=%@&version=1005_%@&eventid=1311&uri=%@&sid=&plat=i&objid=",kSVer,[CNLiveUserSystemTools uidForStat:date],stamp,self.appId,SDKVersion,endDate];
    
    [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:vvUrl]] delegate:nil];
}

- (void)applicationBecomeActive
{
    [self startUserStat];
}

- (void)applicationEnterBackground
{
    [self endUserStat];
}
@end