Skip to content

Commit

Permalink
fix(ios): Add safety checks and remove some of the ! in types declara…
Browse files Browse the repository at this point in the history
…tion (#4182)
  • Loading branch information
freeboub authored Sep 22, 2024
1 parent 17dc2c0 commit ae82c83
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
40 changes: 22 additions & 18 deletions ios/Video/Features/RCTVideoUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -135,7 +138,7 @@ enum RCTVideoUtils {
return []
}

let audioTracks: NSMutableArray! = NSMutableArray()
let audioTracks = NSMutableArray()

let group = await RCTVideoAssetsUtils.getMediaSelectionGroup(asset: asset, for: .audible)

Expand All @@ -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)
Expand All @@ -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),
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down
18 changes: 9 additions & 9 deletions ios/Video/RCTVideo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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()
}

Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -1224,7 +1224,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
}

@objc
func setAdTagUrl(_ adTagUrl: String!) {
func setAdTagUrl(_ adTagUrl: String?) {
_adTagUrl = adTagUrl
}

Expand Down

0 comments on commit ae82c83

Please sign in to comment.