programming-6977450_1280.jpg

DDUtils是一个常用功能的合集, 基于swift开发,在iOS设备上快速实现常用功能。

仓库地址

github

一、导入项目

通过cocoapods导入

pod 'DDUtils'

如果需要idfa的功能,可以选择导入

pod 'DDUtils/idfa'

通过文件导入

下载项目,将项目文件下的pod文件夹里面的内容导入项目即可

二、API列表

已有的数据类型操作可以通过.zx的语法,其他操作可以通过DDUtils.shared单例来使用。

  • 单例和.zx使用没区别,单例会更统一简单,.zx语法的好处就是不需要在使用的地方导入

UI相关

名称功能说明示例
func getCurrentNormalWindow()获取当前的NormalWindowDDUtils.shared.getCurrentNormalWindow()
func getCurrentVC()获取当前的ViewControllerDDUtils.shared.getCurrentVC()
func getImage(color: UIColor)通过颜色生成一张纯色背景图DDUtils.shared.getImage(color: UIColor.red) <br/>或者<br/> UIImage.dd.getImage(color: UIColor.red)
func getLinearGradientImage(colors: [UIColor], directionType: DDUtilsGradientDirection, size: CGSize = CGSize(width: 100, height: 100))生成线性渐变的图片DDUtils.shared.getLinearGradientImage(colors: [UIColor.red, UIColor.black, UIColor.blue] <br/>或者<br/> UIImage.dd.getLinearGradientImage(colors: [UIColor.red, UIColor.black, UIColor.blue], directionType: .leftToRight)
func getRadialGradientImage(colors: [UIColor], raduis: CGFloat, size: CGSize = CGSize(width: 100, height: 100))生成角度渐变的图片DDUtils.shared.getRadialGradientImage(colors: [UIColor.red, UIColor.black, UIColor.blue], raduis: 45) <br/>或者<br/> UIImage.dd.getRadialGradientImage(colors: [UIColor.red, UIColor.black, UIColor.blue], raduis: 45)
func getColor(hexString: String, alpha: CGFloat = 1.0)通过十六进制字符串获取颜色UIColor.dd.color(hexString: "#FFFFFF")
func UIColor(hexValue: Int, darkHexValue: Int = 0x333333, alpha: Float = 1.0, darkAlpha: Float = 1.0)通过十六进制获取颜色UIColor.dd.color(hexValue: 0xffffff)
UIScreenWidth屏幕宽度
UIScreenHeight屏幕高度
DDUtils_StatusBar_Height状态栏高度
DDUtils_HomeIndicator_HeightHome Indicator高度
func DDUtils_Default_NavigationBar_Height(vc: UIViewController? = nil)导航栏高度DDUtils_Default_NavigationBar_Height()
func func DDUtils_Default_Tabbar_Height(vc: UIViewController? = nil)tabbar高度DDUtils_Default_Tabbar_Height()
func addLayerShadow(color: UIColor, offset: CGSize, radius: CGFloat, cornerRadius: CGFloat? = nil)为view添加阴影view.dd.addLayerShadow(color: UIColor.black, offset: CGSize(width: 2, height: 0), radius: 10)
func setFrame(x: CGFloat? = nil, y: CGFloat? = nil, width: CGFloat? = nil, height: CGFloat? = nil)view单独设置Frame的某个值view.dd.setFrame(x: 10)
func className() -> String获取view的类名button.dd.className()

系统和软件信息

名称功能说明示例
func getAppVersionString()获取软件版本DDUtils.shared.getAppVersionString()
func getAppBuildVersionString()获取软件构建版本DDUtils.shared.getAppBuildVersionString()
func getAppNameString()获取软件的显示名字DDUtils.shared.getAppNameString()
func getIOSVersionString()获取系统的iOS版本DDUtils.shared.getIOSVersionString()
func getIOSLanguageStr()获取系统语言DDUtils.shared.getIOSLanguageStr()
func getBundleIdentifier()获取软件Bundle IdentifierDDUtils.shared.getBundleIdentifier()
func getSystemHardware()获取本机机型标识DDUtils.shared.getSystemHardware()
func getSystemUpTime()获取本机上次重启时间DDUtils.shared.getSystemUpTime()
func getIDFAString(idfvIfFailed: Bool = true)模拟软件唯一标示DDUtils.shared.getIDFAString()
func getMacAddress()获取手机WIFI的MAC地址,需要开启Access WiFi informationDDUtils.shared.getMacAddress()
func openSystemSetting()打开系统设置DDUtils.shared.openSystemSetting()
func openAppStorePage(openType: DDUtilsOpenAppStoreType, appleID: String)打开软件对应的App Store页面DDUtils.shared.openAppStorePage(openType: .app, appleID: "1123211")
func openAppStoreReviewPage(openType: DDUtilsOpenAppStoreType, appleID: String = "")打开软件对应的评分页面DDUtils.shared.openAppStoreReviewPage(openType: .app)

软件权限

名称功能说明示例
func requestPermission(type: DDUtilsPermissionType, complete: @escaping ((DDUtilsPermissionStatus) -> Void))请求权限DDUtils.shared.requestPermission(type: .notification) { (status) in print("权限设置回调", status) }
func checkPermission(type: DDUtilsPermissionType, complete: @escaping ((DDUtilsPermissionStatus) -> Void))检测软件权限DDUtils.shared.checkPermission(type: .notification) { (status) in print("当前权限状态", status) }
func requestIDFAPermission(complete: @escaping ((DDUtilsPermissionStatus) -> Void)) -> Void请求软件IDFA权限DDUtils.shared.requestIDFAPermission { (status) in print("当前idfa权限状态", status) }
func checkIDFAPermission(type: DDUtilsPermissionType, complete: @escaping ((DDUtilsPermissionStatus) -> Void)) -> Void检测软件idfa权限DDUtils.shared.checkIDFAPermission { (status) in print("当前权限状态", status) }

多媒体操作

名称功能说明示例
func getVideoDuration(videoURL: URL) -> Double获取指定video的时长, 单位秒DDUtils.shared.getVideoDuration(videoURL: URL(fileURLWithPath: path))
func getVideoSize(videoURL: URL)获取指定视频的分辨率,支持本地或者网络地址DDUtils.shared.getVideoSize(videoURL: URL(fileURLWithPath: path))
func playMusic(url: URL?, repeated: Bool = false, audioSessionCategory: AVAudioSession.Category = AVAudioSession.Category.playback)播放音乐DDUtils.shared.playMusic(url: url, repeated: false)
func stopMusic()关闭音乐播放DDUtils.shared.stopMusic()
func playEffect(url: URL?, vibrate: Bool = false)播放音效,静音模式不会播放音效DDUtils.shared.playEffect(url: url, vibrate: true)
func startVibrate(repeated: Bool = false)开始震动DDUtils.shared.startVibrate()
func stopVibrate()结束震动DDUtils.shared.stopVibrate()

文件操作

名称功能说明示例
func getFileDirectory(type: DDUtilsFileDirectoryType)获取文件夹路径DDUtils.shared.getFileDirectory(type: .documents)
func createFileDirectory(in type: DDUtilsFileDirectoryType, directoryName: String)在指定文件夹中创建文件夹DDUtils.shared.createFileDirectory(in: .documents, directoryName: "filePath")
func getFileSize(filePath: URL)获取指定文件的大小DDUtils.shared.getFileSize(filePath: url)
func getFileDirectorySize(fileDirectoryPth: URL)获取指定文件夹的大小DDUtils.shared.getFileDirectorySize(fileDirectoryPth: url)

其他

DDUtils

名称功能说明示例
func getDictionary(object: Any, debug: Bool = false) -> [String: Any]遍历获取class\struct的所有属性名和值DDUtils.shared.getDictionary(object: testModel)
func runInMainThread(type: ZXMainThreadType = .default, function: @escaping ()->Void)主线程执行functionDDUtils.shared.runInMainThread(type: .sync) { ... }

String

名称功能说明示例
func subString(rang: NSRange)截取字符串string.dd.subString(rang: NSRange(location: 2, length: 5))
func unicodeDecode()unicode转中文"\u54c8\u54c8\u54c8".dd.unicodeDecode()
func unicodeEncode()字符串转unicode"哈哈是电话费".dd.unicodeEncode()
func encodeString(from originType: DDUtilsEncodeType = .system(.utf8), to encodeType: DDUtilsEncodeType)字符串修改编码显示"5ZOI5ZOI5piv55S16K+d6LS5".dd.encodeString(from: .base64, to: .system(.utf8))
func aesCBCEncrypt(password: String, ivString: String = "abcdefghijklmnop")aes cbc模式加密string.dd.aesCBCEncrypt(password: "password")
func aesCBCDecrypt(password: String, ivString: String = "abcdefghijklmnop")aes cbc解密string.dd.aesCBCDecrypt(password: "password")
func hashString(hashType: DDUtilsHashType, lowercase: Bool = true)字符串hash计算string.dd. hashString(hashType: .md5) <br/> 支持md5/sha1/sha224/sha256/sha384/sha512
func aesGCMEncrypt(password: String, encodeType: DDUtilsEncodeType = .base64, nonce: AES.GCM.Nonce? = AES.GCM.Nonce())aes gcm模式加密string.dd.aesGCMEncrypt(password: "password")
func aesGCMDecrypt(password: String, encodeType: DDUtilsEncodeType = .base64)aes gcm模式解密string.dd.aesGCMDecrypt(password: "password")
func hmac(hashType: DDUtilsHashType, password: String, encodeType: DDUtilsEncodeType = .base64)HMAC计算"DDUtils".dd.hmac(hashType: .sha1, password: "67FG", encodeType: .hex)

Data

名称功能说明示例
static func data(from string: String, encodeType: DDUtilsEncodeType)通过字符串和编码获取数据Data.dd.data(from: "d5a423f64b607ea7c65b311d855dc48f36114b227bd0c7a3d403f6158a9e4412", encodeType: .hex)
func encodeString(encodeType: DDUtilsEncodeType)数据转为指定编码的字符串data.dd.encodeString(encodeType: .hex)
func aesCBCEncrypt(password: String, ivString: String = "abcdefghijklmnop")aes cbc模式加密data.dd.aesCBCEncrypt(password: "password")
func aesCBCDecrypt(password: String, ivString: String = "abcdefghijklmnop")aes cbc解密data.dd.aesCBCDecrypt(password: "password")
func hashString(hashType: DDUtilsHashType, lowercase: Bool = true)字符串hash计算data.dd. hashString(hashType: .md5) <br/> 支持md5/sha1/sha224/sha256/sha384/sha512
func aesGCMEncrypt(password: String, encodeType: DDUtilsEncodeType = .base64, nonce: AES.GCM.Nonce? = AES.GCM.Nonce())aes gcm模式加密data.dd.aesGCMEncrypt(password: "password")
func aesGCMDecrypt(password: String, encodeType: DDUtilsEncodeType = .base64)aes gcm模式解密data.dd.aesGCMDecrypt(password: "password")
func hmac(hashType: DDUtilsHashType, password: String, encodeType: DDUtilsEncodeType = .base64)HMAC计算data.dd.hmac(hashType: .sha1, password: "67FG", encodeType: .hex)

License协议

该项目基于MIT协议,您可以自由修改使用


☟☟可点击下方广告支持一下☟☟

最后修改:2024 年 05 月 24 日
请我喝杯可乐,请随意打赏: ☞已打赏列表