• Switch之iCloud的配置和使用(一)


    前言: 如果我们想存一些数据,并且换了一个设备登录,存储的数据还能查找到,就可以使用iCloud

     在使用iCloud之前,需要判断当前设备的iCloud账号是否可用

    import CloudKit
    
    class ViewController: UIViewController{
    
        let container = CKContainer.default()
        override func viewDidLoad() {
            super.viewDidLoad()
    
            NotificationCenter.default.addObserver(self, selector: #selector(applicaitonBecameActive(notification:)), name: UIApplication.didBecomeActiveNotification, object: nil)
            NotificationCenter.default.addObserver(self, selector: #selector(applicaitonWillResignActiveNotification(notification:)), name: UIApplication.willResignActiveNotification, object: nil)
        }
       
        
        
        @objc func applicaitonBecameActive(notification:NSNotification){
            //当用户的iCloud账号发生变化时,调用该方法
            NotificationCenter.default.addObserver(self, selector: #selector(handleIdentityChanged(notification:)), name: NSUbiquitousKeyValueStore.didChangeExternallyNotification, object: nil)
        }
        @objc func applicaitonWillResignActiveNotification(notification:NSNotification){
            NotificationCenter.default.removeObserver(self, name: NSUbiquitousKeyValueStore.didChangeExternallyNotification, object: nil)
        }
        
        @objc func handleIdentityChanged(notification:NSNotification){
            let fileManager = FileManager()
            if let token = fileManager.ubiquityIdentityToken{
                print("new token:(token)")
            }else{
                print("用户退出iCloud")
            }
        }
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            container.accountStatus {[weak self] (status, error) in
                    if error != nil{
                        print("---------error:(error)")
                    }else{
                        switch status{
                        case .couldNotDetermine:
                            print("couldNotDetermine:获取账号出错")
                            break
                        case .available:
                            print("available:可用")
    
                            break
                        case .restricted:
                            print("restricted:受限制")
                            break
                        case .noAccount:
                            print("noAccount:无账号")
                            break
                        default:
                            print("default")
                        }
                    }
                
            }
        }
        deinit {
            NotificationCenter.default.removeObserver(self)
        }
    }
    
  • 相关阅读:
    [LeetCode] 771. Jewels and Stones
    [LeetCode] 129. Sum Root to Leaf Numbers
    java定时器demo
    Spring Boot与监控管理
    springboot与热部署
    springboot中的web项目不能访问templates中的静态资源
    @Component 和 @Bean 的区别
    springcluoud入门
    Dubbo和Zookerper的关系
    Spring boot配置Dubbo三种方式
  • 原文地址:https://www.cnblogs.com/hualuoshuijia/p/12040028.html
Copyright © 2020-2023  润新知