IOS docking CC video interface

Posted by mysterbx on Thu, 13 Feb 2020 23:07:41 +0100

IOS docking CC video interface

On demand interface document
Live interface document

Docking process:
1. Implementation of HTTP communication encryption:
The request parameter hash of CC video interface needs to be composed of other request parameters and api key with salt value hash. First, the common query parameters (including the current timestamp) should be sorted in dictionary order, and then MD5 with key as salt value to get the hash value
The implementation code is as follows:

//Sort strings in dictionary order
//Returns a variable length string
+(NSMutableString *) order: (NSString*)queryString{
    NSArray *stringList = [queryString componentsSeparatedByString:@"&"];
    NSArray *resultList = [stringList sortedArrayUsingSelector:@selector(compare:)];
    NSMutableString *result = [NSMutableString stringWithString:resultList[0]];
    if (resultList.count <= 1){
        return result;
    for (int i = 1; i <= [resultList count] - 1; i++) {
        [result appendString:@"&"];
        [result appendString:resultList[i]];

    return result;

//MD5 encryption
//#import <CommonCrypto/CommonDigest.h>
//To import the package above, otherwise you will be warned
+(NSString *)md5DigestWithString:(NSString*)input{
    const char* str = [input UTF8String];
    unsigned char result[16];
    CC_MD5(str, (uint32_t)strlen(str), result);
    NSMutableString *ret = [NSMutableString stringWithCapacity:16 * 2];
    for(int i = 0; i<16; i++) {
        [ret appendFormat:@"%02x",(unsigned int)(result[i])];
    return ret;


 * @param queryString Parameters to be converted
 * @param isVideo Call on demand or not. true is the on demand api and false is the live api
 * @return Valid request parameters
+(NSString *) decode:(NSString*)queryString and: (BOOL)isVideo{
    NSDate *datenow =[NSDate date];//Now time, you can output to see what format it is
    //Set time zone
    NSTimeZone *zone = [NSTimeZone timeZoneWithAbbreviation:@"GMT+8"];
    NSInteger interval = [zone secondsFromGMTForDate:datenow];
    NSDate *localeDate = [datenow  dateByAddingTimeInterval: interval];
    //Conversion timestamp
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[localeDate timeIntervalSince1970]];
    //Link timestamps
    NSMutableString *hashedQueryString = [THQSUtil order:queryString];
    [hashedQueryString appendString:@"&time="];
    [hashedQueryString appendString:timeSp];
    //Because the final required link does not need the salt value, a copy is used for MD5 encryption, and copy cannot be used, because copy gets a static variable and cannot be operated
    NSMutableString *temp = [NSMutableString stringWithFormat:@"%@", hashedQueryString];
    [temp appendString:@"&salt="];
    //Live and on-demand key are different
    if (isVideo){
        [temp appendString:KEY];
        [temp appendString:ROOM_KEY];
    //Concatenate the obtained hash values
    [hashedQueryString appendString:@"&hash="];
    [hashedQueryString appendString:[THQSUtil md5DigestWithString:temp]];
    return hashedQueryString;

Note: the on-demand key is different from the live key
2.HTTP request implementation
The interface of CC video is accessed by GET mode. The blogger uses the class of NSURLSession to request, and uses the NSURLSession datadelegate delegate delegate to listen for data changes.
The implementation is as follows:

 * Use HttpURLConnection
 * Functions to access CC video interface
 * @param requestUri Address of the api called
 * @param isVideo Call on demand or not,trueFor the on-demand api,falseFor live broadcast api
-(void)request:(NSString*)requestUri has:(NSString*)param and: (BOOL)isVideo{
    //Get CC video interface link
    NSString *urlStr = [THQSUtil decode:param and:YES];
    NSMutableString *str = [NSMutableString stringWithFormat:@"%@", requestUri];
    [str appendFormat:@"%@", urlStr];

    //Using HTTP
    NSURL *url = [[NSURL alloc] initWithString:[str copy]];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:2.0f];
    //Create session and set delegation
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self.delegate delegateQueue:[[NSOperationQueue alloc] init]];
    //Initialize task
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request];
    //Send request (execute Task)
    [dataTask resume];

Note: if the requested parameter exists in Chinese, for example, when calling the search video interface, the parameter value of the title needs to be transcoded ["requested parameter" appendFormat:@"%@", [content stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];, note that special characters also need to be coded, such as & sort = creation ENU date: D ESC should be written as & sort = creation \ date% 3adesc

Topics: Session iOS