現在の場所の現在の季節 (春、夏、冬、秋) を見つけるために iOS にとにかくありますか?
世界のさまざまな地域では季節が異なります。同じ時期にアメリカの季節が別のものであるのに、オーストラリアでは別の季節を経験するかもしれません。
その場所の季節を返す天気APIまたは何か?
現在の場所の現在の季節 (春、夏、冬、秋) を見つけるために iOS にとにかくありますか?
世界のさまざまな地域では季節が異なります。同じ時期にアメリカの季節が別のものであるのに、オーストラリアでは別の季節を経験するかもしれません。
その場所の季節を返す天気APIまたは何か?
アプリケーションで現在のシーズンを知る必要があり、あなたの投稿を見ました。
だから、たとえそれが古い投稿であっても、私の答えと私のコードがあります.
ユーザーの座標を取得し、緯度を比較して、ユーザーが北半球 (緯度 0° から 90°) にいるか南半球 (緯度 0° から -90°) にいるかを判断します。その後、季節の日付を固定し、日付を現在の日付と比較します。
楽しんで :
- (void)currentSeason
{
NSCalendar* myCalendar = [NSCalendar currentCalendar];
NSDateComponents* components = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
fromDate:[NSDate date]];
components.hour = 12;
// Current year
NSInteger currentYear = components.year;
// Today
NSDate *today = [NSDate date];
// First Day of the year
components.month = 1;
components.day = 1;
components.year = currentYear;
NSDate *firstDayDate = [myCalendar dateFromComponents:components];
// Spring Date
components.month = 3;
components.day = 21;
components.year = currentYear;
NSDate *springDate = [myCalendar dateFromComponents:components];
// Summer Date
components.month = 6;
components.day = 21;
components.year = currentYear;
NSDate *summerDate = [myCalendar dateFromComponents:components];
// Autumn Date
components.month = 9;
components.day = 23;
components.year = currentYear;
NSDate *autumnDate = [myCalendar dateFromComponents:components];
// Winter Date
components.month = 12;
components.day = 22;
components.year = currentYear;
NSDate *winterDate = [myCalendar dateFromComponents:components];
// Last Day of the year
components.month = 12;
components.day = 31;
components.year = currentYear;
NSDate *lastDayDate = [myCalendar dateFromComponents:components];
// Current Location
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coord = [location coordinate];
// North Hemisphere
if (coord.latitude > 0)
{
NSLog(@"North Pole");
// Settings the background image
if ([self isDate:today inRangeFirstDate:firstDayDate lastDate:springDate] || [self isDate:today inRangeFirstDate:winterDate lastDate:lastDayDate])
{
NSLog(@"Winter");
}
else if ([self isDate:today inRangeFirstDate:springDate lastDate:summerDate])
{
NSLog(@"Spring");
}
else if ([self isDate:today inRangeFirstDate:summerDate lastDate:autumnDate])
{
NSLog(@"Summer");
}
else if ([self isDate:today inRangeFirstDate:autumnDate lastDate:winterDate])
{
NSLog(@"Autumn");
}
}
else
{
NSLog(@"South Pole");
// Settings the background image
if ([self isDate:today inRangeFirstDate:firstDayDate lastDate:springDate] || [self isDate:today inRangeFirstDate:winterDate lastDate:lastDayDate])
{
NSLog(@"Summer");
}
else if ([self isDate:today inRangeFirstDate:springDate lastDate:summerDate])
{
NSLog(@"Autumn");
}
else if ([self isDate:today inRangeFirstDate:summerDate lastDate:autumnDate])
{
NSLog(@"Winter");
}
else if ([self isDate:today inRangeFirstDate:autumnDate lastDate:winterDate])
{
NSLog(@"Spring");
}
}
}
- (BOOL)isDate:(NSDate *)date inRangeFirstDate:(NSDate *)firstDate lastDate:(NSDate *)lastDate
{
return ([date compare:firstDate] == NSOrderedDescending) && ([date compare:lastDate] == NSOrderedAscending);
}
まず、ユーザーの現在地の座標を取得します。
CLLocationManager *lm = [[CLLocationManager alloc] init];
lm.delegate = self;
lm.desiredAccuracy = kCLLocationAccuracyBest;
lm.distanceFilter = kCLDistanceFilterNone;
[lm startUpdatingLocation];
CLLocation *location = [lm location];
CLLocationCoordinate2D coord;
coord = [location coordinate];
次に、現在の日付を取得します。
NSDate* currentDate = [NSDate date];
必要なのはそれだけだと思います。ユーザーが赤道の上か下かを判断し、季節の変化の日付を定数として保存します。