
- Swift Tutorial
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Data Types
- Swift - Variables
- Swift - Optionals
- Swift - Tuples
- Swift - Constants
- Swift - Literals
- Swift - Operators
- Swift - Decision Making
- Swift - Loops
- Swift - Strings
- Swift - Characters
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift - Functions
- Swift - Closures
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift - Initialization
- Swift - Deinitialization
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Type Casting
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift Useful Resources
- Swift - Compile Online
- Swift - Quick Guide
- Swift - Useful Resources
- Swift - Discussion
How to download a video using a URL and save it in an photo album using Swift?
To download a video from a URL in swift we need to perform a few steps while keeping a few things in mind.
Points to be noted here are,
We’ll be making use of internet to download video, hence we need to allow permissions for App transport security in our Info.plist
We’ll need to save the downloaded video to Photos app, hence photos permission is required.
Video should always be downloaded in background as it may prevent us from using the app if downloaded on foreground.
Now, we’ll use the below code to save a video from a random link in our Device. You’ll need to allow photos permission while running the app.
Now first add the following code to your info.plist file.
<key>NSPhotoLibraryUsageDescription</key> <string>saves</string> <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>
After this, now add the function shown below and call it inside the viewDidLoad method of your class.
Now your viewDidLoad should look like −
override func viewDidLoad() { super.viewDidLoad() self.downloadVideo() }
Below is the code for function to download a video.
func downloadVideo() { let sampleURL = "http://commondatastorage.googleapis.com/gtv-videosbucket/sample/ElephantsDream.mp4"DispatchQueue.global(qos: .background).async { if let url = URL(string: sampleURL), let urlData = NSData(contentsOf: url) { let galleryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]; let filePath="\(galleryPath)/nameX.mp4" DispatchQueue.main.async { urlData.write(toFile: filePath, atomically: true) PHPhotoLibrary.shared().performChanges({ PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: URL(fileURLWithPath: filePath)) }) { success, error in if success { print("Succesfully Saved") } else { print(error?.localizedDescription) } } } } } }
As there is no other UI element on the screen do not expect to see any result on the screen, only the print statements will be shown in output and if successfully done, the video will be saved to photos which can be seen in the Photos app.
- Related Articles
- How to download and save an image from a given URL in android?
- How to Download a YouTube video Using YouTube Premium
- How to download any file and save it to the desired location using Selenium Webdriver?
- How to download mp3 tracks from youtube video using youtube dl
- Get the YouTube video ID from a URL using JavaScript
- How to download a file from a URL in C#?
- How to download a file using Node.js?
- How do I load an image by URL on iOS device using Swift?
- How to save an Android Activity state using save instance state?
- How to create a WebView in an iOS App using Swift?
- How to save a csv and read using fread in R?
- How to save an Image in OpenCV using C++?
- How to extract text from a web page using Selenium and save it as a text file?
- Loading/Downloading an image from a URL in Swift
- How to download image from url in Android?
