From 81dbc0387759b5533d2f3926f04b39272d02e998 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sonee=CC=81=20John?= Date: Mon, 7 Dec 2020 16:37:40 -0400 Subject: [PATCH] Set session user-agent to desktop client (fixes #510) YouTube appears to randomly redirect client to mobile site. This should fix the issue for now --- XCDYouTubeKit/XCDYouTubeVideoOperation.m | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/XCDYouTubeKit/XCDYouTubeVideoOperation.m b/XCDYouTubeKit/XCDYouTubeVideoOperation.m index ae021133..be4ce9ee 100644 --- a/XCDYouTubeKit/XCDYouTubeVideoOperation.m +++ b/XCDYouTubeKit/XCDYouTubeVideoOperation.m @@ -89,12 +89,12 @@ - (instancetype) initWithVideoIdentifier:(NSString *)videoIdentifier languageIde _videoIdentifier = videoIdentifier ?: @""; _languageIdentifier = languageIdentifier ?: @"en"; - _session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration]]; + NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration]; _cookies = [cookies copy]; _customPatterns = [customPatterns copy]; for (NSHTTPCookie *cookie in _cookies) { - [_session.configuration.HTTPCookieStorage setCookie:cookie]; + [configuration.HTTPCookieStorage setCookie:cookie]; } NSString *cookieValue = [NSString stringWithFormat:@"f1=50000000&f6=8&hl=%@", _languageIdentifier]; @@ -107,7 +107,9 @@ - (instancetype) initWithVideoIdentifier:(NSString *)videoIdentifier languageIde NSHTTPCookieSecure:@"TRUE" }]; - [_session.configuration.HTTPCookieStorage setCookie:additionalCookie]; + [configuration.HTTPCookieStorage setCookie:additionalCookie]; + configuration.HTTPAdditionalHeaders = @{@"User-Agent": @"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Safari/605.1.15"}; + _session = [NSURLSession sessionWithConfiguration:configuration]; _operationStartSemaphore = dispatch_semaphore_create(0); return self;