Swiftpack.co - Package - 0xced/XCDYouTubeKit

About

Build Status Coverage Status Platform Pod Version Carthage Compatibility Swift Package Manager Compatibility License

XCDYouTubeKit is a YouTube video player for iOS, tvOS and macOS.

Are you enjoying XCDYouTubeKit? You can say thank you with a tweet. I am also accepting donations. ;-)

Donate button

Requirements

  • Runs on iOS 8.0 and later
  • Runs on macOS 10.9 and later
  • Runs on tvOS 9.0 and later

Warning

XCDYouTubeKit is against the YouTube Terms of Service. The only official way of playing a YouTube video inside an app is with a web view and the iframe player API. Unfortunately, this is very slow and quite ugly, so I wrote this player to give users a better viewing experience.

Installation

XCDYouTubeKit is available through CocoaPods, Carthage and Swift Package Manager.

CocoaPods:

pod "XCDYouTubeKit", "~> 2.15"

Carthage:

github "0xced/XCDYouTubeKit" ~> 2.15

Swift Package Manager:

Add XCDYouTubeKit to the dependencies value of your Package.swift

dependencies: [
	.package(url: "https://github.com/0xced/XCDYouTubeKit.git", from: "2.15.0")
]

Alternatively, you can manually use the provided static library or dynamic framework. In order to use the static library, you must:

  1. Create a workspace (File → New → Workspace…)
  2. Add your project to the workspace
  3. Add the XCDYouTubeKit project to the workspace
  4. Drag and drop the libXCDYouTubeKit.a file referenced from XCDYouTubeKit → Products → libXCDYouTubeKit.a into the Link Binary With Libraries build phase of your app’s target.

These steps will ensure that #import <XCDYouTubeKit/XCDYouTubeKit.h> will work properly in your project.

Usage

XCDYouTubeKit is fully documented.

iOS 8.0+ & tvOS (AVPlayerViewController)

AVPlayerViewController *playerViewController = [AVPlayerViewController new];
[self presentViewController:playerViewController animated:YES completion:nil];

__weak AVPlayerViewController *weakPlayerViewController = playerViewController;
[[XCDYouTubeClient defaultClient] getVideoWithIdentifier:videoIdentifier completionHandler:^(XCDYouTubeVideo * _Nullable video, NSError * _Nullable error) {
    if (video)
    {
        NSDictionary *streamURLs = video.streamURLs;
        NSURL *streamURL = streamURLs[XCDYouTubeVideoQualityHTTPLiveStreaming] ?: streamURLs[@(XCDYouTubeVideoQualityHD720)] ?: streamURLs[@(XCDYouTubeVideoQualityMedium360)] ?: streamURLs[@(XCDYouTubeVideoQualitySmall240)];
        weakPlayerViewController.player = [AVPlayer playerWithURL:streamURL];
        [weakPlayerViewController.player play];
    }
    else
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}];

iOS, tvOS and macOS

NSString *videoIdentifier = @"9bZkp7q19f0"; // A 11 characters YouTube video identifier
[[XCDYouTubeClient defaultClient] getVideoWithIdentifier:videoIdentifier completionHandler:^(XCDYouTubeVideo *video, NSError *error) {
	if (video)
	{
		// Do something with the `video` object
	}
	else
	{
		// Handle error
	}
}];

iOS 8.0

On iOS, you can use the class XCDYouTubeVideoPlayerViewController the same way you use a MPMoviePlayerViewController, except you initialize it with a YouTube video identifier instead of a content URL.

Present the video in full-screen

- (void) playVideo
{
	XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:@"9bZkp7q19f0"];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:videoPlayerViewController.moviePlayer];
	[self presentMoviePlayerViewControllerAnimated:videoPlayerViewController];
}

