CNLiveUserManager.m 4.89 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"

#define formalStatUrl @"http://app.sta.cnlive.com/app.jpg"

@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
{
    [CNLiveUserSystemTools getRealTimeString:^(NSString * _Nullable realTime, NSDate * _Nullable netDate) {
        
        _startDate = netDate;
        
        NSString *stamp = realTime;
        stamp = [stamp stringByReplacingOccurrencesOfString:@"." withString:@""];
        NSString *baseUrl = formalStatUrl;
        NSString *vvUrl = [NSString stringWithFormat:@"%@?sver=%@&uid=%@&tm=%@&from=%@&spid=&appid=%@&version=1005_%@&eventid=1300&uri=&sid=&plat=i_%@&objid=",baseUrl,kSVer,[CNLiveUserSystemTools uidForStat:realTime],stamp,[CNLiveUserManager manager].fromID?[CNLiveUserManager manager].fromID: @"apple",self.appId,SDKVersion, StatAppVersion];
        
        [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:vvUrl]] delegate:nil];
        
    }];
}

- (void)endUserStat
{
    [CNLiveUserSystemTools getRealTimeString:^(NSString * _Nullable realTime, NSDate * _Nullable netDate) {
        
        NSString *stamp = realTime;
        stamp = [stamp stringByReplacingOccurrencesOfString:@"." withString:@""];
        
        NSString *endDate = [NSString stringWithFormat:@"%.3f",[netDate timeIntervalSinceDate:_startDate]];
        endDate = [endDate stringByReplacingOccurrencesOfString:@"." withString:@""];
        NSString *baseUrl = formalStatUrl;
        NSString *vvUrl = [NSString stringWithFormat:@"%@?sver=%@&uid=%@&tm=%@&from=%@&spid=&appid=%@&version=1005_%@&eventid=1311&uri=%@&sid=&plat=i_%@&objid=",baseUrl,kSVer,[CNLiveUserSystemTools uidForStat:realTime],stamp,[CNLiveUserManager manager].fromID?[CNLiveUserManager manager].fromID: @"apple",self.appId,SDKVersion,endDate,StatAppVersion];
        
        [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:vvUrl]] delegate:nil];
    }];
}

- (void)wjjStartUserStat
{
    [CNLiveUserSystemTools getRealTimeString:^(NSString * _Nullable realTime, NSDate * _Nullable netDate) {
        
        _startDate = netDate;
        
        NSString *stamp = realTime;
        stamp = [stamp stringByReplacingOccurrencesOfString:@"." withString:@""];
        NSString *baseUrl = formalStatUrl;
        NSString *vvUrl = [NSString stringWithFormat:@"%@?sver=%@&uid=%@&tm=%@&from=%@&spid=&appid=%@&version=1005_%@&eventid=1320&uri=&sid=%@&plat=i_%@&objid=",baseUrl,kSVer,[CNLiveUserSystemTools uidForStat:realTime],stamp,[CNLiveUserManager manager].fromID?[CNLiveUserManager manager].fromID: @"apple",self.appId,SDKVersion, [CNLiveUserManager manager].userId ? [CNLiveUserManager manager].userId: @"",StatAppVersion];
        NSLog(@"url start:\n%@",vvUrl);
        [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:vvUrl]] delegate:nil];
        
    }];
}

- (void)wjjEndUserStat
{
    [CNLiveUserSystemTools getRealTimeString:^(NSString * _Nullable realTime, NSDate * _Nullable netDate) {
        
        NSString *stamp = realTime;
        stamp = [stamp stringByReplacingOccurrencesOfString:@"." withString:@""];
        
        NSString *endDate = [NSString stringWithFormat:@"%.3f",[netDate timeIntervalSinceDate:_startDate]];
        endDate = [endDate stringByReplacingOccurrencesOfString:@"." withString:@""];
        NSString *baseUrl = formalStatUrl;
        
        
        
        
        NSString *vvUrl = [NSString stringWithFormat:@"%@?sver=%@&uid=%@&tm=%@&from=%@&spid=&appid=%@&version=1005_%@&eventid=1321&uri=%@&sid=%@&plat=i_%@&objid=",baseUrl,kSVer,[CNLiveUserSystemTools uidForStat:realTime],stamp,[CNLiveUserManager manager].fromID?[CNLiveUserManager manager].fromID: @"apple",self.appId,SDKVersion,endDate,[CNLiveUserManager manager].userId ? [CNLiveUserManager manager].userId: @"",StatAppVersion];
        NSLog(@"url end:\n%@",vvUrl);
        [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:vvUrl]] delegate:nil];
    }];
}

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

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