Convert NSDate from string - ios6

I'm displaying an NSDate stored in core data into a UITextField so the user can edit it.
I'm using NSDateFormatter to format the date into "dd-MM-yyyy" format, which works fine.
When the user presses save, i want to convert this string back into a date and update the core data record, which is fine, but i also want to convert it to a different format.
So from 15-01-2004 to 2004-01-15
This is what i have
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-yyyy"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:#"yyyy-mm-dd"];
[formatter2 setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[urlParams appendFormat:#"&DOB=%#", [formatter2 stringFromDate:date]];
The problem i'm having is i'm getting the date back as 2004-00-15 instead of 2004-01-15
I'm obviously misunderstanding something

[formatter2 setDateFormat:#"yyyy-mm-dd"];
to
[formatter2 setDateFormat:#"yyyy-MM-dd"];
mm is minutes

Related

nsfilemanager createFileAtPath results in NSFileCreationDate being null

I am creating an image file from a json object and the file creation is successful and the image is indeed created and can be displayed. I want a process that will check if the file is still in cache or a tmp directory before downloading the image. If the image is indeed there then I want to check the creation date and modification date attributes of the file to see if I need to download a new version of the image to keep it up to date. This is where the problem comes in.
The creation and modification date attributes are null. I've even attempted to manually set the attributes and it didn't change anything.
Here's the code I use to create the file:
NSString *fileName = [NSHomeDirectory() stringByAppendingPathComponent: [NSString stringWithFormat:#"tmp/%#",theFileName]];
BOOL filecreationSuccess =[fileManager createFileAtPath:fileName contents:myData attributes:nil];
if(filecreationSuccess == NO){
NSLog(#"Failed to create the file");
}
I have even tried adding the Code and the attributes on the file are still null:
NSDate *now = [NSDate date];
NSDictionary *createDateAttr = [NSDictionary dictionaryWithObjectsAndKeys: now, NSFileCreationDate,nil];
NSDictionary *modDateAttr = [NSDictionary dictionaryWithObjectsAndKeys: now, NSFileModificationDate, nil];
[fileManager setAttributes:modDateAttr ofItemAtPath:fileName error:&err];
[fileManager setAttributes:createDateAttr ofItemAtPath:fileName error:&err];
I am unsure of what to do next. Any help would be greatly appreciated, Thank you.
I have found my problem. It wasn't that the modification and creation dates weren't being set, but that I was not getting the correct set of properties for the file. It ended up being that the code I was using was getting the properties for the path that the file was in, not the file itself.

UIActivityViewController Create Separate Mail icon

I am trying to create a UIActivityViewController that lets the user send in feedback about the current web page that is being viewed. I want to have a separate button from the Mail icon to pop up with a compose window with a recipient set, subject set, and the message body to include some text and the current URL (I get the URL with: NSURL *urlStringToShare = _webView.request.URL; in the RootViewController). Any ideas how to do this? Thanks for your help.
For example, I already have an IBAction that does this:
- (IBAction)showEmail:(id)sender {
// Email Subject
NSString *emailTitle = #"Feedback on your latest column from the GlennKessler App:";
// Email Content
NSString *messageBody3 = #"I disagree with your latest Fact Check and the number of pinocchios you gave. This is what I would rate it instead and why:";
// To address
NSArray *toRecipents = [NSArray arrayWithObject:#"blah#la.com"];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody3 isHTML:NO];
[mc setToRecipients:toRecipents];
// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:NULL];
}
Any way to convert this into a UIActivityViewController item with an icon? Thanks!
Try using this, You can create a number of Custom Activityies such as mail using the Library found at this link:
https://github.com/imulus/OWActivityViewController

NSXMLParser failed in iOS 6 with NSXMLParserNAMERequiredError [duplicate]

I'm parsing some HTML with NSXMLParser and it hits a parser error anytime it encounters an ampersand. I could filter out ampersands before I parse it, but I'd rather parse everything that's there.
It's giving me error 68, NSXMLParserNAMERequiredError: Name is required.
My best guess is that it's a character set issue. I'm a little fuzzy on the world of character sets, so I'm thinking my ignorance is biting me in the ass.
The source HTML uses charset iso-8859-1, so I'm using this code to initialize the Parser:
NSString *dataString = [[[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding] autorelease];
NSData *dataEncoded = [[dataString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES] autorelease];
NSXMLParser *theParser = [[NSXMLParser alloc] initWithData:dataEncoded];
Any ideas?
To the other posters: of course the XML is invalid... it's HTML!
You probably shouldn't be trying to use NSXMLParser for HTML, but rather libxml2
For a closer look at why, check out this article.
Are you sure you have valid XML? You are required to have special characters like & escaped, in the raw XML file you should see &
Encoding the Data through a NSString worked for me, anyway you are autoreleasing an object that was not allocated by yourself (dataUsingEncoding), so it crashes, the solution is :
NSString *dataString = [[NSString alloc] initWithData:data
encoding:NSISOLatin1StringEncoding];
NSData *dataEncoded = [dataString dataUsingEncoding:NSUTF8StringEncoding
allowLossyConversion:YES];
[dataString release];
NSXMLParser *theParser = [[NSXMLParser alloc] initWithData:dataEncoded];

How to set the starting orientation to landscape?

Hi I was checking the orientations changes of iOS6 and I made everything work fine except one thing. There is no way to start the app on landscape.
How can I do to start the app on landscape? I found this How to force a UIViewController to Portrait orientation in iOS 6 but that's not working, the app ALWAYS start in portrait and I needed to start it on landscape...
When I go to an other view and then go back to the "initial view" it is on landscape! But when the app starts it's on portrait...
Thank you!!
----------------------------- UPDATE -------------------------------
This is how I'm loading from the app delegate the main view:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
myViewController = [[myViewController alloc] init];
self.navController = [[UINavigationController alloc] initWithRootViewController:myViewController] ;
[self.window setRootViewController:navController];
[self.window makeKeyAndVisible];
----------------------------- UPDATE 2------------------------------
I made it work, I changed the previous code for:
self.navController = [[UINavigationController alloc] init] ;
[self.window setRootViewController:navController];
[self.window makeKeyAndVisible];
[navController presentViewController:myViewController animated:NO completion:NULL];
I hope this is useful for someone else!
Have you tried this?
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];
or
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
This is working for me in both iOS 6 and iOS 5
I have another problem on trying to rotate the screen than I realize I was making the mistake of try to load the new screen with:
[self.navigationController presentModalViewController:controller animated:YES];
instead of:
[self.navigationController pushViewController:controller animated:YES];
the second one works but the first one wasn't doing what I expected.
This is how I made it work:
self.navController = [[UINavigationController alloc] init] ;
[self.window setRootViewController:navController];
[self.window makeKeyAndVisible];
[navController presentViewController:myViewController animated:NO completion:NULL];
I hope it helps someone else!
I've been wrestling with this too - this is how I've fixed it:
In viewDidLoad:
CGRect frame = self.view.bounds;
if (frame.size.height > frame.size.width) {
//hack to force landscape
CGFloat holdHeight = frame.size.height;
frame.size.height = frame.size.width;
frame.size.width = holdHeight;
self.view.bounds = frame;
}
But I'd prefer a better way.

How to convert cstring to NSString and NSString to cstring?

Lets say i have the following cstring
char array[1000];
How i can convert it to NSString and vice verse.
Thanks.
Apple's Developer Reference has a good article on this subject. Basically, you will do something like this:
NSString *stringFromUTFString = [[NSString alloc] initWithUTF8String:utf8String];
if the string is UTF8 encoded. Otherwise, you can use initWithCString:encoding: with which you can specify the encoding.
Here is a list of available string encodings.

Resources