Skip to content

Commit

Permalink
Merge pull request #42 from weiran/yt-applink
Browse files Browse the repository at this point in the history
YouTube app link
  • Loading branch information
weiran authored Jul 10, 2021
2 parents 84ec8bd + b8058f2 commit 3c384aa
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 39 deletions.
59 changes: 40 additions & 19 deletions App/DetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,11 @@ class DetailViewController: UIViewController {
if let title = videoStream.title {
self?.titleLabel.text = title
}
if let description = videoStream.description {
if var description = videoStream.description {
description = description.replacingOccurrences(of: #"\[.*.\] "#, with: "", options: .regularExpression)
self?.descriptionLabel.text = description
}
}.catch { error in
var message = "Watch It Later couldn't open this video."

if let failureReason = (error as NSError).userInfo[NSLocalizedFailureReasonErrorKey] as? String {
message = failureReason
}

let alert = UIAlertController(
title: "Error Opening Video", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel))
self.present(alert, animated: true)
}
}.cauterize()
}

let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(didPlay(_:)))
Expand Down Expand Up @@ -137,7 +127,27 @@ class DetailViewController: UIViewController {
self?.activityIndicator.stopAnimating()
self?.view.isUserInteractionEnabled = true
}.catch { [weak self] error in
self?.showError()
// try to open the video in the YouTube app
if videoProvider is YouTubeProvider,
let url = self?.video?.urlString {
let regex = try! NSRegularExpression(pattern: #"([^\/|=]*$)"#, options: .caseInsensitive)
let range = NSRange(location: 0, length: url.utf16.count)

if let youtubeIdMatch = regex.firstMatch(in: url, options: [], range: range),
youtubeIdMatch.range(at: 0).location != NSNotFound {
let lowerBound = url.index(url.startIndex, offsetBy: youtubeIdMatch.range(at: 0).location)
let youtubeId = url[lowerBound...]

if let youtubeURL = URL(string: "youtube://watch/\(youtubeId)") {
if UIApplication.shared.canOpenURL(youtubeURL) {
UIApplication.shared.open(youtubeURL, options: [:], completionHandler: nil)
return
}
}
}
}

self?.showError(error)
}
}

Expand Down Expand Up @@ -200,11 +210,22 @@ class DetailViewController: UIViewController {
}
}

private func showError() {
let alertController = UIAlertController(title: "Video Error", message: "There's something wrong with the video and it can't be played.", preferredStyle: .alert)
let alertAction = UIAlertAction(title: "OK", style: .default, handler:nil)
alertController.addAction(alertAction)
present(alertController, animated: true)
private func showError(_ error: Error? = nil) {
var message = "Watch It Later couldn't play this video."

if let error = error,
let failureReason = (error as NSError).userInfo[NSLocalizedFailureReasonErrorKey] as? String {
message = failureReason
}

let alert = UIAlertController(
title: "Error Opening Video",
message: message,
preferredStyle: .alert
)

alert.addAction(UIAlertAction(title: "OK", style: .cancel))
present(alert, animated: true)
}

func formatTimeInterval(duration: TimeInterval) -> String {
Expand Down
4 changes: 4 additions & 0 deletions App/Supporting Files/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>youtube</string>
</array>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIMainStoryboardFile</key>
Expand Down
2 changes: 1 addition & 1 deletion Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ target 'WatchItLater' do
pod 'Nuke'
pod 'PromiseKit/CorePromise'
pod 'RealmSwift'
pod 'TVVLCKit'
pod 'TVVLCKit', '~> 3.3.15'
pod 'TVVLCPlayer', :git => 'https://github.com/weiran/TVVLCPlayer', :branch => 'kodlian_master'
# pod 'TVVLCPlayer', :path => "../../Forks/TVVLCPlayer/TVVLCPlayer.podspec"
pod 'SwiftyUserDefaults'
Expand Down
30 changes: 15 additions & 15 deletions Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
PODS:
- Locksmith (4.0.0)
- Nuke (9.2.3)
- PromiseKit/CorePromise (6.13.1)
- Realm (10.7.5):
- Realm/Headers (= 10.7.5)
- Realm/Headers (10.7.5)
- RealmSwift (10.7.5):
- Realm (= 10.7.5)
- SwiftyUserDefaults (5.0.0)
- Nuke (9.5.0)
- PromiseKit/CorePromise (6.15.3)
- Realm (10.7.6):
- Realm/Headers (= 10.7.6)
- Realm/Headers (10.7.6)
- RealmSwift (10.7.6):
- Realm (= 10.7.6)
- SwiftyUserDefaults (5.3.0)
- Swinject (2.7.1)
- SwinjectStoryboard (2.2.0):
- Swinject (~> 2.7.1)
Expand All @@ -25,7 +25,7 @@ DEPENDENCIES:
- SwiftyUserDefaults
- Swinject
- SwinjectStoryboard (from `https://github.com/Swinject/SwinjectStoryboard`, commit `0ca45c83a8aa398c153d8a036c95abb4343cfa0c`, branch `master`)
- TVVLCKit
- TVVLCKit (~> 3.3.15)
- TVVLCPlayer (from `https://github.com/weiran/TVVLCPlayer`, branch `kodlian_master`)
- "XCDYouTubeKit (from `https://github.com/jatin19121990/XCDYouTubeKit`, branch `hotfix/Can-Not-Play-video-any-more---XCDYouTubeVideoErrorDomain-Code=-3-#525`)"
- YTVimeoExtractor (from `https://github.com/lilfaf/YTVimeoExtractor`, commit `57bf479e860abd7dcd5fcbc520062cea3c7b5587`)
Expand Down Expand Up @@ -72,18 +72,18 @@ CHECKOUT OPTIONS:

SPEC CHECKSUMS:
Locksmith: e9bebbaaa4cee3c511bc358a44f843c3fe00e21f
Nuke: 3dd19fd03612c179036d2af7f56ec57cb84144e2
PromiseKit: 28fda91c973cc377875d8c0ea4f973013c05b6db
Realm: 06f5c67f87cb8c5f27b122218ce22e9a77314517
RealmSwift: b46535b52e69b9ed33912f0fcb3182ab0a5743ee
SwiftyUserDefaults: 33fcb42bd1feb53a37d873feb62c82967db5f7f6
Nuke: 6f400a4ea957e09149ec335a3c6acdcc814d89e4
PromiseKit: 3b2b6995e51a954c46dbc550ce3da44fbfb563c5
Realm: ed860452717c8db8f4bf832b6807f7f2ce708839
RealmSwift: e31c4ddbcc42ac879313d656b86f9ca539f6f4f4
SwiftyUserDefaults: 63f80248cf5bfb3458825d9a78f2eb7e1293a040
Swinject: ddf78b8486dd9b71a667b852cad919ab4484478e
SwinjectStoryboard: f0d5b1c16459949d6ff1cbb6610107946b18e1f8
TVVLCKit: c0d238a966ead6bfcc452d5c42c1d273b1003ba7
TVVLCPlayer: 357ae9ec2c52ff6cdd41588fe3e58525640a41eb
XCDYouTubeKit: b120aced3d8638ffb570c5450cddb5a1dac448c7
YTVimeoExtractor: f4ed90f43e08d33d56c1d1aff3f4f64663747966

PODFILE CHECKSUM: 993fbd39128043bad857780ad1d521c58f982491
PODFILE CHECKSUM: 81bb56df429a445bb56d0e14a06013e4f3508348

COCOAPODS: 1.10.1
8 changes: 4 additions & 4 deletions WatchItLater.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -558,15 +558,15 @@
CODE_SIGN_IDENTITY = "Apple Development";
"CODE_SIGN_IDENTITY[sdk=appletvos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 42;
CURRENT_PROJECT_VERSION = 43;
DEVELOPMENT_TEAM = 2KB59GPA9B;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/**",
);
INFOPLIST_FILE = "App/Supporting Files/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MARKETING_VERSION = 2.0.6;
MARKETING_VERSION = 2.0.7;
PRODUCT_BUNDLE_IDENTIFIER = co.weiran.WatchItLater;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
Expand All @@ -590,15 +590,15 @@
CODE_SIGN_IDENTITY = "Apple Development";
"CODE_SIGN_IDENTITY[sdk=appletvos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 42;
CURRENT_PROJECT_VERSION = 43;
DEVELOPMENT_TEAM = 2KB59GPA9B;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/**",
);
INFOPLIST_FILE = "App/Supporting Files/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MARKETING_VERSION = 2.0.6;
MARKETING_VERSION = 2.0.7;
PRODUCT_BUNDLE_IDENTIFIER = co.weiran.WatchItLater;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
Expand Down

0 comments on commit 3c384aa

Please sign in to comment.