Skip to content

Instantly share code, notes, and snippets.

@ChrisRisner
Created September 14, 2012 18:36
Show Gist options
  • Save ChrisRisner/3723823 to your computer and use it in GitHub Desktop.
Save ChrisRisner/3723823 to your computer and use it in GitHub Desktop.
GeoDemo-ios3
@interface Constants : NSObject
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
extern NSString *kGetPOIUrl;
extern NSString *kGetSASUrl;
extern NSString *kAddPOIUrl;
extern NSString *kContainerName;
@end
@implementation Constants
NSString *kGetPOIUrl = @"http://yoursubdomain.azurewebsites.net/api/Location/FindPointsOfInterestWithinRadius";
NSString *kGetSASUrl = @"http://yoursubdomain.azurewebsites.net/api/blobsas/get";
NSString *kAddPOIUrl = @"http://yoursubdomain.azurewebsites.net/api/location/postpointofinterest/";
NSString *kContainerName = @"test";
@end
- (IBAction)tapGetSASURL:(id)sender {
NSString *time = [NSString stringWithFormat:@"%i", -CFAbsoluteTimeGetCurrent()];
//Just pass the call over to our generic serviceCaller
[serviceCaller postToUrl:[NSString stringWithFormat:@"%@?container=%@&blobname=%@"
,kGetSASUrl, kContainerName, time]
withBody:nil andPostType:@"GET" andContentType:nil withCallback:^(NSString *response) {
//This is the callback code that the ServiceCaller will call upon success
labelSasUrl.text = response;
sasURL = [response stringByReplacingOccurrencesOfString:@"\"" withString:@""];
NSLog(@"SAS URL: %@", sasURL);
buttonPostPoi.enabled = YES;
buttonGetSasUrl.enabled = NO;
}];
}
#import <UIKit/UIKit.h>
#import "ServiceCaller.h"
@interface NewPOIViewController : UIViewController <UIImagePickerControllerDelegate> {
@private
ServiceCaller *serviceCaller;
NSString *sasURL;
}
- (IBAction)tapSelectImage:(id)sender;
- (IBAction)tapPostPOI:(id)sender;
- (IBAction)tapGetSASURL:(id)sender;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *labelSasUrl;
@property (weak, nonatomic) IBOutlet UIButton *buttonGetSasUrl;
@property (weak, nonatomic) IBOutlet UIButton *buttonPostPoi;
@end
- (IBAction)tapSelectImage:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
imageView.image = image;
[picker dismissModalViewControllerAnimated:YES];
buttonGetSasUrl.enabled = YES;
buttonPostPoi.enabled = NO;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
buttonGetSasURL.enabled = NO;
buttonPostPOI.enabled = NO;
serviceCaller = [[ServiceCaller alloc] init];
}
@interface ServiceCaller : NSObject <NSURLConnectionDelegate> {
@private
NSMutableDictionary* callbacks;
}
-(ServiceCaller*) init;
-(void) postToUrl: (NSString*) url withBody: (NSData*) body andPostType: (NSString *) postType andContentType: (NSString *) contentType withCallback: (void(^) (NSString* response)) callback;
@end
#import "ServiceCaller.h"
#import "StateObject.h"
@implementation ServiceCaller
-(ServiceCaller*) init {
callbacks = [[NSMutableDictionary alloc] init];
return self;
}
/**
This method performs a generic post to a web endpoint. It can handle different content types, post types,
as well as data or no data. It expects a callback method (block) which will be called upon completion.
*/
-(void) postToUrl:(NSString *)url withBody:(NSData *)body andPostType: (NSString *) postType andContentType: (NSString *) contentType
withCallback: (void (^)(NSString *))callback {
NSLog(@"Posting Url: %@", url);
NSMutableURLRequest* request = [NSMutableURLRequest
requestWithURL: [NSURL URLWithString:url]];
[request setHTTPMethod:postType];
if (contentType)
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
NSError *error;
// should check for and handle errors here but we aren't
[request setHTTPBody:body];
//Start the request
NSURLConnection *conn = [[NSURLConnection alloc]
initWithRequest: request delegate:self];
//Store a StateObject which has an instance of receivedData to store any data that comes back for the
//request in addition to the callback that needs to be called upon completion.
StateObject* connectionState = [[StateObject alloc] init];
connectionState.receivedData = [[NSMutableData alloc] init];
[connectionState.receivedData setLength:0];
connectionState.callbackBlock = callback;
//Here the StateObject is being stored with the connections Hash as it's key
[callbacks setValue:connectionState forKey:[NSString stringWithFormat:@"%i", conn.hash]];
}
#pragma NSUrlConnectionDelegate Methods
-(void)connection:(NSConnection*)conn didReceiveResponse:
(NSURLResponse *)response
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if ([httpResponse statusCode] >= 400) {
NSLog(@"Status Code: %i", [httpResponse statusCode]);
NSLog(@"Remote url returned error %d %@",[httpResponse statusCode],[NSHTTPURLResponse localizedStringForStatusCode:[httpResponse statusCode]]);
}
else {
NSLog(@"Safe Response Code: %i", [httpResponse statusCode]);
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:
(NSData *)data
{
StateObject* connectionState = [callbacks objectForKey:[NSString stringWithFormat:@"%i", connection.hash]];
[connectionState.receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:
(NSError *)error
{
//We should do something more with the error handling here
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:
NSURLErrorFailingURLStringErrorKey]);
//Remove the state object on an error, or add an error callback to it and use that
NSString* connectionHash = [NSString stringWithFormat:@"%i", connection.hash];
[callbacks removeObjectForKey:connectionHash];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString* connectionHash = [NSString stringWithFormat:@"%i", connection.hash];
//Pull out the stateobject by the connection's hash
StateObject* connectionState = [callbacks objectForKey:connectionHash];
NSString *txt = [[NSString alloc] initWithData:connectionState.receivedData
encoding: NSASCIIStringEncoding];
NSLog(@"Response data: %@", txt);
//Call the callback that was originally sent in for this request
connectionState.callbackBlock(txt);
[callbacks removeObjectForKey:connectionHash];
}
@end
@interface StateObject : NSObject
@property (nonatomic, copy) void (^ callbackBlock)(NSString *response);
@property (nonatomic, retain) NSMutableData* receivedData;
@end
@implementation StateObject
@synthesize receivedData;
@synthesize callbackBlock;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment