From ae82c83eef2fc7c383fd844c7471613e4ac1c7ee Mon Sep 17 00:00:00 2001 From: Olivier Bouillet <62574056+freeboub@users.noreply.github.com> Date: Sun, 22 Sep 2024 18:41:25 +0200 Subject: [PATCH] fix(ios): Add safety checks and remove some of the ! in types declaration (#4182) --- ios/Video/Features/RCTVideoUtils.swift | 40 ++++++++++++++------------ ios/Video/RCTVideo.swift | 18 ++++++------ 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/ios/Video/Features/RCTVideoUtils.swift b/ios/Video/Features/RCTVideoUtils.swift index 703b35fbb8..329b26ff3a 100644 --- a/ios/Video/Features/RCTVideoUtils.swift +++ b/ios/Video/Features/RCTVideoUtils.swift @@ -81,22 +81,25 @@ enum RCTVideoUtils { return 0 } - static func urlFilePath(filepath: NSString!, searchPath: FileManager.SearchPathDirectory) -> NSURL! { - if filepath.contains("file://") { - return NSURL(string: filepath as String) + static func urlFilePath(filepath: NSString?, searchPath: FileManager.SearchPathDirectory) -> NSURL! { + guard let _filepath = filepath else { return nil } + + if _filepath.contains("file://") { + return NSURL(string: _filepath as String) } // if no file found, check if the file exists in the Document directory - let paths: [String]! = NSSearchPathForDirectoriesInDomains(searchPath, .userDomainMask, true) - var relativeFilePath: String! = filepath.lastPathComponent + let paths: [String] = NSSearchPathForDirectoriesInDomains(searchPath, .userDomainMask, true) + var relativeFilePath: String = _filepath.lastPathComponent // the file may be multiple levels below the documents directory - let directoryString: String! = searchPath == .cachesDirectory ? "Library/Caches/" : "Documents" - let fileComponents: [String]! = filepath.components(separatedBy: directoryString) + let directoryString: String = searchPath == .cachesDirectory ? "Library/Caches/" : "Documents" + let fileComponents: [String] = _filepath.components(separatedBy: directoryString) if fileComponents.count > 1 { relativeFilePath = fileComponents[1] } - let path: String! = (paths.first! as NSString).appendingPathComponent(relativeFilePath) + guard let _pathFirst = paths.first else { return nil } + let path: String = (_pathFirst as NSString).appendingPathComponent(relativeFilePath) if FileManager.default.fileExists(atPath: path) { return NSURL.fileURL(withPath: path) as NSURL } @@ -135,7 +138,7 @@ enum RCTVideoUtils { return [] } - let audioTracks: NSMutableArray! = NSMutableArray() + let audioTracks = NSMutableArray() let group = await RCTVideoAssetsUtils.getMediaSelectionGroup(asset: asset, for: .audible) @@ -146,14 +149,14 @@ enum RCTVideoUtils { if (values?.count ?? 0) > 0, let value = values?[0] { title = value as! String } - let language: String! = currentOption?.extendedLanguageTag ?? "" + let language: String = currentOption?.extendedLanguageTag ?? "" let selectedOption: AVMediaSelectionOption? = player.currentItem?.currentMediaSelection.selectedMediaOption(in: group!) let audioTrack = [ "index": NSNumber(value: i), "title": title, - "language": language ?? "", + "language": language, "selected": currentOption?.displayName == selectedOption?.displayName, ] as [String: Any] audioTracks.add(audioTrack) @@ -178,7 +181,7 @@ enum RCTVideoUtils { if (values?.count ?? 0) > 0, let value = values?[0] { title = value as! String } - let language: String! = currentOption?.extendedLanguageTag ?? "" + let language: String = currentOption?.extendedLanguageTag ?? "" let selectedOption: AVMediaSelectionOption? = player.currentItem?.currentMediaSelection.selectedMediaOption(in: group!) let textTrack = TextTrack([ "index": NSNumber(value: i), @@ -363,10 +366,11 @@ enum RCTVideoUtils { static func prepareAsset(source: VideoSource) -> (asset: AVURLAsset?, assetOptions: NSMutableDictionary?)? { guard let sourceUri = source.uri, sourceUri != "" else { return nil } var asset: AVURLAsset! - let bundlePath = Bundle.main.path(forResource: source.uri, ofType: source.type) ?? "" - let url = source.isNetwork || source.isAsset - ? URL(string: source.uri ?? "") - : URL(fileURLWithPath: bundlePath) + let bundlePath = Bundle.main.path(forResource: sourceUri, ofType: source.type) ?? "" + guard let url = source.isNetwork || source.isAsset + ? URL(string: sourceUri) + : URL(fileURLWithPath: bundlePath) else { return nil } + let assetOptions: NSMutableDictionary! = NSMutableDictionary() if source.isNetwork { @@ -375,9 +379,9 @@ enum RCTVideoUtils { } let cookies: [AnyObject]! = HTTPCookieStorage.shared.cookies assetOptions.setObject(cookies as Any, forKey: AVURLAssetHTTPCookiesKey as NSCopying) - asset = AVURLAsset(url: url!, options: assetOptions as? [String: Any]) + asset = AVURLAsset(url: url, options: assetOptions as? [String: Any]) } else { - asset = AVURLAsset(url: url!) + asset = AVURLAsset(url: url) } return (asset, assetOptions) } diff --git a/ios/Video/RCTVideo.swift b/ios/Video/RCTVideo.swift index 21027ca8e3..751c7b4a64 100644 --- a/ios/Video/RCTVideo.swift +++ b/ios/Video/RCTVideo.swift @@ -48,12 +48,12 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH private var _preventsDisplaySleepDuringVideoPlayback = true private var _preferredForwardBufferDuration: Float = 0.0 private var _playWhenInactive = false - private var _ignoreSilentSwitch: String! = "inherit" // inherit, ignore, obey - private var _mixWithOthers: String! = "inherit" // inherit, mix, duck - private var _resizeMode: String! = "cover" + private var _ignoreSilentSwitch: String = "inherit" // inherit, ignore, obey + private var _mixWithOthers: String = "inherit" // inherit, mix, duck + private var _resizeMode: String = "cover" private var _fullscreen = false private var _fullscreenAutorotate = true - private var _fullscreenOrientation: String! = "all" + private var _fullscreenOrientation: String = "all" private var _fullscreenPlayerPresented = false private var _fullscreenUncontrolPlayerPresented = false // to call events switching full screen mode from player controls private var _filterName: String! @@ -741,14 +741,14 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH @objc func setIgnoreSilentSwitch(_ ignoreSilentSwitch: String?) { - _ignoreSilentSwitch = ignoreSilentSwitch + _ignoreSilentSwitch = ignoreSilentSwitch ?? "inherit" RCTPlayerOperations.configureAudio(ignoreSilentSwitch: _ignoreSilentSwitch, mixWithOthers: _mixWithOthers, audioOutput: _audioOutput) applyModifiers() } @objc func setMixWithOthers(_ mixWithOthers: String?) { - _mixWithOthers = mixWithOthers + _mixWithOthers = mixWithOthers ?? "inherit" applyModifiers() } @@ -1050,9 +1050,9 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH @objc func setFullscreenOrientation(_ orientation: String?) { - _fullscreenOrientation = orientation + _fullscreenOrientation = orientation ?? "all" if _fullscreenPlayerPresented { - _playerViewController?.preferredOrientation = orientation + _playerViewController?.preferredOrientation = _fullscreenOrientation } } @@ -1224,7 +1224,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH } @objc - func setAdTagUrl(_ adTagUrl: String!) { + func setAdTagUrl(_ adTagUrl: String?) { _adTagUrl = adTagUrl }