NSString *XAPIKEY = @"myapikey";
NSString *LOGINUSER = @"login username";
NSString *LOGINPASS = @"passwords";
// Setup NSURLConnection
- (void) postRequest:(NSString *) requestFun requestData:(NSDictionary *) sendDataDic{
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", @"http://api.domain.com/index.php/api/", requestFun]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:30.0];
[request setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:XAPIKEY forHTTPHeaderField:@"X-API-KEY"];
if(sendDataDic != nil){
NSString *stringData = [self urlEncodedString:sendDataDic];
[request setHTTPBody:[stringData dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[stringData length]] forHTTPHeaderField:@"Content-Length"];
}
[request setHTTPMethod:@"POST"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
}
- (void) getRequest:(NSString *) requestFun requestData:(NSDictionary *) sendDataDic{
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", @"http://api.domain.com/index.php/api/", requestFun]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
[request setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:XAPIKEY forHTTPHeaderField:@"X-API-KEY"];
if(sendDataDic != nil){
NSString *stringData = [self urlEncodedString:sendDataDic];
[request setHTTPBody:[stringData dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[stringData length]] forHTTPHeaderField:@"Content-Length"];
}
[request setHTTPMethod:@"GET"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
}
static NSString *toString(id object) {
return [NSString stringWithFormat: @"%@", object];
}
static NSString *urlEncode(id object) {
NSString *string = toString(object);
return [string stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
}
-(NSString*) urlEncodedString:(NSDictionary*) ObjDic {
NSMutableArray *parts = [NSMutableArray array];
for (id key in ObjDic) {
id value = [ObjDic objectForKey: key];
NSString *part = [NSString stringWithFormat: @"%@=%@", urlEncode(key), urlEncode(value)];
[parts addObject: part];
}
return [parts componentsJoinedByString: @"&"];
}
// NSURLConnection Delegates
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge previousFailureCount] == 0) {
NSLog(@"received authentication challenge");
NSURLCredential *newCredential = [NSURLCredential credentialWithUser:LOGINUSER
password:LOGINPASS
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 {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSLog(@"response status code: %ld", (long)[httpResponse statusCode]);
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSString *strData = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", strData);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"responded to authentication challenge");
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"responded to authentication challenge");
}