Notice
Recent Posts
Recent Comments
Link
ยซ   2024/05   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

lgvv98

[iOS15] Device์˜ ๋„คํŠธ์›Œํฌ ์—ฐ๊ฒฐ์—ฌ๋ถ€ ํ™•์ธ ๐Ÿ“ก ๋ณธ๋ฌธ

โš ๏ธ deprecated โš ๏ธ/๐Ÿ˜ท ์ฝ”๋กœ๋‚˜19์•Œ๋ฆผ์•ฑ

[iOS15] Device์˜ ๋„คํŠธ์›Œํฌ ์—ฐ๊ฒฐ์—ฌ๋ถ€ ํ™•์ธ ๐Ÿ“ก

๐Ÿฅ• ์บ๋Ÿฟ๋งจ 2021. 11. 5. 00:28

โœ… ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” Device์˜ ๋„คํŠธ์›Œํฌ ์—ฐ๊ฒฐ ์ƒํƒœ๋ฅผ ํ™•์ธํ•˜๋Š” ์ฝ”๋“œ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณผ ์˜ˆ์ •์ด์•ผ. 

 

๋‚ด๊ฐ€ ๋งŒ๋“  ์•ฑ์˜ ํ•ต์‹ฌ ๊ธฐ๋Šฅ์€ API ํ†ต์‹ ์„ ํ•„์ˆ˜์ ์œผ๋กœ ํ•ด์•ผํ•œ๋‹ค๋Š” ๊ฒƒ์ด์˜€์–ด.

 

๊ทธ! ๋ž˜! ์„œ!

 

โœ… DeviceManager

//
//  DeviceConfig.swift
//  Covid19AlarmApp
//
//  Created by Hamlit Jason on 2021/08/01.
//

import Foundation
import SystemConfiguration

class DeviceManager {
    static let shared : DeviceManager = DeviceManager()
    
    var networkStatue : Bool {
        get {
            return checkDeviceNetworkStatus()
        }
    }
    
    private init() {
        
    }
    
    private func checkDeviceNetworkStatus() -> Bool {
            print("Check to Device Natwork Status....")
            var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
            zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
            zeroAddress.sin_family = sa_family_t(AF_INET)
            
            let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
                $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
                    SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
                }
            }
            
            var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)
            if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false {
                return false
            }
            
            // Working for Cellular and WIFI
            let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
            let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
            let ret = (isReachable && !needsConnection)
            return ret
        }
}

์œ„์˜ ์ฝ”๋“œ๋Š” ๋„คํŠธ์›Œํฌ ์ƒํƒœ๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ DeviceManager ํด๋ž˜์Šค์•ผ

 

โœ… ์œ„์˜ DeviceManager๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๋งŒ๋“  ํ•จ์ˆ˜

   func checkDeviceNetworkStatus() {
        
        if !(DeviceManager.shared.networkStatue) {
            let alert: UIAlertController = UIAlertController(title: "๋„คํŠธ์›Œํฌ ์ƒํƒœ ํ™•์ธ", message: "๋„คํŠธ์›Œํฌ๊ฐ€ ๋ถˆ์•ˆ์ • ํ•ฉ๋‹ˆ๋‹ค.", preferredStyle: .alert)
            let action: UIAlertAction = UIAlertAction(title: "๋‹ค์‹œ ์‹œ๋„", style: .default, handler: { (action) in
                self.checkDeviceNetworkStatus()
            })
            alert.addAction(action)
            present(alert, animated: true, completion: nil)
        } else { 
        	// ๋„คํŠธ์›Œํฌ ์ƒํƒœ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™ํ•˜๊ณ  ์žˆ์„ ์‹œ
            // do Something... 
        }
    }

์ด๋ ‡๊ฒŒ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด.

Comments