0

I want to give my user the option to "share" my apps detailed view page.

Once the user clicks "tweet this" I want the initialText to include a date string ('release_date') that's being parsed with JSON. I'm having trouble displaying the date string in a readable format.

This is how I'm formatting the date (in a NSObject)

- (NSString *) formattedDate {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
    NSDate *readableDate = [dateFormatter dateFromString:self.release_date];

    [dateFormatter setDateFormat:@"MMMM dd"];
    return [dateFormatter stringFromDate:readableDate];
}

My release_date is a NSString (this is how the JSON looks)

release_date: "2013-11-16T00:00:00.000Z"

This is how I'm displaying the formattedDate in my detailedViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    if([_singleRelease objectForKey:@"release_date"] != NULL)
    {
        NSString *readableDate = [_singleRelease objectForKey:@"release_date"];
        UpcomingRelease *singleRelease = [[UpcomingRelease alloc] init];
        singleRelease.release_date = readableDate;

        self.release_date.text = [NSString stringWithFormat:@"%@", singleRelease.formattedDate];
    }
}

My SLComposeViewController

- (IBAction)shareOnTwitter:(id)sender {
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *tweetSheet = [SLComposeViewController
                                               composeViewControllerForServiceType:SLServiceTypeTwitter];
        [tweetSheet setInitialText:[NSString stringWithFormat:@"%@ is releasing on %@, via @soleresource", [_singleRelease objectForKey:@"release_name"], [_singleRelease objectForKey:@"release_date"]]];
        [self presentViewController:tweetSheet animated:YES completion:nil];
    }
}

Thanks

4

1 回答 1

0

您的代码有点杂乱无章。一种选择是这样的:

- (NSString *) formattedDate:(NSString *)jsonDateString {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
    NSDate *readableDate = [dateFormatter dateFromString:jsonDateString];

    [dateFormatter setDateFormat:@"MMMM dd"];

    return [dateFormatter stringFromDate:readableDate];
}

- (IBAction)shareOnTwitter:(id)sender {
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *tweetSheet = [SLComposeViewController
                                               composeViewControllerForServiceType:SLServiceTypeTwitter];
        NSString *formattedDate = [self formattedDate:[_singleRelease objectForKey:@"release_date"]];
        [tweetSheet setInitialText:[NSString stringWithFormat:@"%@ is releasing on %@, via @soleresource", [_singleRelease objectForKey:@"release_name"], formattedDate]];
        [self presentViewController:tweetSheet animated:YES completion:nil];
    }
}
于 2013-11-12T16:40:54.930 回答