- (void) moviePlayerPlaybackDidFinish:(NSNotification *)notification
{
	[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:notification.object];
	MPMovieFinishReason finishReason = [notification.userInfo[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
	if (finishReason == MPMovieFinishReasonPlaybackError)
	{
		NSError *error = notification.userInfo[XCDMoviePlayerPlaybackDidFinishErrorUserInfoKey];
		// Handle error
	}
}

Present the video in a non full-screen view

XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:@"9bZkp7q19f0"];
[videoPlayerViewController presentInView:self.videoContainerView];
[videoPlayerViewController.moviePlayer play];

See the demo project for more sample code.

Logging

Since version 2.2.0, XCDYouTubeKit produces logs. XCDYouTubeKit supports CocoaLumberjack but does not require it.

See the XCDYouTubeLogger class documentation for more information.

Credits

The URL extraction algorithms in XCDYouTubeKit are inspired by the YouTube extractor module of the youtube-dl project.

Contact

Cédric Luthi

License

XCDYouTubeKit is available under the MIT license. See the LICENSE file for more information.

Github

link
Stars: 2749

Dependencies

Used By

Total: 0

Releases

Version 2.15.0 -

  • Adaptation to YouTube API change (#501)
  • New XCDYouTubeErrorUnknown error code (#484, #491)
  • New XCDYouTubeErrorTooManyRequests error code (comment)
  • Improve error message for XCDYouTubeErrorNoStreamAvailable code
  • Remove platforms in Swift Package Manager (#496)

Version 2.14.1 -

  • Adaptation to YouTube API change. (#487)
  • Annotate streamURL as nullable (#482)

Version 2.14.0 -

  • Add new XCDYouTubeErrorEmptyResponse error (#479)
  • Adaptation to YouTube API change. (a048776eb0d87de869c1636e244d78ffb311adc6)

Version 2.13.0 -

  • Deprecated -[XCDYouTubeVideo thumbnailURL] use -[XCDYouTubeVideo thumbnailURLs] (#477)
  • Exclude plist files from Swift Package Manager (#478)
  • Annotate languageIdentifier argument as nullable in XCDYouTubeVideoOperation
  • Add new author, videoDescription and channelIdentifier properties to XCDYouTubeVideo (#471)

Version 2.12.0 -

  • Add the ability to specify which streamURLs to query via -[XCDYouTubeClient queryVideo:video:streamURLsToQuery:options:cookies:completionHandler:] & XCDYouTubeVideoQueryOperation class.
    • initWithVideo:streamURLsToQuery:options:cookies: is now the designated initializer for XCDYouTubeVideoQueryOperation.
  • Correctly annotate the streamURLs & streamErrors properties as nullable.

Version 2.11.0 -

  • -[XCDYouTubeClient queryVideo:video:cookies:completionHandler:] completion handler is now correctly called on the main thread.
  • Improved video querying to handle videos that have incomplete streams. (#456)
  • Errors returned in streamErrors may contain the NSLocalizedRecoverySuggestionErrorKey key in -[NSError userInfo] when NSURLErrorNetworkConnectionLost is reported, this may indicate the file is incomplete on YouTube's server.
  • Add logging to XCDYouTubeVideoQueryOperation class.
  • Improved -[XCDYouTubeVideoQueryOperation description].

Version 2.10.0 -

  • Fixed issue that caused certain videos to return error (#468)
  • Add the ability to query XCDYouTubeVideo for reachable streams via -[XCDYouTubeClient queryVideo:video:cookies:completionHandler:] & XCDYouTubeVideoQueryOperation class
  • Add new videoIdentifiers property in XCDYouTubeVideo (#290)

Version 2.9.0 -

  • Add the ability to use custom regular expression patterns via -[XCDYouTubeClient getVideoWithIdentifier:cookies:customPatterns:completionHandler:] & initWithVideoIdentifier:cookies:languageIdentifier:customPatterns (#463, #199)
  • Add new viewCount property in XCDYouTubeVideo (#460)
  • Silence warning about deprecated implementation (#450)

Version 2.8.3 -

  • Adaptation to YouTube API change. (#458)

Version 2.8.2 -

  • Add new streamURL property on XCDYouTubeVideo
  • Fixed Swift Package Manager (#441)
  • Support iOS 13 in Demo Projects
    • Support background playback (#427, #442)

Version 2.8.1 -

  • Adaptation to YouTube API change. (#447, #448, #449)

Version 2.8.0 -

  • Adaptation to YouTube API change.
    • Deprecated XCDYouTubeErrorRestrictedPlayback & XCDYouTubeErrorInvalidVideoIdentifier use XCDYouTubeErrorNoStreamAvailable instead.

Version 2.7.8 -

  • Adaptation to YouTube API change. (#443)

Version 2.7.7 -

  • Add another player script regular expression (#438)

Version 2.7.6 -

Version 2.7.6

  • Adaptation to YouTube API change. (#437)

Version 2.7.5 -

  • Adaptation to YouTube API change. (#433)

Version 2.7.4 -

Version 2.7.4

  • Add the ability to fetch certain age-gate videos. (#431)

Version 2.7.3 -

Version 2.7.3

  • Adaptation to YouTube API change. (#419, #422. #421)
  • Fix JavaScript exception: ReferenceError (#418, #420)

Version 2.7.2 -

  • Adaptation to YouTube API change. (#406)

Version 2.7.1 -

Version 2.7.1

  • Fixed Xcode project parsing error (#409, #411)

Version 2.7.0 -

Version 2.7.0

  • Add the ability assign cookies via -[XCDYouTubeClient getVideoWithIdentifier:cookies:completionHandler:] & initWithVideoIdentifier:cookies:languageIdentifier:
  • Add the ability to fetch videos allowed to be watched on youtube only (#315, #309)
  • Deprecated XCDYouTubeVideoPlayerViewController (#157)
  • Fixed internal JavaScript exception (#375, #397)

Version 2.6.1 -

  • Adaptation to YouTube API change. (#406)

Version 2.6.0 -

  • Adaptation to YouTube API change. (#383, #388, #385, #382)
  • Include DASH formats URLs in streamURLs. (#354)
  • Add new autoGeneratedCaptionURLs and captionURLs properties to XCDYouTubeVideo. (#281)
  • Fix Messaging unqualified id warnings. (#393)
  • Deprecated smallThumbnailURL, mediumThumbnailURL, largeThumbnailURL. Use thumbnailURL instead.

Version 2.5.6 -

  • Xcode 9 compatibility (#345, #347, #349)

Version 2.5.5 -

  • Adaptation to YouTube API change. (#314)

Version 2.5.4 -

  • Adaptation to YouTube API change. (#295, #296, #297)
  • Upgrade deployment target to iOS 8.0 (macOS is still 10.9 and tvOS is still 9.0)

Version 2.5.3 -

  • Adaptation to YouTube API change. (#257)

Version 2.5.2 -

  • Ensure that all the streams are downloadable at full speed. (#177)
  • Post the video notification after setting the movie player’s contentURL property. (#233)
  • Fixed playback of some age restricted videos.

Version 2.5.1 -

  • Adaptation to YouTube API change. (#231)
  • Fixed compilation issue with Xcode 7.3 beta. (#228)
  • Properly annotated the preferredVideoQualities property as null_resettable.

Version 2.5.0 -

  • Logging is fully configurable with the new XCDYouTubeLogger class.
  • Added XCDYouTubeAllowedCountriesUserInfoKey for better diagnosis of some unplayable (geoblocked) videos. (#216)