Welcome to the UnirateAPI Objective-C documentation! Our free exchange rate API provides real-time currency conversion capabilities that you can easily integrate into your iOS and macOS applications. Whether you're building a financial app, travel utility, or any application that needs currency conversion, our API has you covered.
UnirateAPI is designed to be developer-friendly and reliable, with a focus on simplicity and performance. This guide will show you how to integrate currency conversion functionality into your Objective-C applications.
For more detailed information about our API endpoints, rate limits, and advanced features, please check our main API documentation.
Below you'll find examples showing how to use our API with Objective-C.
You can use our API in Objective-C by making HTTP requests using NSURLSession. The following examples demonstrate how to implement currency conversion in your iOS or macOS applications.
// UnirateAPIClient.h
#import
@interface UnirateAPIClient : NSObject
@property (nonatomic, strong, readonly) NSString *apiKey;
- (instancetype)initWithAPIKey:(NSString *)apiKey;
// Convert currency
- (void)convertAmount:(double)amount
fromCurrency:(NSString *)fromCurrency
toCurrency:(NSString *)toCurrency
completion:(void (^)(NSDictionary *result, NSError *error))completion;
// Get exchange rate
- (void)getExchangeRateFromCurrency:(NSString *)fromCurrency
toCurrency:(NSString *)toCurrency
completion:(void (^)(double rate, NSError *error))completion;
// Get supported currencies
- (void)getSupportedCurrenciesWithCompletion:(void (^)(NSDictionary *currencies, NSError *error))completion;
@end
// UnirateAPIClient.m
#import "UnirateAPIClient.h"
@interface UnirateAPIClient ()
@property (nonatomic, strong, readwrite) NSString *apiKey;
@property (nonatomic, strong) NSURLSession *session;
@property (nonatomic, strong) NSString *baseURL;
@end
@implementation UnirateAPIClient
- (instancetype)initWithAPIKey:(NSString *)apiKey {
self = [super init];
if (self) {
_apiKey = apiKey;
_baseURL = @"https://api.unirateapi.com/api";
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
_session = [NSURLSession sessionWithConfiguration:config];
}
return self;
}
- (void)convertAmount:(double)amount
fromCurrency:(NSString *)fromCurrency
toCurrency:(NSString *)toCurrency
completion:(void (^)(NSDictionary *result, NSError *error))completion {
NSString *endpoint = @"/convert";
NSDictionary *params = @{
@"api_key": self.apiKey,
@"from": fromCurrency,
@"to": toCurrency,
@"amount": [NSString stringWithFormat:@"%f", amount]
};
[self performRequestWithEndpoint:endpoint parameters:params completion:^(id responseObject, NSError *error) {
if (error) {
completion(nil, error);
return;
}
completion(responseObject, nil);
}];
}
- (void)getExchangeRateFromCurrency:(NSString *)fromCurrency
toCurrency:(NSString *)toCurrency
completion:(void (^)(double rate, NSError *error))completion {
NSString *endpoint = @"/rates";
NSDictionary *params = @{
@"api_key": self.apiKey,
@"base": fromCurrency,
@"symbols": toCurrency
};
[self performRequestWithEndpoint:endpoint parameters:params completion:^(id responseObject, NSError *error) {
if (error) {
completion(0, error);
return;
}
NSDictionary *rates = responseObject[@"rates"];
double rate = [rates[toCurrency] doubleValue];
completion(rate, nil);
}];
}
- (void)getSupportedCurrenciesWithCompletion:(void (^)(NSDictionary *currencies, NSError *error))completion {
NSString *endpoint = @"/currencies";
NSDictionary *params = @{
@"api_key": self.apiKey
};
[self performRequestWithEndpoint:endpoint parameters:params completion:^(id responseObject, NSError *error) {
if (error) {
completion(nil, error);
return;
}
completion(responseObject, nil);
}];
}
- (void)performRequestWithEndpoint:(NSString *)endpoint
parameters:(NSDictionary *)parameters
completion:(void (^)(id responseObject, NSError *error))completion {
NSURLComponents *components = [NSURLComponents componentsWithString:[self.baseURL stringByAppendingString:endpoint]];
NSMutableArray *queryItems = [NSMutableArray array];
[parameters enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *value, BOOL *stop) {
[queryItems addObject:[NSURLQueryItem queryItemWithName:key value:value]];
}];
components.queryItems = queryItems;
NSURLRequest *request = [NSURLRequest requestWithURL:components.URL];
NSURLSessionDataTask *task = [self.session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
dispatch_async(dispatch_get_main_queue(), ^{
completion(nil, error);
});
return;
}
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.statusCode != 200) {
NSError *statusError = [NSError errorWithDomain:@"UnirateAPIErrorDomain"
code:httpResponse.statusCode
userInfo:@{NSLocalizedDescriptionKey: @"API request failed"}];
dispatch_async(dispatch_get_main_queue(), ^{
completion(nil, statusError);
});
return;
}
NSError *jsonError;
id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
if (jsonError) {
dispatch_async(dispatch_get_main_queue(), ^{
completion(nil, jsonError);
});
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
completion(jsonObject, nil);
});
}];
[task resume];
}
@end
// Example usage in a view controller
#import "ViewController.h"
#import "UnirateAPIClient.h"
@interface ViewController ()
@property (nonatomic, strong) UnirateAPIClient *apiClient;
@property (nonatomic, strong) IBOutlet UITextField *amountTextField;
@property (nonatomic, strong) IBOutlet UILabel *resultLabel;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Initialize the API client with your API key
self.apiClient = [[UnirateAPIClient alloc] initWithAPIKey:@"YOUR_API_KEY"];
}
- (IBAction)convertButtonTapped:(id)sender {
double amount = [self.amountTextField.text doubleValue];
[self.apiClient convertAmount:amount
fromCurrency:@"USD"
toCurrency:@"EUR"
completion:^(NSDictionary *result, NSError *error) {
if (error) {
NSLog(@"Error: %@", error.localizedDescription);
self.resultLabel.text = @"Conversion failed";
return;
}
NSString *formattedResult = [NSString stringWithFormat:@"%.2f %@ = %.2f %@",
amount,
result[@"from"],
[result[@"result"] doubleValue],
result[@"to"]];
self.resultLabel.text = formattedResult;
}];
}
@end
// API Response:
{
"from": "USD",
"to": "EUR",
"amount": 100,
"result": 92.45
}
// Console Output:
100.00 USD = 92.45 EUR
Sign up now to get your API key and start converting currencies in your objc applications.
Get Your API Key