Skip to content

Commit

Permalink
refactor code base to be more lightweight
Browse files Browse the repository at this point in the history
  • Loading branch information
shams-ahmed committed Aug 21, 2016
1 parent df26b3e commit c846bb1
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 23 deletions.
6 changes: 3 additions & 3 deletions Classes/Source/SANetworkTester.m
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,13 @@ - (NSString *)shortErrorFromError:(NSError *)error {
// Handle DNS errors as a special case.
if ([error.domain isEqualToString:(NSString *)kCFErrorDomainCFNetwork] &&
(error.code == kCFHostErrorUnknown)) {
failureKey = [error.userInfo objectForKey:(id)kCFGetAddrInfoFailureKey];
failureKey = (error.userInfo)[(id)kCFGetAddrInfoFailureKey];

if (failureKey.intValue != 0) {
failedReason = gai_strerror(failureKey.intValue);

if (failedReason != NULL) {
result = [NSString stringWithUTF8String:failedReason];
result = @(failedReason);
}
}
}
Expand Down Expand Up @@ -211,7 +211,7 @@ - (void)stopPingWithError:(NSError *)error {
self.errorHandler(hostAddress, error);
}
} else {
__block NSNumber *responses = [NSNumber numberWithInteger:_attempts];
__block NSNumber *responses = @(_attempts);

if ([self.networkTesterDelegate respondsToSelector:@selector(didReceiveNetworkResponse:)]) {
dispatch_async(dispatch_get_main_queue(), ^
Expand Down
7 changes: 6 additions & 1 deletion Example/SANetworkTester.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@
D5238F2F18EB01C1007E9A43 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0510;
LastUpgradeCheck = 0730;
ORGANIZATIONNAME = SA;
TargetAttributes = {
D5238F5118EB01C1007E9A43 = {
Expand Down Expand Up @@ -338,6 +338,7 @@
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_OPTIMIZATION_LEVEL = 0;
Expand Down Expand Up @@ -401,6 +402,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "SANetworkTester/SANetworkTester-Prefix.pch";
INFOPLIST_FILE = "SANetworkTester/SANetworkTester-Info.plist";
PRODUCT_BUNDLE_IDENTIFIER = "SA.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
Expand All @@ -414,6 +416,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "SANetworkTester/SANetworkTester-Prefix.pch";
INFOPLIST_FILE = "SANetworkTester/SANetworkTester-Info.plist";
PRODUCT_BUNDLE_IDENTIFIER = "SA.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
Expand All @@ -435,6 +438,7 @@
"$(inherited)",
);
INFOPLIST_FILE = "SANetworkTesterTests/SANetworkTesterTests-Info.plist";
PRODUCT_BUNDLE_IDENTIFIER = "SA.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "$(BUNDLE_LOADER)";
WRAPPER_EXTENSION = xctest;
Expand All @@ -453,6 +457,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "SANetworkTester/SANetworkTester-Prefix.pch";
INFOPLIST_FILE = "SANetworkTesterTests/SANetworkTesterTests-Info.plist";
PRODUCT_BUNDLE_IDENTIFIER = "SA.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "$(BUNDLE_LOADER)";
WRAPPER_EXTENSION = xctest;
Expand Down
49 changes: 32 additions & 17 deletions Example/SANetworkTester/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,22 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
}
#endif

/* 1. Active Network test - i.e wifi or data */
/*
[self exampleGetActiveNetworkStatus];

// Example 1
[self exampleGoogleDNSUsingDelegate];

// Example 2
// [self exampleGoogleDNSUsingBlock];

return YES;
}

#pragma mark
#pragma mark - SANetworkTester

/// Active network test - i.e WIFI or DATA status
- (void)exampleGetActiveNetworkStatus {
switch ([SANetworkTester networkStatus]) {
case SANotReachable:
[self showAlert:@"Network is not reachable via WIFI, DATA or WWAN"];
Expand All @@ -35,12 +49,15 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
default:
break;
}
*/

/* 2. Ping test with Delegate approach*/
// [SANetworkTester googleDnsWithDelegate:self];

/* 3. Ping test with Block approach*/
}

/// Ping test with Delegate approach
- (void)exampleGoogleDNSUsingDelegate {
[SANetworkTester googleDnsWithDelegate:self];
}

/// Ping test with Block approach
- (void)exampleGoogleDNSUsingBlock {
__weak typeof(self) weakSelf = self;

[SANetworkTester googleDNSWithCompletion:^(NSNumber *response) {
Expand All @@ -52,27 +69,25 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(

[strongSelf showAlert:[NSString stringWithFormat:@"Failed %@ wError: %@", address, error.localizedDescription]];
}];

return YES;
}

#pragma mark
#pragma mark - SANetworkTesterDelegate

- (void)didFailToReceiveResponseFromAddress:(NSString *)address withError:(NSError *)error {
[self showAlert:[NSString stringWithFormat:@"failed %@ wError: %@", address, error.localizedDescription]];
[self showAlert:[NSString stringWithFormat:@"Failed %@ wError: %@", address, error.localizedDescription]];
}

- (void)didReceiveNetworkResponse:(NSNumber *)response {
[self showAlert:[NSString stringWithFormat:@"received %@ packets", response]];
[self showAlert:[NSString stringWithFormat:@"Received %@ packets", response]];
}

#pragma mark
#pragma mark - Helper method for demo purpose only
#pragma mark - Private - Helper for demo purpose only

- (void)showAlert:(NSString *)message {
dispatch_async(dispatch_get_main_queue(), ^{
[[[UIAlertView alloc] initWithTitle:@"SANetworkTester"
[[[UIAlertView alloc] initWithTitle:@"SANetworkTester feedback"
message:message
delegate:nil
cancelButtonTitle:@"Ok"
Expand All @@ -82,12 +97,12 @@ - (void)showAlert:(NSString *)message {
}

#pragma mark
#pragma mark - Test Helper
#pragma mark - Private - Test Helper

+ (BOOL)isTesting {
NSDictionary *environment = [[NSProcessInfo processInfo] environment];
NSDictionary *environment = [NSProcessInfo processInfo].environment;

return [environment objectForKey:@"TESTING"] != nil;
return environment[@"TESTING"] != nil;
}

@end
2 changes: 1 addition & 1 deletion Example/SANetworkTester/SANetworkTester-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIdentifier</key>
<string>SA.${PRODUCT_NAME:rfc1034identifier}</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIdentifier</key>
<string>SA.${PRODUCT_NAME:rfc1034identifier}</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
Expand Down

0 comments on commit c846bb1

Please sign in to comment.