0

嗨,我制作了一个代码,它给了我一个带有日期的网址,如 www.google.com/2012-10-09

但由于某种原因,它不会将我重定向到正确的网址?理解 NSURL URLwithstring 似乎有些问题?

这是我的代码

- (IBAction)sedato:(id)sender {



    //Finds date and make string



    NSDate *ddato = [_datepickout date];

    NSString *vdato = [NSString stringWithFormat:@"%@", ddato];


    NSDateFormatter *datoform = [[NSDateFormatter alloc] init];
    [datoform setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
    [datoform setDateFormat: @"yyyy-MM-dd HH:mm:ss Z"];

    NSDate *datemedform = [datoform dateFromString: vdato];
    NSString *strdate = [[NSString alloc] initWithFormat:@"%@", datemedform];
    NSArray *arr = [strdate componentsSeparatedByString:@" "];
    NSString *endelig;
    endelig = [arr objectAtIndex:0];

    NSArray *arr2 = [endelig componentsSeparatedByString:@"-"];
    NSString *year;
    year = [arr2 objectAtIndex:0];
    NSString *mm;
    mm = [arr2 objectAtIndex:1];
    NSString *dag;
    day = [arr2 objectAtIndex:2];

   NSString *url = [NSString stringWithFormat:@"http://www.coutbound.dk/logon/add_date.asp?view_date=%@/%@/%@",year,mm,day];



//Send url request with string     


    ASIFormDataRequest *request2 = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:url]];

    [request2 setUseKeychainPersistence:YES];
    [request2 setUseCookiePersistence:YES];
    [request2 setDelegate:self];
    [request2 startAsynchronous];




}
    - (void)requestFinished:(ASIHTTPRequest *)request {
        NSLog(@"Saved form successfully");
        NSLog(@"Response was:");
        NSLog(@"%@",[request responseString]);
        NSLog(@"HEADERS: %@",[request responseHeaders]);
        NSLog(@"STATUSCODE: %u",[request responseStatusCode]);
        NSLog(@"COOkies: %@",[request responseCookies]);
    }
4

1 回答 1

1

您正在将日期转换为字符串,然后再转换为日期,再转换为字符串,再转换为组件,然后再转换为字符串!某处一定有错误。

试试这个:

NSDate *myDate = [_datePicker date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy/MM/dd";
NSString *dateStr = [formatter stringFromDate:myDate];
NSString *url = [NSString stringWithFormat:@"%@%@", urlBase, dateStr];

检查结果

NSLog(@"URL: %@", url);
于 2012-10-12T00:48:14.073 回答