0xced / XCDYouTubeKit

YouTube video player for iOS, tvOS and macOS
MIT License
2.92k stars 627 forks source link

Add the NSDictionary to the header of XCDYouTubeVideo #294

Open SoneeJohn opened 7 years ago

SoneeJohn commented 7 years ago

I think exposing the NSDictionary object that the XCDYouTubeVideo class is initialized with would be really helpful. For instance, this would make it more flexible to use features that aren't implemented by the library as yet (https://github.com/0xced/XCDYouTubeKit/pull/281). Sometimes pull requests may not be merged with the library for whatever reason. By exposing this property it enables us to add these features on our own.

Currently, this is my method of getting the NSDictionary which isn't pretty for obvious reasons. 😉

self.operation = [[XCDYouTubeClient defaultClient] getVideoWithIdentifier:@"wJdeVtnUxuk" completionHandler:^(XCDYouTubeVideo * _Nullable video, NSError * _Nullable error) {

            NSURLSessionDataTask *task = [self.operation valueForKey:@"dataTask"];

            if (!task) {
                return;
            }

            [[[NSURLSession sharedSession]dataTaskWithURL:task.response.URL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

                if (!data) {
                    return;
                }
                CFStringEncoding encoding = CFStringConvertIANACharSetNameToEncoding((__bridge CFStringRef)response.textEncodingName ?: CFSTR(""));
                // Use kCFStringEncodingMacRoman as fallback because it defines characters for every byte value and is ASCII compatible. See https://mikeash.com/pyblog/friday-qa-2010-02-19-character-encodings.html
                NSString *responseString = CFBridgingRelease(CFStringCreateWithBytes(kCFAllocatorDefault, data.bytes, (CFIndex)data.length, encoding != kCFStringEncodingInvalidId ? encoding : kCFStringEncodingMacRoman, false)) ?: @"";

                NSDictionary *dic = DictionaryWithQueryString(responseString);

            }]resume];

        }];
SoneeJohn commented 7 years ago

Update: Plus my method does not always work in getting back the original dictionary that the XCDYouTubeVideo was initialized with.