Skip to content

Instantly share code, notes, and snippets.

@YuliiaVeres
Created April 4, 2016 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YuliiaVeres/0f33c44756061a83f80203b2498de5ae to your computer and use it in GitHub Desktop.
Save YuliiaVeres/0f33c44756061a83f80203b2498de5ae to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
@interface InviteManager : NSObject
- (void)inviteClients;
@end
#import "InviteManager.h"
#import <UIKit/UIKit.h>
#import <math.h>
const CGFloat officeLatitude = 53.3381985 * M_PI / 180.0;
const CGFloat officeLongitude = -6.2592576;
const CGFloat EARTH_RADIUS_KM = 6371.0;
const CGFloat ACCEPTED_RADIUS_KM = 100.0;
@implementation InviteManager
- (void)inviteClients
{
NSString *requestUrl = @"https://gist.githubusercontent.com/brianw/19896c50afa89ad4dec3/raw/6c11047887a03483c50017c1d451667fd62a53ca/gistfile1.txt";
NSURL *url = [NSURL URLWithString:requestUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
__weak typeof (self)weakSelf = self;
[[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSString *clientsString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
clientsString = [clientsString stringByReplacingOccurrencesOfString:@"\n" withString:@","];
clientsString = [NSString stringWithFormat:@"[%@]", clientsString];
NSData *clientsData = [clientsString dataUsingEncoding:NSUTF8StringEncoding];
NSError *e = nil;
NSArray *clients = [NSJSONSerialization JSONObjectWithData:clientsData options:0 error:&e];
typeof(weakSelf)strongSelf = weakSelf;
[strongSelf filterClientsWithinAcceptedDistance:clients];
}] resume];
}
- (void)filterClientsWithinAcceptedDistance:(NSArray *)clients
{
NSMutableArray *invitedClients = [NSMutableArray new];
for (NSDictionary *client in clients)
{
if ([self isWithinAcceptedRadius:client])
[invitedClients addObject:client];
}
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"user_id" ascending:YES];
[invitedClients sortUsingDescriptors:@[descriptor]];
for (NSDictionary *client in invitedClients)
NSLog(@"\nName: %@, user id : %@ \n", client[@"name"], client[@"user_id"]);
}
- (BOOL)isWithinAcceptedRadius:(NSDictionary *)client
{
CGFloat latitude = [client[@"latitude"] floatValue];
CGFloat latitudeRad = latitude * M_PI / 180.0;
CGFloat longitude = [client[@"longitude"] floatValue];
CGFloat longitudeDiff = fabs(officeLongitude - longitude) * M_PI / 180.0;
CGFloat centralAngle = acos(sinf(latitudeRad) * sinf(officeLatitude) + cosf(latitudeRad) * cosf(officeLatitude) * cosf(longitudeDiff));
CGFloat distance = centralAngle * EARTH_RADIUS_KM;
NSLog(@"Distance: %f", distance);
return distance <= ACCEPTED_RADIUS_KM;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment