I want to save and retrieve the password and userAccount in keychain. I found a solution in stackoverflow and I use the code there. Save and Load from KeyChain | Swift. But this code only saves and load the password not the accountName. I think I figured out how to save the accountName but I need to figure out how to load it along with the password. Here is the code to save you some time from going into that link.
var userAccount = "AuthenticatedUser"
let accessGroup = "SecurityService"
// Mark: User defined keys for new entry
// Note: add new keys for new secure item and use them in load and save methods
let accountKey = "KeyForAccount"
let passwordKey = "KeyForPassword"
private class func save(service: String, data: String) {
let dataFromString: NSData = data.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue), allowLossyConversion: false)! as NSData
//Instantiate a new default keychain query
let keychainQuery: NSMutableDictionary = NSMutableDictionary(objects: [kSecClassGenericPasswordValue, service, userAccount, dataFromString], forKeys: [kSecClassValue, kSecAttrServiceValue as NSCopying, kSecAttrAccountValue as NSCopying, kSecValueDataValue as NSCopying])
//Add new keychain item
let status = SecItemAdd(keychainQuery as CFDictionary, nil)
if status != errSecSuccess { //Always check status
print("Write failed. Attempting update.")
//updatePassword(token: data)
}
}
private class func load(service: String) -> String? {
//Instantiate a new default keychain query
//Tell the query to return a result
//Limit our results to one item
let keychainQuery: NSMutableDictionary = NSMutableDictionary(objects: [kSecClassGenericPasswordValue, service, userAccount, kCFBooleanTrue, kSecMatchLimitOneValue], forKeys: [kSecClassValue, kSecAttrServiceValue as NSCopying, kSecAttrAccountValue as NSCopying, kSecReturnDataValue as NSCopying, kSecMatchLimitValue as NSCopying])
var dataTypeRef: AnyObject?
//Search for the keychain items
let status : OSStatus = SecItemCopyMatching(keychainQuery, &dataTypeRef)
var contentsOfKeychain: String? = nil
if status == errSecSuccess {
if let retrieveData = dataTypeRef as? Data {
contentsOfKeychain = String(data: retrieveData as Data, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))
}
} else {
print("Nothing was retrieved from the keychain. Status code \(status)")
}
print(contentsOfKeychain ?? "none found")
return contentsOfKeychain
}
Complete code is inside the link. Thanks all.
But this code only saves and load the password not the accountName.
The code you show doesn't save or load the password or the account name.
What you have are two functions, one to save a key/value pair, one to load a key's value. It is how you call these two functions that determines what gets saved/loaded.
Somewhere in your code you call save to save the password, you need to call save (using a different key) to save the account name as well. Likewise for load. Your code does define the two keys to use (accountKey & passwordKey).
HTH
Related
I was trying to pre-populate the custom field value from C# code but I was not able to. I have used following snippet of code to do so
var client = new Client("MY API KEY");
var request = new TemplateSignatureRequest();
request.AddTemplate("docID");
request.Title = "Test Titel";
request.Subject = "Test Subject";
request.Message = "Test Message.";
request.AddSigner("Me", "xxxxx#example.com", "Test");
request.AddSigner("Client", "xxxxx#example.com", "Test");
var firstName = request.GetCustomField("FirstName");
firstName.Value = "John";
request.TestMode = true;
var response = client.SendSignatureRequest(request);
I have already assigned the customer field to me and verified the API ID key for the field also.But still not able to get the result. Is there anything else we need to add to so that value John will be shown.
Try
request.AddCustomField("fName", "Alex");
Also, please feel free to reach out to apisupport#hellosign.com for more assistance if you would like.
I am writing a visual studio extension that has a section on the code review page. I would like access to the information about the rest of the code review page, specifically what code review is current on the page being displayed. I should be able to access the workitemId but so far I have not figured out how.
Edit
Using Cole's suggestion I have accessed the PageContent but I do not know what type I should cast the content to. Nor do I know when it will be available. I would like access both when I initialize my section, and later. Here is my code when I try to initialize the section:
public override object SectionContent
{
get
{
if (base.SectionContent == null)
{
var teamExplorerPage = this.GetService<ITeamExplorerPage>();
var pageContent = teamExplorerPage.PageContent;
this.model.CodeReviewId = pageContent;
base.SectionContent = new CodePlusTeamExplorerSectionView(this.ServiceProvider, this.model);
}
return base.SectionContent;
}
}
When I debug the code I see that a DataContext is available on the PageContent, but I do not know what type to cast the pageContent (orITeamExplorerPage) to, to gain access to it. Also the DataContext has a CodeReviewId property which seems like the value I need but it is null at this point of the Lifecycle. If I want to retrieve some additional data based on the CodeReviewId when/where is it available?
I am still trying to figure out how to get it earlier in the lifecycle of the page but from an event like a button click in the page I can retrieve the value using reflection. This seems like a bit of a hack, perhaps someone else has a better way, but here it is.
private void Button_Click(object sender, RoutedEventArgs e)
{
var teamExplorer = this.GetService<ITeamExplorer>();
var currentPage = teamExplorer.CurrentPage;
var currentContent = currentPage.PageContent;
Type type = currentContent.GetType();
PropertyInfo pi = type.GetProperty("DataContext");
var dataContext = pi.GetValue(currentContent);
Type contextTypetype = dataContext.GetType();
PropertyInfo contextTypePi = contextTypetype.GetProperty("CodeReviewId");
var codeReviewId = contextTypePi.GetValue(dataContext);
var context = new Dictionary<string, object>();
context.Add("WorkItemId", codeReviewId);
teamExplorer.NavigateToPage(new Guid(TeamExplorerPageIds.ViewCodeReview), context);
}
Please be patient and Do Not flag this as duplicate: Using the Rally REST API, how can I get the non-API (website) URL for a user story?
I want to be able to generate a link for the user story.
Something like this: https://rally1.rallydev.com/#/-/detail/userstory/*********
As opposed to this: https://rally1.rallydev.com/slm/webservice/v2.0/hierarchicalrequirement/88502329352
The link will be integrated into another application for the managers to see the user story.
I did read about the getDetailUrl() method, but in my case I am creating the user stories by parsing email and linking that to a notification service in Slack.
I am aware of the formattedID and (_ref), but I would have to query for it again, and I am creating batches of userstories through a loop. I need the actual web site link to the user story.
Here is my sample code:
public void CreateUserStory(string workspace, string project, string userstoryName){
//authenticate with Rally
this.EnsureRallyIsAuthenticated();
//DynamicJsonObject for HierarchicalRequirement
DynamicJsonObject toCreate = new DynamicJsonObject();
toCreate[RallyConstant.WorkSpace] = workspace;
toCreate[RallyConstant.Project] = project;
toCreate[RallyConstant.Name] = userstoryName;
try
{
//Create the User Story Here
CreateResult createUserStory = _api.Create(RallyConstant.HierarchicalRequirement, toCreate);
Console.WriteLine("Created Userstory: " + "URL LINK GOES HERE");
}
catch (WebException e)
{
Console.WriteLine(e.Message);
}
}
We don't have a method in the .NET toolkit for doing this, but it's easy to create.
The format is this:
https://rally1.rallydev.com/#/detail/<type>/<objectid>
Just fill in the type (hierarchicalrequirement turns into userstory, but all the others are the same as the wsapi type) and the objectid from the object you just created.
var parameters = new NameValueCollection();
parameters["fetch"] = "FormattedID";
var toCreate = new DynamicJsonObject();
var createResult = restApi.create("hierarchicalrequirement", toCreate, parameters);
var type = Ref.getTypeFromRef(createResult.Reference);
var objectID = Ref.getOidFromRef(createResult.Reference);
var formattedID = createResult.Object["FormattedID"];
And you can specify fetch fields to be returned on the created object so you don't have to re-query for it.
I am using Swift 3 and facebook login, and save some basic values i get from the facebookSDKGraphrequest in user defaults like this:
// save basic settings in standard user defaults: age, gender, first name
let defaults = UserDefaults.standard
defaults.set(firstName!, forKey: "firstName")
defaults.set(gender!, forKey: "gender")
let currentUserAge = calculateAge(birthday: birthDay!)
defaults.set(currentUserAge, forKey: "age")
I only need to run that code once when the user logs in, then I save the info into userDefaults, and I'm set.
so the issue i Have is that recently, the last few times I build and open my app on the phone, my userDefaults was empty.
when I run this code in another class,
let defaults = UserDefaults.standard
var name = defaults.object(forKey: "firstName") as? String
var age = defaults.integer(forKey: "age")
var gender = defaults.object(forKey: "gender") as? String
I was getting nil for all those values. So now I'm paranoid because if this happens in a user's app, it will crash.
Can someone explain why UserDefaults would lose its memory?
I did nothing to delete or reset the values. I went through probably 100 builds and this last time, UserDefaults' values were nil.
NSUserDefaults does not store immediately its content to disk. synchronize method is called periodically to write data to disk. It seems it is not called in your case. Try call defaults.synchronize when you have finished setting up your values.
(source : https://developer.apple.com/reference/foundation/userdefaults/1414005-synchronize)
I have a booking program in Google Sheets where a user can pick their name (uses data verification to provide a list of emails) and then the cell is then placed under protection so no other user can change the booking.
The strange thing that happens is that a person can enter in another person's email and then the cell is protected by the entered email not the user. The user can enter in a non-email string and it does not protect the cell.
The desired result would be that if the user's email and the data entered is the same, protect the cells otherwise it is free to be edited.
Any help would be appreciated!
function onEdit(e){
var CurrentUser = Session.getEffectiveUser().getEmail()
var range_DataEntered = SpreadsheetApp.getActiveRange();
var DataEntered = range_DataEntered.getValue();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var SheetName = SpreadsheetApp.getActiveSheet();
var Cell = ss.getActiveCell();
if (CurrentUser = DataEntered) {
var protection = range_DataEntered.protect()
// Ensure the current user is an editor before removing others. Otherwise, if the user's edit
// permission comes from a group, the script will throw an exception upon removing the group.
protection.addEditor(CurrentUser);
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
}
else {
//remove protection, set everyone to edit
range_DataEntered.protect().setDomainEdit(true);
}
}
if (CurrentUser = DataEntered) needs to be
if(CurrentUser === DataEntered)
A single = will assign a value not check for equivalency.