Skip to content

Instantly share code, notes, and snippets.

@VerizonMediaOwner
Last active October 16, 2019 07:29
Show Gist options
  • Save VerizonMediaOwner/8b3c4d81004be5975d9e5070f27b2ac5 to your computer and use it in GitHub Desktop.
Save VerizonMediaOwner/8b3c4d81004be5975d9e5070f27b2ac5 to your computer and use it in GitHub Desktop.
Yahoo Weather API Objective-C Example
//
// ViewController.m
// weather-api-demo-objc
//
// Copyright 2019 Oath Inc. Licensed under the terms of the zLib license see https://opensource.org/licenses/Zlib for terms.
//
#import "ViewController.h"
#import "YahooWeatherAPI.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Weather by location name
[YahooWeatherAPI.shared weatherForLocation:@"sunnyvale,ca" format:nil unit:nil failure:^(NSError * _Nullable error) {
NSLog(@"error: %@", error.localizedDescription);
} success:^(NSData * _Nullable data) {
NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"data: %@", response);
}];
// Weather by woeid
[YahooWeatherAPI.shared weatherForWoeId:@"2502265" format:nil unit:nil failure:^(NSError * _Nullable error) {
NSLog(@"error: %@", error.localizedDescription);
} success:^(NSData * _Nullable data) {
NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"data: %@", response);
}];
// Weather by lat and lon
[YahooWeatherAPI.shared weatherForLat:@"37.372" lon:@"-122.038" format:nil unit:nil failure:^(NSError * _Nullable error) {
NSLog(@"error: %@", error.localizedDescription);
} success:^(NSData * _Nullable data) {
NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"data: %@", response);
}];
// Weather by location in XML format and metric units.
[YahooWeatherAPI.shared weatherForLocation:@"sunnyvale,ca" format:@"xml" unit:@"c" failure:^(NSError * _Nullable error) {
NSLog(@"error: %@", error.localizedDescription);
} success:^(NSData * _Nullable data) {
NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"data: %@", response);
}];
}
@end
//
// YahooWeatherAPI.h
// weather-api-demo-objc
//
// Copyright 2019 Oath Inc. Licensed under the terms of the zLib license see https://opensource.org/licenses/Zlib for terms.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface YahooWeatherAPI : NSObject
-(nonnull instancetype) init NS_UNAVAILABLE;
/**
Shared instance of the YahooWeatherAPI class. Make your calls on this object: [YahooWeatherAPI.shared weatherForLocation ...]
@return Shared instance of YahooWeatherAPI
*/
+ (YahooWeatherAPI*)shared;
/**
Returns weather data by location in a format and unit you specify.
@param location The location you want weather for, i.e. 'sunnyvale,ca'
@param format Response format. json or xml, default is json
@param unit Units returned. imperial or metric, default is imperial
@param failure Failure callback
@param success Success callback
*/
-(void)weatherForLocation:(NSString *) location format:(NSString* _Nullable)format unit:(NSString* _Nullable) unit failure:(void(^)(NSError * _Nullable error)) failure success:(void(^)(NSData* _Nullable data)) success;
/**
Returns weather data by woeid (Where on Earth ID) in a format and unit you specify
@param woeid The woeid for the location
@param format Response format. json or xml, default is json
@param unit Units returned. imperial or metric, default is imperial
@param failure Failure callback
@param success Success callback
*/
-(void)weatherForWoeId:(NSString* _Nonnull) woeid format:(NSString* _Nullable)format unit:(NSString* _Nullable) unit failure:(void(^)(NSError * _Nullable error)) failure success:(void(^)(NSData* _Nullable data)) success;
/**
Returns weather data for a given latitude and longitude
@param lat Latitude of the location
@param lon Longitude of the location
@param format Response format. json or xml, default is json
@param unit Units returned. imperial or metric, default is imperial
@param failure Failure callback
@param success Success callback
*/
-(void)weatherForLat:(NSString* _Nonnull) lat lon:(NSString* _Nonnull) lon format:(NSString* _Nullable)format unit:(NSString* _Nullable) unit failure:(void(^)(NSError * _Nullable error)) failure success:(void(^)(NSData* _Nullable data)) success;
@end
NS_ASSUME_NONNULL_END
//
// YahooWeatherAPI.m
// weather-api-demo-objc
//
// Copyright 2019 Oath Inc. Licensed under the terms of the zLib license see https://opensource.org/licenses/Zlib for terms.
//
#import "YahooWeatherAPI.h"
/*
See https://github.com/yahoo/TDOAuth for information on adding this library to your project.
*/
#import <TDOAuth/TDOAuth.h>
@interface YahooWeatherAPI()
@property (nonatomic) NSString *appId;
@property (nonatomic) NSString *consumerKey;
@property (nonatomic) NSString *consumerSecret;
@end
@implementation YahooWeatherAPI
+ (YahooWeatherAPI*)shared
{
static YahooWeatherAPI *_shared;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_shared = [YahooWeatherAPI new];
});
return _shared;
}
-(instancetype)init
{
self = [super init];
if(self) {
// Fill these with your own values.
self.appId = @"-your-app-id-";
self.consumerKey = @"-your-client-id-";
self.consumerSecret = @"-your-client-secret-";
}
return self;
}
-(void)weatherForLocation:(NSString* _Nonnull) location format:(NSString* _Nullable)format unit:(NSString* _Nullable) unit failure:(void(^)(NSError * _Nullable error)) failure success:(void(^)(NSData* _Nullable data)) success
{
NSMutableURLRequest *request = [self requestWithParameters:@{@"location":location, @"format":format?:@"json", @"u":unit?:@"f"}];
[self makeRequest:request failure:failure success:success];
}
-(void)weatherForWoeId:(NSString* _Nonnull) woeid format:(NSString* _Nullable)format unit:(NSString* _Nullable) unit failure:(void(^)(NSError * _Nullable error)) failure success:(void(^)(NSData* _Nullable data)) success
{
NSMutableURLRequest *request = [self requestWithParameters:@{@"woeid":woeid, @"format":format?:@"json", @"u":unit?:@"f"}];
[self makeRequest:request failure:failure success:success];
}
-(void)weatherForLat:(NSString* _Nonnull) lat lon:(NSString* _Nonnull) lon format:(NSString* _Nullable)format unit:(NSString* _Nullable) unit failure:(void(^)(NSError * _Nullable error)) failure success:(void(^)(NSData* _Nullable data)) success
{
NSMutableURLRequest *request = [self requestWithParameters:@{@"lat":lat, @"lon":lon, @"format":format?:@"json", @"u":unit?:@"f"}];
[self makeRequest:request failure:failure success:success];
}
-(NSMutableURLRequest *)requestWithParameters:(NSDictionary *)parameters
{
NSMutableURLRequest *request = [[TDOAuth URLRequestForPath:@"/forecastrss"
GETParameters:parameters
scheme:@"https"
host:@"weather-ydn-yql.media.yahoo.com"
consumerKey:self.consumerKey
consumerSecret:self.consumerSecret
accessToken:nil
tokenSecret:nil] mutableCopy];
[request addValue:self.appId forHTTPHeaderField:self.appId];
return request;
}
-(void)makeRequest:(NSMutableURLRequest*) request failure:(void(^)(NSError * _Nullable error)) failure success:(void(^)(NSData* data)) success
{
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if(error != nil) {
dispatch_async(dispatch_get_main_queue(), ^(void){
if(failure != nil) {
failure(error);
}
});
} else {
dispatch_async(dispatch_get_main_queue(), ^(void){
if(success != nil) {
success(data);
}
});
}
}] resume];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment