Skip to content

Instantly share code, notes, and snippets.

@tewodroswondimu
Last active August 29, 2015 14:16
Show Gist options
  • Save tewodroswondimu/3f96a5ed8a9ff18ca0c6 to your computer and use it in GitHub Desktop.
Save tewodroswondimu/3f96a5ed8a9ff18ca0c6 to your computer and use it in GitHub Desktop.
A simple block for sending SMS messages through Nexmo
//
// Nexmo.m
//
// Created by Tewodros Wondimu on 3/7/15.
// Contact: tewodroswondimu@gmail.com
// Copyright (c) 2015 Tewodros Wondimu. All rights reserved.
//
#import "Nexmo.h"
// Please replace the value below with your own account details
#define apiKey @"Your-Nexmo-API-Key"
#define apiSecret @"Your-Nexmo-API-Secret"
#define yourNumber @"Your-Nexmo-Phone-Number"
@implementation Nexmo
// A completion block that helps you send your messages to a specific number
+ (void)sendSMSToUserWithPhoneNumber:(NSString *)phoneNumber andMessage:(NSString *)message ithCompletionBlock:(void(^)(NSArray *messagesArray))complete
{
// Modify message to strip out spaces
NSString *modifiedMessage = [message stringByReplacingOccurrencesOfString:@" " withString:@"+"];
// URL to send the message to
NSString *urlString = [NSString stringWithFormat:@"https://rest.nexmo.com/sms/json?api_key=%@&api_secret=%@&from=@%&to=%@&text=%@", apiKey, apiSecret, yourNumber, phoneNumber, modifiedMessage];
NSURL *url = [NSURL URLWithString:urlString];
// Setup a URL Request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];;
// Send a request
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (!connectionError) {
NSError *error = nil;
NSDictionary *response = [NSJSONSerialization JSONObjectWithData:data options:NSASCIIStringEncoding error:&error];
if (!error) {
// A response from Nexmo with details about the message you just sent
NSArray *messagesArray = response[@"messages"];
complete(messagesArray);
}
else
{
NSLog(@"Error retrieving results from Nexmo");
}
}
}];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment