Created
October 30, 2012 20:08
-
-
Save devarispbrown/3982666 to your computer and use it in GitHub Desktop.
Zendesk API Consumption
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-( void )postToZenDesk:( NSString *)name withEmail:( NSString *)email andError:( NSError **)error{ | |
NSMutableDictionary *ticketDic = [[ NSMutableDictionary | |
alloc ] init ]; | |
NSMutableDictionary *dic = [[ NSMutableDictionary | |
alloc ] init ]; | |
NSMutableDictionary *commentDic = [[ NSMutableDictionary | |
alloc ] init ]; | |
[commentDic setObject :email forKey : @"value" ]; | |
[dic setObject :name | |
forKey : @"subject" ]; | |
[dic setObject :commentDic | |
forKey : @"comment" ]; | |
[ticketDic setObject :dic | |
forKey : @"ticket" ]; | |
NSData *postData = [ticketDic | |
JSONData ]; | |
// Setup NSURLConnection | |
NSURL *URL = [ NSURL | |
URLWithString : @"https://{subdomain}.zendesk.com/api/v2/tickets.json" ]; | |
NSMutableURLRequest *request = [ NSMutableURLRequest | |
requestWithURL :URL | |
cachePolicy : NSURLRequestUseProtocolCachePolicy | |
timeoutInterval : 30.0 ]; | |
[request setHTTPMethod : @"POST" ]; | |
[request setHTTPBody :postData]; | |
[request addValue : @"application/json" | |
forHTTPHeaderField : @"Content-Type" ]; | |
NSURLConnection *connection = [[ NSURLConnection | |
alloc ] initWithRequest :request | |
delegate : self ]; | |
[connection start ]; | |
} | |
// NSURLConnection Delegates | |
- ( void )connection:( NSURLConnection *)connection didReceiveAuthenticationChallenge:( NSURLAuthenticationChallenge *)challenge { | |
if ([challenge | |
previousFailureCount ] == 0 ) { | |
// NSLog(@"received authentication challenge"); | |
NSURLCredential *newCredential = [ NSURLCredential | |
credentialWithUser : @“USERNAME" | |
password : @“PASSWORD" | |
persistence : NSURLCredentialPersistenceForSession ]; | |
// NSLog(@"credential created"); | |
[[challenge sender ] | |
useCredential :newCredential forAuthenticationChallenge :challenge]; | |
// NSLog(@"responded to authentication challenge"); | |
} | |
else { | |
// NSLog(@"previous authentication failure"); | |
} | |
} | |
- ( void )connection:( NSURLConnection *)connection didReceiveResponse:( NSURLResponse *)response { | |
NSLog ( @"Recived Response %@" , response); | |
} | |
- ( void )connection:( NSURLConnection *)connection didReceiveData:( NSData *)data { | |
NSLog ( @"String sent from server %@" ,[[ NSString | |
alloc ] initWithData :data | |
encoding : NSUTF8StringEncoding ]); | |
} | |
- ( void )connectionDidFinishLoading:( NSURLConnection *)connection { | |
} | |
- ( void )connection:( NSURLConnection *)connection didFailWithError:( NSError *)error { | |
if ([error code ] == - 1009 ) { | |
UIAlertView *alert = [[ UIAlertView | |
alloc ] initWithTitle : @"Not Connected to the Internet" | |
message : @"Please connect to the internet and try again." | |
delegate : self | |
cancelButtonTitle : @"OK" | |
otherButtonTitles : | |
nil ]; | |
[alert show ]; | |
NSLog ( @"Failed %@" , error); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment