1

我正在尝试从iOS 应用程序中www.myurl.com/a?timezone=+n.n使用的表单的 url 请求数据。或参考发出请求的设备的时区。NSMutableURLRequest+n.n-n.n

如何在 url 中填充这个时区参数?

我四处搜索并没有找到与上述格式的时区相关的任何内容。

4

1 回答 1

2

我们需要逐步解决这个问题。首先,让我们得到一个几乎是您想要的字符串(注意,未经测试):

NSDateFormatter *formatter;
NSString *almostThere;

formatter = [[NSDateFormatter alloc] initWithDateFormat:@"ZZZZZ" allowNaturalLanguage:NO];
almostThere = [formatter stringFromDate:[NSDate date]];
[formatter release];

现在almostThere应该类似于-08:00根据日期格式字符串文档(NSDateFormatter 使用 Unicode 格式并引用此文档)。

所以现在剩下的就是用点替换冒号:

NSString *tz;

tz = [almostThere stringByReplacingOccurrencesOfString:@":" withString:@"."];

最后,网址:

NSURL *theURL;
NSString *urlString;

urlString = [NSString stringWithFormat:@"http://www.myurl.com/a?timezone=%@", tz]
theURL = [NSURL URLWithString:urlString];
于 2013-04-24T19:05:43.723 回答