Skip to content

Instantly share code, notes, and snippets.

View PraveenKommuri's full-sized avatar
🎯
Focusing

Praveen Kommuri PraveenKommuri

🎯
Focusing
View GitHub Profile
@PraveenKommuri
PraveenKommuri / CellularNetworkConectionType
Last active November 2, 2021 21:22
Get the Cellular network connection type i.e. 2G, 3G & LTE etc.
// Support iOS 14+
func getNetworkConnectionType() -> String {
var connectionType: String = "Unknown"
let networkInfo = CTTelephonyNetworkInfo()
let carrierType = networkInfo.serviceCurrentRadioAccessTechnology
guard let carrierTypeName = carrierType?.first?.value else {
return connectionType
@PraveenKommuri
PraveenKommuri / UniversalLinkTest_Simulator
Created May 10, 2021 18:41
Test your universal link in simulator
Universal link testing from terminal
xcrun simctl openurl booted “https://www.way.com/parkingdetails/1579270/Holiday-Inn-San-Francisco-SFO-Airport-Parking”
@PraveenKommuri
PraveenKommuri / CustomUserDefaults
Created April 20, 2021 19:35
CustomUserDefaults for writing unit tests. If we use suite name, it won't impact other test cases in same/other files.
//Initialization
var userDefaultsCustom: UserDefaults?
let userDefaultsSuiteName = "UserStateManagerTests" //You can pass class name here.
// In setUp() method
UserDefaults().removePersistentDomain(forName: userDefaultsSuiteName)
userDefaultsCustom = UserDefaults(suiteName: userDefaultsSuiteName)
// In test cases methods.
userDefaultsCustom?.setValue("SomeRandomValue", forKey: "SomeRandomKey")
@PraveenKommuri
PraveenKommuri / Generics_Stack_Implementation
Created April 15, 2021 21:23
Implemented Stack Data Structure with/without Generics
//Without using generics
struct StackEx {
private var items: [Any] = []
mutating func push(item: Any) { items.append(item) }
mutating func pop() -> Any { items.popLast() as Any }
var top: Any { return items.last as Any }
mutating func getAll() -> [Any] { return items }
}
var stacktEx = StackEx()
@PraveenKommuri
PraveenKommuri / UIButton_Programmatically_Constraints
Created March 30, 2021 05:49
Programmatically creating UIButton & set the constraints in code. Setting the button in middle of the screen for box x&y axis.
let takeVideoButton = UIButton(type: .system)
//takeVideoButton.frame = CGRect(x: 30, y: 30, width: 100, height: 50)
takeVideoButton.setTitle("Take Video", for: .normal)
takeVideoButton.addTarget(self, action: #selector(takeVideo), for: .touchUpInside)
self.view.addSubview(takeVideoButton)
takeVideoButton.translatesAutoresizingMaskIntoConstraints = false
let horizontalConstraint = NSLayoutConstraint(item: takeVideoButton, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1, constant: 0)
let verticalConstraint = NSLayoutConstraint(item: takeVideoButton, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1, constant: 0)
@PraveenKommuri
PraveenKommuri / DocumentDirectory_SaveFile_ReadFile
Last active March 30, 2021 00:10
Get the document directory. save the video file & txt file & read the same.
/// Get the app document direcitory path
/// - Returns: path as URL
func getDocumentsDirectory() -> URL {
// Find all possible documents directories for this user
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
// just send back the first one, Usually it will be only one
return paths[0]
}
@PraveenKommuri
PraveenKommuri / StringExtension
Created March 26, 2021 00:20
String Extension Playground
extension String {
/// Method to check whether the string has only characters.
/// - Returns: bool value.
func isNumber() -> Bool {
return !isEmpty && rangeOfCharacter(from: CharacterSet.decimalDigits.inverted) == nil
}
/// Method to validate email address string
/// - Returns: BOOL
@PraveenKommuri
PraveenKommuri / Parse Data object
Created March 25, 2021 00:15
Function will parse the data object based on model what we pass in decode method.
func parse(jsonData: Data) -> sampleRecord? {
do {
let decodedData = try JSONDecoder().decode(sampleRecord.self, from: jsonData)
return decodedData
} catch {
print("error: \(error)")
}
return nil
}
@PraveenKommuri
PraveenKommuri / Reading local JSON file
Created March 25, 2021 00:03
Function will read the local json file & return the content as Data object
func readLocalJSONFile(forName name: String) -> Data? {
do {
if let filePath = Bundle.main.path(forResource: name, ofType: "json") {
let fileUrl = URL(fileURLWithPath: filePath)
let data = try Data(contentsOf: fileUrl)
return data
}
} catch {
print("error: \(error)")
}
@PraveenKommuri
PraveenKommuri / Playground Validation
Created March 24, 2021 23:06
Test your methods in playground through XCTestCase
import XCTest
// Sample class with one method in it.
class evenOddClass {
func isEvenNumber(_ number: Int) -> Bool {
if number % 2 == 0 {
return true
} else {
return false