Working with Access Control in swift?

点点圈 提交于 2019-12-12 06:57:42

问题


I want to get device ID in at all classes level in swift which will be included as parameter every time async call made.And I really don't get clearly about Apple Access Control.So,I really need help

So,How to make saving device id at start point and them accessible to all classes for async request?

In app,didFinishLaunchingWithOptions,I will add the following code let say keychain is the library where i store value

if keychain.get("deviceID") == nil{
     keychain.set(UIDevice.currentDevice().identifierForVendor!.UUIDString,forKey : ”deviceID”)
     //some private var deviceID = keychain.get(“deviceID”)
}
else{
    some private var deviceID = keychain.get(“deviceID”)
}

And I want to access that private var at everywhere in the UIViewController class.

class ViewController : UIViewController {
     let deviceID = //some private var with getter?
}

Is there any idea that i can do in swift?using Apple Access Control?

And that problem is connected to that question : Getting Device ID is not unique


回答1:


define your deviceID as static and you will be able to access it everywhere

after your code to initiate the keychain, set it as a static at some class

class DeviceArg {
 static var deviceId = keychain.get(“deviceID”)
}


// some other place in the code
let check = DeviceArg.deviceId //get deviceId



回答2:


Check My Code :-

var mytoken1: String = deviceToken.description.stringByReplacingOccurrencesOfString("<", withString: "")
var mytoken2: String = mytoken1.stringByReplacingOccurrencesOfString(">", withString: "")
mytoken2 = mytoken2.stringByReplacingOccurrencesOfString(" ", withString: "")
NSUserDefaults.standardUserDefaults().setValue(mytoken2, forKey: "DEVICE_TOKEN")
NSUserDefaults.standardUserDefaults().synchronize()


来源:https://stackoverflow.com/questions/35520040/working-with-access-control-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!