How to detect total available/free disk space on the iPhone/iPad device?

后端 未结 18 1766
别跟我提以往
别跟我提以往 2020-11-22 11:14

I\'m looking for a better way to detect available/free disk space on the iPhone/iPad device programmatically.
Currently I\'m using the NSFileManager to detect the disk s

18条回答
  •  失恋的感觉
    2020-11-22 11:31

    Swift implementation of above code:-

    import UIKit
    
    class DiskInformation: NSObject {
    
        var totalSpaceInBytes: CLongLong = 0; // total disk space
        var totalFreeSpaceInBytes: CLongLong = 0; //total free space in bytes
    
        func getTotalDiskSpace() -> String { //get total disk space
            do{
            let space: CLongLong = try FileManager.default.attributesOfFileSystem(forPath: NSHomeDirectory())[FileAttributeKey.systemSize] as! CLongLong; //Check for home dirctory and get total system size
                totalSpaceInBytes = space; // set as total space
                return memoryFormatter(space: space); // send the total bytes to formatter method and return the output
    
            }catch let error{ // Catch error that may be thrown by FileManager
                print("Error is ", error);
            }
            return "Error while getting memory size";
        }
    
        func getTotalFreeSpace() -> String{ //Get total free space
            do{
                let space: CLongLong = try FileManager.default.attributesOfFileSystem(forPath: NSHomeDirectory())[FileAttributeKey.systemFreeSize] as! CLongLong;
                totalFreeSpaceInBytes = space;
                return memoryFormatter(space: space);
    
            }catch let error{
                print("Error is ", error);
            }
            return "Error while getting memory size";
        }
    
        func getTotalUsedSpace() -> String{ //Get total disk usage from above variable
            return memoryFormatter(space: (totalSpaceInBytes - totalFreeSpaceInBytes));
        }
    
        func memoryFormatter(space : CLongLong) -> String{ //Format the usage to return value with 2 digits after decimal
            var formattedString: String;
    
            let totalBytes: Double = 1.0 * Double(space);
            let totalMb: Double = totalBytes / (1024 * 1024);
            let totalGb: Double = totalMb / 1024;
            if (totalGb > 1.0){
                formattedString = String(format: "%.2f", totalGb);
            }else if(totalMb >= 1.0){
                formattedString = String(format: "%.2f", totalMb);
            }else{
                formattedString = String(format: "%.2f", totalBytes);
            }
            return formattedString;
        }
    
    
    }
    

    Call it from any other class.

    func getDiskInfo(){
            let diskInfo = DiskInformation();
            print("Total disk space is", diskInfo.getTotalDiskSpace(),"Gb");
            print("Total free space is", diskInfo.getTotalFreeSpace(),"Gb");
            print("Total used space is", diskInfo.getTotalUsedSpace(),"Gb");
        }
    

    While testing the returned value, it's same as shown by other apps. At least in my iPhone 6S+. It's just the swift implementation of the above shown answer. And for me the accepted answer didn't work.

提交回复
热议问题