Found 517 Articles for Swift

How to download a video using a URL and save it in an photo album using Swift?

Jennifer Nicholas
Updated on 29-Jun-2020 14:07:25

2K+ Views

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.plistWe’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 ... Read More

How to Add Live Camera Preview to UIView in Swift?

Anvi Jain
Updated on 30-Jul-2019 22:30:24

582 Views

To add a live camera preview to our default UIView in swift we can either use AVFoundation framework of iOS SDK or native UIImagePickerController(). In this example we’ll be using ImagePicker as our aim is to present camera preview on the UIView and Imagepicker is suitable for that task. AVFoundation can be used when we need a lot of customization on our camera or different types of custom actions.To show a camera preview on the UIView we need to perform the following steps.Create a UIImagePickerController object.Conform our class to UIImagePickerControllerDelegate and UINavigationControllerDelegate.Assign delegates to the object we created in step ... Read More

Detect current device with UIUserInterfaceIdiom in Swift

Anvi Jain
Updated on 30-Jul-2019 22:30:24

2K+ Views

To detect current device with iOS/Swift we can use UserInterfaceIdiom. It is an enum in swift, which tells which device is being used.The interface idiom provides multiple values in it’s enum which are.case unspecified @available(iOS 3.2, *) case phone // iPhone and iPod touch style UI @available(iOS 3.2, *) case pad // iPad style UI @available(iOS 9.0, *) case tv // Apple TV style UI @available(iOS 9.0, *) case carPlay // CarPlay style UIIn swift interfaceIdiom can be used in the following way:print(UIDevice.current.userInterfaceIdiom) if UIDevice.current.userInterfaceIdiom == .phone { print("running on iPhone") }When we run the above code ... Read More

How to detect shake gesture using Swift?

Rishi Rathor
Updated on 29-Jun-2020 14:03:26

662 Views

To detect a shake gesture in iOS UIKit provides three different methods, let’s see them one by one.Method 1 − When the shake gesture begins.override func motionBegan(_ motion: UIEvent.EventSubtype, with event: UIEvent?) { // code you want to implement }Method 2 − When the shake gesture ends.override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) { // Code you want to implement. }Method 3 − when the shake gesture is cancelled.override func motionCancelled(_ motion: UIEvent.EventSubtype, with event: UIEvent?) { // code you want to implement. }Now let’s add some code in our motionBegan method, override func motionBegan(_ motion: UIEvent.EventSubtype, with event: ... Read More

How to subtract 1 hour from current time using Swift?

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:24

3K+ Views

To subtract hours from a date in swift we need to create a date first. Once that date is created we have to subtract hours from that, though swift does not provide a way to subtract date or time, but it provides us a way to add date or date component in negative value. In this example we’ll see how we can achieve the same.Let’s create a date first, let it be today, let today = Date()Now to modify this date we’ll use the add function with negative value, let modifiedDate = Calendar.current.date(byAdding: .hour, value: -2, to: today)!Now to see ... Read More

Check if string contains special characters in Swift

Anvi Jain
Updated on 29-Jun-2020 14:04:33

2K+ Views

To check if a string contains a special character in swift we can use conditionals like if else or switch but that would need a lot of conditions to be executed, making programming as well as execution time consuming. So in this example we’ll see how to do the same task with regular expressions and another method that swift provides to check if some character exists in a character set.Method 1 − Using regular expressionLet’s create an extension of String and add the following code into thatextension String {    var containsSpecialCharacter: Bool {       let regex = ... Read More

Email & Phone Validation in Swift

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:24

1K+ Views

To validate email and phone in swift language we can use multiple conditional statements like if conditions, but that’s a long process and may contain 50-100s of if statements to validate email.So instead of conditionals we’ll use Regular expression. Swift provides NSPredicates which we can use to evaluate a regular expression and test them.Let’s see how we can use regular expressions to do the same.We’ll create a function which we can use as extension of String class or UIViewController to use through out the project.Add the following code to any class in your Project, or create a separate swift class ... Read More

Advertisements