Accessing imageurl using userdefaults in objective c - objective-c

I have fetched the image from image picker and uploaded the image to web service and i have a image url.I have saved the imageurl using user defaults as given below:
-(void)postSignupService {
[NewJsonHelperClass postExecuteWithParams:#"signup" secondParm:[self signUpParams] onCompletion:^(NSDictionary *json){
if ([[json valueForKey:#"status"] isEqualToString:#"success"]) {
NSDictionary *dataDict =[NSDictionary new];
dataDict =[json valueForKey:#"user"];
[[NSUserDefaults standardUserDefaults] setObject:#"sucess" forKey:#"LoginStatus"];
[[NSUserDefaults standardUserDefaults] setObject:[dataDict valueForKey:#"_id"] forKey:#"user_Id"];
[[NSUserDefaults standardUserDefaults] setObject:[dataDict valueForKey:#"userName"] forKey:#"userName"];
[[NSUserDefaults standardUserDefaults] setObject:[dataDict valueForKey:#"password"] forKey:#"Password"];
[[NSUserDefaults standardUserDefaults] setObject:[dataDict valueForKey:#"email"] forKey:#"email_Id"];
[[NSUserDefaults standardUserDefaults] setObject:[dataDict valueForKey:#"profile_pic"] forKey:#"image_Str"];
NSLog(#"datadict is %#",dataDict);
SelectFoodVC *selectVc = [appDelegateRef.storyBoard instantiateViewControllerWithIdentifier:#"SelectFoodVC"];
[self.navigationController pushViewController:selectVc animated:YES];
// TabBarController *tabVc = [appDelegateRef.storyBoard instantiateViewControllerWithIdentifier:#"TabBarController"];
// [self.navigationController pushViewController:tabVc animated:YES];
}
else{
[reUse showAlertWithTitle:[json valueForKey:#"status"] message:[json valueForKey:#"user"] thirdParam:self];
}
}];
}
-(NSDictionary *)signUpParams {
NSMutableDictionary *params =[NSMutableDictionary new];
params[#"userName"] =self.userNameTf.text;
params[#"email"] =self.emailTf.text;
params[#"password"] =self.passwordTf.text;
NSLog(#"imgstr is %#",imgStr);
params[#"profile_pic"] =imgStr;
return params;
}
Here profile pic value is imgStr which is got as imageurl from web service.
When i try to retrieve the imageurl in different viewcontroller,the value of userdefault is null as it shows error.
NSString *urlstring = [[NSUserDefaults standardUserDefaults] objectForKey:#"image_Str"];
NSLog(#"urlstring is %#",urlstring);
Here urlstring is null and the error is Error: Request failed: not found (404)

NSString *str = [NSString stringWithFormat:#"http://swiftdeveloperblog.com/wp-content/uploads/2015/07/1.jpeg"];
NSURL *url = [NSURL URLWithString:str];
[[NSUserDefaults standardUserDefaults]setURL:url forKey:#"URL"];
[[NSUserDefaults standardUserDefaults]synchronize];
NSString *getURL = [[NSUserDefaults standardUserDefaults]URLForKey:#"URL"];
NSLog(#"url is %#",getURL);

Related

IOS how to query Username and Password like from a plist

I want to code a LoginView in Xcode, to query the Password and Username i want to use a Textfile or plist. How can i use it?
Generally passwords are either taken from service call or from userdefaults:
You can do something as :
- (IBAction)submitPressed:(id)sender {
NSString *userName = [[NSUserDefaults standardUserDefaults] objectForKey:#"userName"];
if (userName == NULL) {//no user pwd exists. create a new one
if (self.userTextField.text.length>0 && self.passwordTextField.text.length>0) {
NSLog(#"Create a user+pwd in server. Now for showing purpose we are saving it in userdefaults.");
[[NSUserDefaults standardUserDefaults] setObject:self.userTextField.text forKey:#"userName"];
[[NSUserDefaults standardUserDefaults] setObject:self.passwordTextField.text forKey:#"password"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
else{ //some user pwd exists, now check that with json value.
NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:#"password"];
if ([userName isEqualToString:self.userTextField.text] &&
[password isEqualToString:self.passwordTextField.text]) {
NSLog(#"logged in successfully");
//[self performSegueWithIdentifier:#"HomeView" sender:nil];
}
else{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Login"
message:#"Invalid username and/or password"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil, nil];
[alertView show];
}
}
}
Edit:
If you still want to read user/password from text file of plist you can use :
//PLIST
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Credentials" ofType:#"plist"]];
NSString *user = dictionary[#"username"];
NSString *password = dictionary[#"password"];
//TEXT
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"Credentials" ofType:#"txt"];
NSString *contents = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:NULL];
//then use NSString api's to extract user and password from contents
Here is the lines you need to write to save the username and password to save in the PList which is in the Documents Directory..
- (IBAction)saveToPlist:(id)sender {
NSDictionary *loginDetails=[[NSDictionary alloc]initWithObjects:#[self.userName.text,self.password.text] forKeys:#[#"username",#"password"]];
if ([loginDetails writeToFile:[self plistPath] atomically:YES]) {
NSLog(#"successfully entered");
}
else{
NSLog(#"something wrong");
}
}
//code to retrieve the data from plist
NSDictionary *details=[[NSDictionary alloc]initWithContentsOfFile:[self plistPath]];
where the [self plistPath] is
-(NSString*)plistPath{
NSArray *arr=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [[arr objectAtIndex:0]stringByAppendingPathComponent:#"login.plist"];
}
Happy Coding :)

NSMutableArray isn't adding my NSString

Im trying to add a NSString to an NSMutableArray and then make the array into NSData and save it with NSUserDefaults. But the array is always nil.
Here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
library = [[ALAssetsLibrary alloc] init];
groups = [[NSMutableArray alloc] init];
userDefaults = [[NSUserDefaults alloc] init];
NSData *data = [userDefaults objectForKey:#"GroupArray"];
groups = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(#"%i", [groups count]);
}
-(IBAction)newFolder {
if (textField.text != nil) {
NSString *string = textField.text.capitalizedString;
[library addAssetsGroupAlbumWithName:string
resultBlock:^(ALAssetsGroup *group) {
NSLog(#"Created a folder named: %#", group);
}
failureBlock:^(NSError *error) {
NSLog(#"An error occured! - %#", error);
}
];
[groups addObject:string];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:groups];
[userDefaults setObject:data forKey:#"GroupArray"];
NSLog(#"%i", [groups count]);
}
[self.view sendSubviewToBack:subView];
}
When the app starts i get a message in the console that the array is NULL. When I try to add the string the NSLog(#"%i", [groups count]); always return 0.
Why does this happen?
userDefaults = [[NSUserDefaults alloc] init];
NSData *data = [userDefaults objectForKey:#"GroupArray"];
In this case, data will be nil when the code is executed for the first time, since there is yet no "GroupArray" property present.
groups = [NSKeyedUnarchiver unarchiveObjectWithData:data];
This causes groups be become nil as well, because calling unarchiveObjectWithData: with nil as an argument will return nil as well.
And because of all that, in -newFolder, [groups addObject:string] becomes [nil addObject:string]
Calling a method on nil is allowed in Objective-C, so you get no exception there. The return value of any method called on nil is, again, nil or 0.
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:groups];
This causes data to be nil because groups is nil.
So you are always calling:
[userDefaults setObject:data forKey:#"GroupArray"];
with data = nil
Quick fix:
Add if (groups == nil) groups = [NSMutableArray array]; to the beginning of -newFolder
In view didLoad method every time you are initialising new instance of NSUserDefaults and calling
NSData *data = [userDefaults objectForKey:#"GroupArray"]; on the newly created object
which will return nil all the time because in the new instance there wont be any object for key #"GroupArray". Instead replace userDefaults with singleton object [NSUserDefaults standardUserDefaults]
Modify your code as shown below
- (void)viewDidLoad
{
[super viewDidLoad];
library = [[ALAssetsLibrary alloc] init];
NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:#"GroupArray"];
groups = [NSKeyedUnarchiver unarchiveObjectWithData:data];
if (!groups)
{
groups = [[NSMutableArray alloc] init];
}
NSLog(#"group %#", groups);
NSLog(#"%i", [groups count]);
}
Your newFolder method
-(IBAction)newFolder
{
if (textField.text != nil)
{
NSString *string = textField.text.capitalizedString;
[library addAssetsGroupAlbumWithName:string
resultBlock:^(ALAssetsGroup *group) {
NSLog(#"Created a folder named: %#", group);
}
failureBlock:^(NSError *error) {
NSLog(#"An error occured! - %#", error);
}
];
[groups addObject:string];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:groups];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:#"GroupArray"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"%i", [groups count]);
}
[self.view sendSubviewToBack:subView];
}
This should work. As it worked for me.

Removing objects from array in NSUserDefaults

When setting NSUserDefaults, I was initially using this code to set the defaults...
NSMutableArray *array = [NSMutableArray arrayWithObjects: #"string1", #"string2", #"string3", nil];
[[NSUserDefaults standardUserDefaults] setObject:array forKey: #"preset1"];
[[NSUserDefaults standardUserDefaults] synchronize];
I've learned that I should be using this instead:
NSMutableArray *array = [NSMutableArray arrayWithObjects: #"string1", #"string2", #"string3", nil];
NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:array forKey:#"preset1"];
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
Now I'm having an issue manipulating the objects later on in array. Here is the code I use to add/remove strings from array. It worked fine when I was initially setting the defaults manually in my first example. Now, the objects will not remove from the array. I did notice when printing the array in LLDB debugger that array is now being stored as a NSCFArray when it was just an NSArray before.
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObjectsFromArray:[[NSUserDefaults standardUserDefaults] objectForKey:#"preset1"]];
NSArray *stringsToRemove = #[#"string1", #"string2" ];
for (NSUInteger i = 0; i < stringsToRemove.count; i++) {
[array removeObjectIdenticalTo:[stringsToRemove objectAtIndex:i]];
}
[[NSUserDefaults standardUserDefaults] setObject:array forKey: #"preset1"];
[[NSUserDefaults standardUserDefaults] synchronize];
This code works for me with your setup, after initializing the defaults the second way you described:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *array = [[NSMutableArray alloc] initWithArray: [defaults objectForKey:#"preset1"]];
NSArray *stringsToRemove = [NSArray arrayWithObjects:#"string1", #"string2", nil];
for (NSString *aString in stringsToRemove) {
[array removeObjectIdenticalTo:aString];
}
[defaults setObject:array forKey: #"preset1"];
[defaults synchronize];

How to check the latest version of the app with NSUserDefaults

I want to check if user is using the latest version of the app. If so, i should color the background of some cells.
This is the code i'm using:
appDelegate.m
NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:#"lastVer"];
method where i want to check:
NSString *lastVersion = (NSString *) [[NSUserDefaults standardUserDefaults] objectForKey:#"lastVer"];
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleVersion"];
lastVersion = #"1.3.4";
if(![lastVersion isEqualToString:version]){
cell.backgroundColor = [UIColor yellowColor]; ;
cell.imageView.image = [UIImage imageNamed:#"image.png"];
}
else {
cell.imageView.image = nil;
cell.backgroundColor = [UIColor whiteColor];
}
}
Am i doing it right? Is my code checking for the latest version or? How can i simulate this behavior in the simulator \ on device?
What i want to do is check if the user is using the latest version of the app, in order to call in another NSUserDefaults key to show the cells of my tableView with a different background color.
EDIT
Code i'm using to change the cells background if the user is using the latest version of my app and if the user has used the app less than three times:
NSString *cellValue = cell.textLabel.text;
NSNumber *runNumber = (NSNumber *)[[NSUserDefaults standardUserDefaults] objectForKey:#"runNum"];
NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:#"lastVer"];
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleVersion"];
if (!runNumber) {
runNumber = [NSNumber numberWithInt:0];
[[NSUserDefaults standardUserDefaults] setObject:runNumber forKey:#"runNum"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
if([lastVersion isEqualToString:version]){ //check if the user is using the latest version
if ([runNumber intValue] < 4) { //check if user has used the app less than 3 times
if ([cellValue isEqual: "someText"] ){
cell.backgroundColor = [UIColor yellowColor]; ;
cell.imageView.image = [UIImage imageNamed:#"image.png"];
}
else {
cell.imageView.image = nil;
cell.backgroundColor = [UIColor whiteColor];
}
}
else {
cell.imageView.image = nil;
cell.backgroundColor = [UIColor whiteColor];
}
}
}
this code will not set anything..
NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:#"lastVer"];
lastVersion = #"1.3.4";
you need a setter:
[[NSUserDefaults standardUserDefaults] setObject: #"1.3.4" forKey:#"lastVer"];//in memory
[[NSUserDefaults standardUserDefaults] synchronize];//to disk
and a getter:
NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:#"lastVer"];
edit:
remember that lastVersion is a pointer and you just set it to point somewhere else, that doesn't set it in stdUserDefaults and it doesn't synchronize it to disk.
Surely if you want to check if the user is using the latest version of the app you would need to check with an external resource.
e.g.
Simple HTTP request to your own server, which returns your latest app version.
Then compare this against your current version [[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleVersion"];
Store the result of this test in NSUserDefaults

Objective-C save NSColor in NSUserDefaults

How do you save the color of the dot so that when the app is opened and closed the dot is the color it was last set to by the user?
Could someone explain to me how to use NSUserDefaults and in which methods to declare NSUserDefaults.
So far i have this:
NSData *data = [NSArchiver archivedDataWithRootObject:color];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:#"MyColor"];
NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:#"MyColor"];
NSColor *color = [NSUnarchiver unarchiveObjectWithData:data];
Link for tutorial I followed:
http://www.mactech.com/articles/mactech/Vol.25/25.04/2504RoadtoCode/index.html
This is what I use:
- (NSColor *)colorForKey:(NSString *)key
{
NSData *data;
NSColor *color;
data = [[NSUserDefaults standardUserDefaults] objectForKey:key];
color= [NSUnarchiver unarchiveObjectWithData:data];
if( ! [color isKindOfClass:[NSColor class]] )
{
color = nil;
}
return color;
}
- (void)setColor:(NSColor *)color forKey:(NSString *)key
{
NSData *data = [NSArchiver archivedDataWithRootObject:color];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:key];
[BFChatWindow refresh];
}