Skip to content

Commit

Permalink
Network Reachability check before uploading.
Browse files Browse the repository at this point in the history
  • Loading branch information
smurthas committed Nov 8, 2014
1 parent 8d204a1 commit 1d867cc
Show file tree
Hide file tree
Showing 6 changed files with 476 additions and 5 deletions.
4 changes: 2 additions & 2 deletions KeenClient.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ Pod::Spec.new do |spec|
The Keen client is designed to be simple to develop with, yet incredibly flexible. Our goal is to let you decide what events are important to you, use your own vocabulary to describe them, and decide when you want to send them to Keen service.
DESC
spec.source = { :git => 'https://github.com/keenlabs/KeenClient-iOS.git', :tag => '3.2.20' }
spec.source_files = 'KeenClient/*.{h,m}','Library/sqlite-amalgamation/*.{h,c}'
spec.source_files = 'KeenClient/*.{h,m}','Library/sqlite-amalgamation/*.{h,c}','Library/Reachability/*.{h,m}'
spec.public_header_files = 'KeenClient/*.h'
spec.private_header_files = 'Library/sqlite-amalgamation/*.h'
spec.frameworks = 'CoreLocation'
spec.requires_arc = true
end
end
32 changes: 32 additions & 0 deletions KeenClient.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@
DEFA50191947A67E00F78E13 /* KIOEventStore.m in Sources */ = {isa = PBXBuildFile; fileRef = CA6410D718E37E7C00E53E3C /* KIOEventStore.m */; };
DEFA501A1947A70800F78E13 /* KIOEventStore.h in Headers */ = {isa = PBXBuildFile; fileRef = CA6410D618E37E7C00E53E3C /* KIOEventStore.h */; settings = {ATTRIBUTES = (Public, ); }; };
DEFA501B1947A72600F78E13 /* KIOEventStore.h in Headers */ = {isa = PBXBuildFile; fileRef = CA6410D618E37E7C00E53E3C /* KIOEventStore.h */; settings = {ATTRIBUTES = (Public, ); }; };
F4D3B6021A0D8EB4000825FE /* Reachability.h in Headers */ = {isa = PBXBuildFile; fileRef = F4D3B6001A0D8EB4000825FE /* Reachability.h */; };
F4D3B6031A0D8EB4000825FE /* Reachability.h in Headers */ = {isa = PBXBuildFile; fileRef = F4D3B6001A0D8EB4000825FE /* Reachability.h */; };
F4D3B6041A0D8EB4000825FE /* Reachability.h in Headers */ = {isa = PBXBuildFile; fileRef = F4D3B6001A0D8EB4000825FE /* Reachability.h */; };
F4D3B6051A0D8EB4000825FE /* Reachability.h in Headers */ = {isa = PBXBuildFile; fileRef = F4D3B6001A0D8EB4000825FE /* Reachability.h */; };
F4D3B6061A0D8EB4000825FE /* Reachability.m in Sources */ = {isa = PBXBuildFile; fileRef = F4D3B6011A0D8EB4000825FE /* Reachability.m */; };
F4D3B6081A0D8EB4000825FE /* Reachability.m in Sources */ = {isa = PBXBuildFile; fileRef = F4D3B6011A0D8EB4000825FE /* Reachability.m */; };
F4D3B6091A0D8EB4000825FE /* Reachability.m in Sources */ = {isa = PBXBuildFile; fileRef = F4D3B6011A0D8EB4000825FE /* Reachability.m */; };
F4D3B60A1A0D8EB4000825FE /* Reachability.m in Sources */ = {isa = PBXBuildFile; fileRef = F4D3B6011A0D8EB4000825FE /* Reachability.m */; };
F4D3B61E1A0D98B4000825FE /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F4D3B61D1A0D98B4000825FE /* SystemConfiguration.framework */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -167,6 +176,9 @@
DE34F6F0197586EE00051390 /* keen_io_sqlite3.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = keen_io_sqlite3.c; sourceTree = "<group>"; };
DE34F6F1197586EE00051390 /* keen_io_sqlite3.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = keen_io_sqlite3.h; sourceTree = "<group>"; };
DE34F6F2197586EE00051390 /* keen_io_sqlite3ext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = keen_io_sqlite3ext.h; sourceTree = "<group>"; };
F4D3B6001A0D8EB4000825FE /* Reachability.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Reachability.h; sourceTree = "<group>"; };
F4D3B6011A0D8EB4000825FE /* Reachability.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Reachability.m; sourceTree = "<group>"; };
F4D3B61D1A0D98B4000825FE /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -199,6 +211,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
F4D3B61E1A0D98B4000825FE /* SystemConfiguration.framework in Frameworks */,
015D308B1822E8D200F8735B /* libOCMock.a in Frameworks */,
012E8A5E1672BFA00021F6FA /* CoreLocation.framework in Frameworks */,
017EE13014E30C96000F3868 /* SenTestingKit.framework in Frameworks */,
Expand Down Expand Up @@ -277,6 +290,7 @@
017EE12014E30C96000F3868 /* Frameworks */ = {
isa = PBXGroup;
children = (
F4D3B61D1A0D98B4000825FE /* SystemConfiguration.framework */,
1296398C19C10B8500B2B653 /* Cocoa.framework */,
CA795C4D18E32BAB00EBDFC4 /* libsqlite3.dylib */,
017EE12114E30C96000F3868 /* Foundation.framework */,
Expand Down Expand Up @@ -346,6 +360,7 @@
01BA1AD414E77DC600CF9F84 /* Library */ = {
isa = PBXGroup;
children = (
F4D3B5FF1A0D8EB4000825FE /* Reachability */,
DE6955781951FBCC0044D2C4 /* sqlite-amalgamation */,
015D30821822E8D200F8735B /* Headers */,
015D308A1822E8D200F8735B /* libOCMock.a */,
Expand Down Expand Up @@ -373,6 +388,15 @@
path = "sqlite-amalgamation";
sourceTree = "<group>";
};
F4D3B5FF1A0D8EB4000825FE /* Reachability */ = {
isa = PBXGroup;
children = (
F4D3B6001A0D8EB4000825FE /* Reachability.h */,
F4D3B6011A0D8EB4000825FE /* Reachability.m */,
);
path = Reachability;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXHeadersBuildPhase section */
Expand All @@ -386,6 +410,7 @@
DE34F6F7197586EE00051390 /* keen_io_sqlite3.h in Headers */,
DE34F6FA197586EE00051390 /* keen_io_sqlite3ext.h in Headers */,
0111012D14EF709A009794A5 /* KeenConstants.h in Headers */,
F4D3B6031A0D8EB4000825FE /* Reachability.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -399,6 +424,7 @@
DE34F6F8197586EE00051390 /* keen_io_sqlite3.h in Headers */,
DE34F6FB197586EE00051390 /* keen_io_sqlite3ext.h in Headers */,
0111014514EF70AB009794A5 /* KeenConstants.h in Headers */,
F4D3B6041A0D8EB4000825FE /* Reachability.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -412,6 +438,7 @@
DE34F6F6197586EE00051390 /* keen_io_sqlite3.h in Headers */,
DE34F6F9197586EE00051390 /* keen_io_sqlite3ext.h in Headers */,
01BA1B0514E895BC00CF9F84 /* KeenConstants.h in Headers */,
F4D3B6021A0D8EB4000825FE /* Reachability.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -425,6 +452,7 @@
1296398E19C10D0000B2B653 /* keen_io_sqlite3.h in Headers */,
1296398F19C10D0000B2B653 /* keen_io_sqlite3ext.h in Headers */,
1296399219C10D0000B2B653 /* KeenConstants.h in Headers */,
F4D3B6051A0D8EB4000825FE /* Reachability.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -620,6 +648,7 @@
0111011E14EF709A009794A5 /* KeenClient.m in Sources */,
010EB08C1676D1D6002B07B6 /* KeenProperties.m in Sources */,
0111011F14EF709A009794A5 /* KeenConstants.m in Sources */,
F4D3B6081A0D8EB4000825FE /* Reachability.m in Sources */,
DE34F6F4197586EE00051390 /* keen_io_sqlite3.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -632,6 +661,7 @@
0111013614EF70AB009794A5 /* KeenClient.m in Sources */,
010EB08D1676D1EE002B07B6 /* KeenProperties.m in Sources */,
0111013714EF70AB009794A5 /* KeenConstants.m in Sources */,
F4D3B6091A0D8EB4000825FE /* Reachability.m in Sources */,
DE34F6F5197586EE00051390 /* keen_io_sqlite3.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -644,6 +674,7 @@
01BA1B0614E895BC00CF9F84 /* KeenConstants.m in Sources */,
CA6410D918E37E7C00E53E3C /* KIOEventStore.m in Sources */,
012E8A571672B9A90021F6FA /* KeenProperties.m in Sources */,
F4D3B6061A0D8EB4000825FE /* Reachability.m in Sources */,
DE34F6F3197586EE00051390 /* keen_io_sqlite3.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -665,6 +696,7 @@
1296399419C10D2C00B2B653 /* KeenClient.m in Sources */,
1296399519C10D2C00B2B653 /* KeenProperties.m in Sources */,
1296399619C10D2C00B2B653 /* KeenConstants.m in Sources */,
F4D3B60A1A0D8EB4000825FE /* Reachability.m in Sources */,
1296399719C10D2C00B2B653 /* KIOEventStore.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
11 changes: 11 additions & 0 deletions KeenClient/KeenClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "KeenClient.h"
#import "KeenConstants.h"
#import "KIOEventStore.h"
#import "Reachability.h"
#import <CoreLocation/CoreLocation.h>


Expand Down Expand Up @@ -779,11 +780,21 @@ - (NSString *)pathForEventInCollection:(NSString *)collection WithTimestamp:(NSD

# pragma mark - Uploading

- (BOOL)isNetworkConnected {
Reachability *hostReachability = [Reachability reachabilityForInternetConnection];
return [hostReachability currentReachabilityStatus] != NotReachable;
}

- (void)uploadHelper
{
// only one thread should be doing an upload at a time.
@synchronized(self) {

if (![self isNetworkConnected]) {
// no network connection, so don't even attempt to upload
return;
}

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

// Check if we've done an import before. (A missing value returns NO)
Expand Down
25 changes: 22 additions & 3 deletions KeenClientTests/KeenClientTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ @interface KeenClient (testability)
@property (nonatomic) Boolean isRunningTests;

- (NSData *)sendEvents: (NSData *) data returningResponse: (NSURLResponse **) response error: (NSError **) error;
- (BOOL)isNetworkConnected;
- (id)convertDate: (id) date;
- (id)handleInvalidJSONInObject:(id)value;

Expand Down Expand Up @@ -399,6 +400,10 @@ - (NSDictionary *)buildResponseJsonWithSuccess:(Boolean)success
}

- (id)uploadTestHelperWithData:(id)data andStatusCode:(NSInteger)code {
return [self uploadTestHelperWithData:data andStatusCode:code andNetwork:@YES];
}

- (id)uploadTestHelperWithData:(id)data andStatusCode:(NSInteger)code andNetwork:(NSNumber *)network {
if (!data) {
data = [self buildResponseJsonWithSuccess:YES AndErrorCode:nil AndDescription:nil];
}
Expand All @@ -420,7 +425,9 @@ - (id)uploadTestHelperWithData:(id)data andStatusCode:(NSInteger)code {
[[[mock stub] andReturn:serializedData] sendEvents:[OCMArg any]
returningResponse:[OCMArg setTo:response]
error:[OCMArg setTo:nil]];


[[[mock stub] andReturnValue:network] isNetworkConnected];

return mock;
}

Expand All @@ -446,6 +453,8 @@ - (id)uploadTestHelperWithDataInstanceClient:(id)data andStatusCode:(NSInteger)c
[[[mock stub] andReturn:serializedData] sendEvents:[OCMArg any]
returningResponse:[OCMArg setTo:response]
error:[OCMArg setTo:nil]];

[[[mock stub] andReturnValue:@YES] isNetworkConnected];

return mock;
}
Expand Down Expand Up @@ -549,13 +558,23 @@ - (void)testUploadFailedBadRequestUnknownError {

- (void)testUploadFailedBadRequestUnknownErrorInstanceClient {
id mock = [self uploadTestHelperWithDataInstanceClient:@{} andStatusCode:400];

[self addSimpleEventAndUploadWithMock:mock];

// make sure the file wasn't deleted locally
STAssertTrue([[KeenClient getEventStore] getTotalEventCount] == 1, @"An upload that results in an unexpected error should not delete the event.");
}

- (void)testUploadSkippedNoNetwork {
id mock = [self uploadTestHelperWithData:nil andStatusCode:200 andNetwork:@NO];

NSLog(@"my failure here.");
[self addSimpleEventAndUploadWithMock:mock];

// make sure the file wasn't deleted locally
STAssertTrue([[KeenClient getEventStore] getTotalEventCount] == 1, @"An upload with no network should not delete the event.");
}

- (void)testUploadMultipleEventsSameCollectionSuccess {
NSDictionary *result1 = [self buildResultWithSuccess:YES
andErrorCode:nil
Expand Down
98 changes: 98 additions & 0 deletions Library/Reachability/Reachability.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
File: Reachability.h
Abstract: Basic demonstration of how to use the SystemConfiguration Reachablity APIs.
Version: 3.5
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms. If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Copyright (C) 2014 Apple Inc. All Rights Reserved.
*/

#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <netinet/in.h>


typedef enum : NSInteger {
NotReachable = 0,
ReachableViaWiFi,
ReachableViaWWAN
} NetworkStatus;


extern NSString *kReachabilityChangedNotification;


@interface Reachability : NSObject

/*!
* Use to check the reachability of a given host name.
*/
+ (instancetype)reachabilityWithHostName:(NSString *)hostName;

/*!
* Use to check the reachability of a given IP address.
*/
+ (instancetype)reachabilityWithAddress:(const struct sockaddr_in *)hostAddress;

/*!
* Checks whether the default route is available. Should be used by applications that do not connect to a particular host.
*/
+ (instancetype)reachabilityForInternetConnection;

/*!
* Checks whether a local WiFi connection is available.
*/
+ (instancetype)reachabilityForLocalWiFi;

/*!
* Start listening for reachability notifications on the current run loop.
*/
- (BOOL)startNotifier;
- (void)stopNotifier;

- (NetworkStatus)currentReachabilityStatus;

/*!
* WWAN may be available, but not active until a connection has been established. WiFi may require a connection for VPN on Demand.
*/
- (BOOL)connectionRequired;

@end
Loading

0 comments on commit 1d867cc

Please sign in to comment.