I would like to add an item to keychain if it does not exists or update if it does exist.
I call SecItemCopyMatching to check if the item exists passing the following query:
NSDictionary *query = #{
(__bridge id)kSecClass: (__bridge id)kSecClassGenericPassword,
(__bridge id)kSecAttrService: #"myservice",
(__bridge id)kSecReturnData: #NO
};
SecItemCopyMatching prompts the user for the touchid/passcode.
After that I need to update the keychain which prompts the user again.
I can store a BOOL in my program that saves the state but that may be out of sync with the value in keychain so I would rather query the keychain itself if the item exists or not but I don't want the user to get double prompted. Is there any way to do that?
A workaround here would be to call update first. It won't prompt you unless the item exists. If the update call returns an errSecItemNotFound, then add the data. This should only prompt you at most once. If you add an item, it shouldn't prompt you at all.
class func updateData(value: NSData, forKey keyName: String) -> Bool {
let keychainQueryDictionary: NSMutableDictionary = self.setupKeychainQueryDictionaryForKey(keyName)
let updateDictionary = [SecValueData:value]
let status: OSStatus = SecItemUpdate(keychainQueryDictionary, updateDictionary)
if status == errSecSuccess {
return true
} else if status == errSecItemNotFound {
return setData(value, forKey: keyName)
} else {
return false
}
}
class func setData(value: NSData, forKey keyName: String) -> Bool {
var keychainQueryDictionary: NSMutableDictionary = self.setupKeychainQueryDictionaryForKey(keyName)
keychainQueryDictionary[SecValueData] = value
var error:Unmanaged<CFErrorRef>?
let sacObject = SecAccessControlCreateWithFlags(kCFAllocatorDefault, kSecAttrAccessibleAfterFirstUnlock, SecAccessControlCreateFlags.UserPresence, &error)
keychainQueryDictionary[SecAttrAccessControl] = sacObject.takeRetainedValue()
let status: OSStatus = SecItemAdd(keychainQueryDictionary, nil)
if status == errSecSuccess {
return true
} else {
return false
}
}
private class func setupKeychainQueryDictionaryForKey(keyName: String) -> NSMutableDictionary {
var attributes = NSMutableDictionary()
attributes[SecClass] = kSecClassGenericPassword as String
attributes[SecAttrService] = "Sample Service 1"
attributes[SecUseOperationPrompt] = "Operation prompt goes here"
attributes[SecAttrAccount] = keyName
return attributes
}
This code is modified from Jason Rendel's KeychainWrapper.
Related
Airwatch documentation is gawdawful. So bear with me.
From Airwatch SDK, how to I get the currently logged in username/user certificate?
It's in the API
//setup authorization profile
var client = new RestClient("https://enterprise.telstra.com/api/mdm");
client.Authenticator=new HttpBasicAuthenticator("#####", "#######");
var request = new RestRequest("devices/search", DataFormat.Json);
request.AddHeader("aw-tenant-code", "##########");
//get the data as json
var response = client.Get(request);
if (response.IsSuccessful)
{
//serialize the data into the Devices[] array RootObject
RootObject ro = new RootObject();
ro = JsonConvert.DeserializeObject<RootObject>(response.Content);
//create a new DataTable with columns specifically required
DataTable dt = new DataTable("tblDevices");
dt.Columns.Add("AssetNumber");
dt.Columns.Add("DeviceFriendlyName");
dt.Columns.Add("IMEI");
dt.Columns.Add("MacAddress");
dt.Columns.Add("Model");
dt.Columns.Add("DeviceType");
dt.Columns.Add("OEMinfo");
dt.Columns.Add("Platform");
dt.Columns.Add("SerialNumber");
dt.Columns.Add("UserEmailAddress");
dt.Columns.Add("UserName");
//iterate through each device data and add it into a DataRow
foreach(Device d in ro.Devices)
{
DataRow dr = dt.NewRow();
dr["UserName"] = d.UserName.ToUpper(); //uppercase the userid
//add the row to the table
dt.Rows.Add(dr);
}
With godly help from the client
AWController.clientInstance().retrieveStoredPublicCertificates { payload, error in
if let _ = error {
return
}
guard let payload = payload as? [String : [PKCS12Certificate]] else {
return
}
processCertificates(payload)
NotificationCenter.default.post(name: .awSDKCeritificatesReceived,
object: payload)
}
Getting the image url null as response from api, is there any way for fix it. Response from api is:
"image_path" = "<null>";
Tried to handle it like this:
if !(arrList.objectAtIndex(indexPath.row).valueForKey("media")?.objectAtIndex(0).valueForKey("image_path") is NSNull) {
// optional is NOT NULL, neither NIL nor NSNull
}
It is still crashing please guide.
Update:
Found the problem, null was not the issue actually. Posting the response below to make things clear.
media = (
{
id = "97eb48a0-429a-11e6-873c-ad7b848648f1";
"image_path" = "https://s3-eu-west-1.mazws.com/facebook_profile_images/15105131.png";
"room_post_id" = "97c572e0-429a-11e6-b72b-fd489f63a1fc";
"user_id" = "66fe22a0-4296-11e6-a1d9-69dc307add4b";
"video_path" = "<null>";
},
{
id = "981a6880-429a-11e6-b039-736a0bf954dc";
"image_path" = "<null>";
"room_post_id" = "97c572e0-429a-11e6-b72b-fd489f63a1fc";
"user_id" = "66fe22a0-4296-11e6-a1d9-69dc307add4b";
"video_path" = "https://s3-eu-west-1.naws.com/facebook_profile_images/1255803.mp4";
}
);
This is the normal response, now the response where trouble is:
media = (
);
Can it be handle any how? Please guide.
Try like this .. it'll not crash
if let imgPath = arrList.objectAtIndex(indexPath.row).valueForKey("media")?.objectAtIndex(0).valueForKey("image_path") as? String {
//... your value is string
}
Let me guess types by information you provided:
if let arrList = arrList as? NSArray,
firstObject = arrList.objectAtIndex(indexPath.row) as? NSDictionary,
media = firstObject.valueForKey("media") as? NSArray,
secondObject = media.objectAtIndex(0) as? NSDictionary,
image_path = secondObject.valueForKey("image_path") as? String {
print(image_path)
// now validate your string
}
Validating your string can be like this:
if !image_path.isEmpty && image_path != "<null>" {
//do something
}
or like this:
if !image_path.isEmpty {
if let image_pathURL = NSURL(string: image_path), _ = image_pathURL.host {
//do something
}
}
Why don't to simple check with == like:
if !(arrList.objectAtIndex(indexPath.row).valueForKey("media")?.objectAtIndex(0).valueForKey("image_path") == "<null>")
if let image = myJson["image_path"] as? String {
imageView.image = image
}
else {
imageview.image = "default.png"
}
also try this
if !(arrList.objectAtIndex(indexPath.row).valueForKey("media")?.objectAtIndex(0).valueForKey("image_path") == "<null>") {
// optional is NOT NULL, neither NIL nor NSNull
}
in my project i want to fetch the key value from the dictionary along with the condition that if key value is Yes then tick image shown and if the key value is no then cross image shown. i am trying to get the key value but not getting the value.
please help me to solve this problem. i am sharing my snapshot n codes
NSDictionary *strName9 = [productDict objectForKey:#"hand_emb"];
inside StrName9 below dictionary i fetched
"hand_emb" = {
1 = {
key = lengha;
value = yes;
};
2 = {
key = Pyjami;
value = no;
};
};
now what to do after this please help me.
What you are accessing is NSDictionary not NSString so replace it with following snippet which iterates through all the elements of NSDictionary.
Objective C
NSDictionary *dict = productDict[#"hand_emb"];
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop)
{
if([obj isKindOfClass:[NSDictionary class]])
{
NSLog(#"key : %#",obj[#"key"]);
NSLog(#"value : %#",obj[#"value"]);
if([obj[#"value"] isEqualToString:#"yes"])
{
// *** Write code to show tick ***
}
else
{
// *** Write code to show cross ***
}
}
}];
Swift
var dict: [NSObject : AnyObject] = productDict["hand_emb"]
dict.enumerateKeysAndObjectsUsingBlock({(key: AnyObject, obj: AnyObject, stop: Bool) -> Void in
if obj.isKindOfClass([NSObject : AnyObject]) {
NSLog("key : %#", obj["key"])
NSLog("value : %#", obj["value"])
if (obj["value"] == "yes") {
// *** Write code to show tick ***
}
else {
// *** Write code to show cross ***
}
}
})
I stumbled upon this piece of Objective-C code:
- (void)updateUser:(NSDictionary<FBGraphUser> *)user inSlot:(NSInteger)slot {
[self validateSlotNumber:slot];
NSString *idKey = [NSString stringWithFormat:SUUserIDKeyFormat, (long)slot];
NSString *nameKey = [NSString stringWithFormat:SUUserNameKeyFormat, (long)slot];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (user != nil) {
NSLog(#"SUUserManager updating slot %li: fbid = %#, name = %#", (long)slot, user.objectID, user.name);
[defaults setObject:user.objectID forKey:idKey];
[defaults setObject:user.name forKey:nameKey];
} else {
NSLog(#"SUUserManager clearing slot %li", (long)slot);
// Can't be current user anymore
if (slot == _currentUserSlot) {
[self switchToNoActiveUser];
}
// FBSample logic
// Also need to tell the token cache to forget the tokens for this user
FBSessionTokenCachingStrategy *tokenCachingStrategy = [self createCachingStrategyForSlot:slot];
[tokenCachingStrategy clearToken];
[defaults removeObjectForKey:idKey];
[defaults removeObjectForKey:nameKey];
}
[defaults synchronize];
[self sendNotification];
}
It is from one of the Facebook iOS SDK examples. I am trying to use it in my Swift written app. This is what I come up with:
func updateUser(user: Dictionary<String, FBGraphUser>, slot: Int) {
var idKey: String = String(format: userIDKeyFormat, slot)
var nameKey: String = String(format: userNameKeyFormat, slot)
var defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
if(user != nil) {
println("UserManager updating slot \(slot): Facebook ID = \(user.objectID), Name = \(user.name)")
defaults.setObject(user.objectID, forKey: idKey)
defaults.setObject(user.name, forKey: nameKey)
} else {
println("UserManager clearing slot \(slot)")
if(slot == currentUserSlot) {
self.switchToNoActiveUser()
}
var tokenCachingStrategy: FBSessionTokenCachingStrategy = self.createCachingStrategyForSlot(slot)
tokenCachingStrategy.clearToken()
defaults.removeObjectForKey(idKey)
defaults.removeObjectForKey(nameKey)
}
defaults.synchronize()
self.sendNotification()
}
The problem is in Dictionary<String, FBGraphUser> which, I think, is just not the same as the Obj-C equivalent. In the if statement to check if user is not nil, Xcode throws an error: 'Dictionary' is not convertible to 'String'.
So how do I translate the Obj-C code to Swift?
Thanks!
You have to set you dictionary as an optional:
func updateUser(user: Dictionary<String, FBGraphUser>?, slot: Int) {...}
After some extensive research I finally found a way:
func updateUser<val: NSDictionary where val:protocol<FBGraphUser>>(user:val!, slot: Int) {
var idKey: String = String(format: userIDKeyFormat, slot)
var nameKey: String = String(format: userNameKeyFormat, slot)
var defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
if(user != nil) {
println("UserManager updating slot \(slot): Facebook ID = \(user!.objectID), Name = \(user!.name)")
defaults.setObject(user!.objectID, forKey: idKey)
defaults.setObject(user!.name, forKey: nameKey)
} else {
println("UserManager clearing slot \(slot)")
if(slot == currentUserSlot) {
self.switchToNoActiveUser()
}
var tokenCachingStrategy: FBSessionTokenCachingStrategy = self.createCachingStrategyForSlot(slot)
tokenCachingStrategy.clearToken()
defaults.removeObjectForKey(idKey)
defaults.removeObjectForKey(nameKey)
}
defaults.synchronize()
self.sendNotification()
}
In my today extension with my device unlocked, this line of code works as expected, returning the data from the image path:
let imageData = NSData(contentsOfFile: path)
However when my device is locked with a passcode, it returns nil. Is there any way to access images in the file system when the device is locked? I can access UserDefaults just fine, but not files in the directory for my shared group. Here is how I am creating the path, calling imagePath, which is correctly populated with the path I expect in both cases:
func rootFilePath() -> String? {
let manager = NSFileManager()
let containerURL = manager.containerURLForSecurityApplicationGroupIdentifier(GROUP_ID)
if let unwrappedURL = containerURL {
return unwrappedURL.path
}
else {
return nil
}
}
func imagePath() -> String? {
let rootPath = rootFilePath()
if let uPath = rootPath {
return "\(uPath)/\(imageId).png"
}
else {
return nil
}
}
I just figured it out! You need to set the file permissions accordingly:
NSFileManager *fm = [[NSFileManager alloc] init];
NSDictionary *attribs = #{NSFileProtectionKey : NSFileProtectionNone};
NSError *unprotectError = nil;
BOOL unprotectSuccess = [fm setAttributes:attribs
ofItemAtPath:[containerURL path]
error:&unprotectError];
if (!unprotectSuccess) {
NSLog(#"Unable to remove protection from file! %#", unprotectError);
}
In many cases you wouldn't normally want to do this, but because the information is intended to be viewed from the lock screen, I'm OK with removing file protection.