diff --git a/OTRestClient.h b/OTRestClient.h index 7fcbd2bf..573ed209 100644 --- a/OTRestClient.h +++ b/OTRestClient.h @@ -59,11 +59,24 @@ */ + (OTRestClient*)clientWithBaseURL:(NSString*)baseURL username:(NSString*)username password:(NSString*)password; +/** + * Will check for network connectivity (to google.com) + */ +- (BOOL)isNetworkAvailable; + /** * Fetch a resource via an HTTP GET and invoke a callback with the resulting payload */ - (OTRestRequest*)get:(NSString*)resourcePath delegate:(id)delegate callback:(SEL)callback; +/** + * Fetch a resource via an HTTP GET with a dictionary of params and invoke a callback with the resulting payload + * + * Note that this request _only_ allows NSDictionary objects as the params. The dictionary will be coerced into a URL encoded + * string and then appended to the resourcePath as the query string of the request. + */ +- (OTRestRequest*)get:(NSString*)resourcePath params:(NSDictionary*)params delegate:(id)delegate callback:(SEL)callback; + /** * Create a resource via an HTTP POST with a set of form parameters and invoke a callback with the resulting payload */ diff --git a/OTRestClient.m b/OTRestClient.m index 1b6d16dc..f256a1fa 100644 --- a/OTRestClient.m +++ b/OTRestClient.m @@ -8,6 +8,7 @@ #import "OTRestClient.h" #import "OTRestModelLoader.h" +#import /////////////////////////////////////////////////////////////////////////////////////////////////// // global @@ -66,6 +67,25 @@ static OTRestClient* sharedClient = nil; [super dealloc]; } +- (BOOL)isNetworkAvailable { + Boolean success; + + const char *host_name = "google.com"; // your data source host name + + SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host_name); +#ifdef TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR + SCNetworkReachabilityFlags flags; +#else + SCNetworkConnectionFlags flags; +#endif + success = SCNetworkReachabilityGetFlags(reachability, &flags); + BOOL isNetworkAvailable = success && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired); + CFRelease(reachability); + + return isNetworkAvailable; + +} + - (NSURL*)URLForResourcePath:(NSString*)resourcePath { NSString* urlString = [NSString stringWithFormat:@"%@%@", self.baseURL, resourcePath]; return [NSURL URLWithString:urlString]; @@ -78,6 +98,14 @@ static OTRestClient* sharedClient = nil; return request; } +- (OTRestRequest*)get:(NSString*)resourcePath params:(NSDictionary*)params delegate:(id)delegate callback:(SEL)callback { + NSString* resourcePathWithQueryString = [NSString stringWithFormat:@"%@?%@", resourcePath, [params URLEncodedString]]; + OTRestRequest* request = [[OTRestRequest alloc] initWithURL:[self URLForResourcePath:resourcePathWithQueryString] delegate:delegate callback:callback]; + request.additionalHTTPHeaders = _HTTPHeaders; + [request get]; + return request; +} + - (OTRestRequest*)post:(NSString*)resourcePath params:(NSObject*)params delegate:(id)delegate callback:(SEL)callback { OTRestRequest* request = [[OTRestRequest alloc] initWithURL:[self URLForResourcePath:resourcePath] delegate:delegate callback:callback]; request.additionalHTTPHeaders = _HTTPHeaders; diff --git a/OTRestManagedModel.m b/OTRestManagedModel.m index 67a72833..178bb991 100644 --- a/OTRestManagedModel.m +++ b/OTRestManagedModel.m @@ -7,7 +7,7 @@ // #import "OTRestManagedModel.h" - +#import @implementation OTRestManagedModel @@ -15,7 +15,8 @@ #pragma mark NSManagedObject helper methods + (NSEntityDescription*)entity { - return [NSEntityDescription entityForName:[[self class] className] inManagedObjectContext:myContext]; + NSString* className = [NSString stringWithCString:class_getName([self class])]; + return [NSEntityDescription entityForName:className inManagedObjectContext:myContext]; } + (NSFetchRequest*)request {