Skip to content

Commit c512bb6

Browse files
Ciprian Redinciuccustompods
authored andcommitted
AFNetworkingReachabilityManager support for IPv6. (#1)
* AFNetworkingReachabilityManager support for IPv6. * Podspec update for 2.6.4.
1 parent 806d870 commit c512bb6

4 files changed

Lines changed: 57 additions & 40 deletions

File tree

AFNetworking.podspec

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
Pod::Spec.new do |s|
22
s.name = 'AFNetworking'
3-
s.version = '2.6.3'
3+
s.version = '2.6.4'
44
s.license = 'MIT'
55
s.summary = 'A delightful iOS and OS X networking framework.'
6-
s.homepage = 'https://github.com/AFNetworking/AFNetworking'
6+
s.homepage = 'https://github.com/custompods/AFNetworking'
77
s.social_media_url = 'https://twitter.com/AFNetworking'
88
s.authors = { 'Mattt Thompson' => 'm@mattt.me' }
9-
s.source = { :git => 'https://github.com/AFNetworking/AFNetworking.git', :tag => s.version, :submodules => true }
9+
s.source = { :git => 'https://github.com/custompods/AFNetworking.git', :tag => s.version, :submodules => true }
1010
s.requires_arc = true
11-
11+
1212
s.public_header_files = 'AFNetworking/AFNetworking.h'
1313
s.source_files = 'AFNetworking/AFNetworking.h'
14-
14+
1515
pch_AF = <<-EOS
1616
#ifndef TARGET_OS_IOS
1717
#define TARGET_OS_IOS TARGET_OS_IPHONE
@@ -22,11 +22,11 @@ Pod::Spec.new do |s|
2222
#endif
2323
EOS
2424
s.prefix_header_contents = pch_AF
25-
25+
2626
s.ios.deployment_target = '7.0'
2727
s.osx.deployment_target = '10.9'
2828
s.watchos.deployment_target = '2.0'
29-
29+
3030
s.subspec 'Serialization' do |ss|
3131
ss.source_files = 'AFNetworking/AFURL{Request,Response}Serialization.{h,m}'
3232
ss.public_header_files = 'AFNetworking/AFURL{Request,Response}Serialization.h'
@@ -67,7 +67,7 @@ EOS
6767
ss.ios.deployment_target = '7.0'
6868
ss.osx.deployment_target = '10.9'
6969
ss.watchos.deployment_target = '2.0'
70-
70+
7171
ss.dependency 'AFNetworking/Serialization'
7272
ss.ios.dependency 'AFNetworking/Reachability'
7373
ss.osx.dependency 'AFNetworking/Reachability'

AFNetworking/AFNetworkReachabilityManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ NS_ASSUME_NONNULL_BEGIN
9393
/**
9494
Creates and returns a network reachability manager for the socket address.
9595
96-
@param address The socket address (`sockaddr_in`) used to evaluate network reachability.
96+
@param address The socket address (`sockaddr_in6`) used to evaluate network reachability.
9797
9898
@return An initialized network reachability manager, actively monitoring the specified socket address.
9999
*/

AFNetworking/AFNetworkReachabilityManager.m

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static AFNetworkReachabilityStatus AFNetworkReachabilityStatusForFlags(SCNetwork
5353
BOOL canConnectionAutomatically = (((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) || ((flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0));
5454
BOOL canConnectWithoutUserInteraction = (canConnectionAutomatically && (flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0);
5555
BOOL isNetworkReachable = (isReachable && (!needsConnection || canConnectWithoutUserInteraction));
56-
56+
5757
AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusUnknown;
5858
if (isNetworkReachable == NO) {
5959
status = AFNetworkReachabilityStatusNotReachable;
@@ -66,7 +66,7 @@ static AFNetworkReachabilityStatus AFNetworkReachabilityStatusForFlags(SCNetwork
6666
else {
6767
status = AFNetworkReachabilityStatusReachableViaWiFi;
6868
}
69-
69+
7070
return status;
7171
}
7272

@@ -116,23 +116,32 @@ + (instancetype)sharedManager {
116116
static AFNetworkReachabilityManager *_sharedManager = nil;
117117
static dispatch_once_t onceToken;
118118
dispatch_once(&onceToken, ^{
119+
#if (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 90000) || (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101100)
120+
struct sockaddr_in6 address;
121+
bzero(&address, sizeof(address));
122+
address.sin6_len = sizeof(address);
123+
address.sin6_family = AF_INET6;
124+
125+
_sharedManager = [self managerForAddress:&address];
126+
#else
119127
struct sockaddr_in address;
120128
bzero(&address, sizeof(address));
121129
address.sin_len = sizeof(address);
122130
address.sin_family = AF_INET;
123-
131+
124132
_sharedManager = [self managerForAddress:&address];
133+
#endif
125134
});
126-
135+
127136
return _sharedManager;
128137
}
129138

130139
#ifndef __clang_analyzer__
131140
+ (instancetype)managerForDomain:(NSString *)domain {
132141
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [domain UTF8String]);
133-
142+
134143
AFNetworkReachabilityManager *manager = [[self alloc] initWithReachability:reachability];
135-
144+
136145
return manager;
137146
}
138147
#endif
@@ -141,7 +150,7 @@ + (instancetype)managerForDomain:(NSString *)domain {
141150
+ (instancetype)managerForAddress:(const void *)address {
142151
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr *)address);
143152
AFNetworkReachabilityManager *manager = [[self alloc] initWithReachability:reachability];
144-
153+
145154
return manager;
146155
}
147156
#endif
@@ -151,10 +160,10 @@ - (instancetype)initWithReachability:(SCNetworkReachabilityRef)reachability {
151160
if (!self) {
152161
return nil;
153162
}
154-
163+
155164
self.networkReachability = CFBridgingRelease(reachability);
156165
self.networkReachabilityStatus = AFNetworkReachabilityStatusUnknown;
157-
166+
158167
return self;
159168
}
160169

@@ -185,27 +194,27 @@ - (BOOL)isReachableViaWiFi {
185194

186195
- (void)startMonitoring {
187196
[self stopMonitoring];
188-
197+
189198
if (!self.networkReachability) {
190199
return;
191200
}
192-
201+
193202
__weak __typeof(self)weakSelf = self;
194203
AFNetworkReachabilityStatusBlock callback = ^(AFNetworkReachabilityStatus status) {
195204
__strong __typeof(weakSelf)strongSelf = weakSelf;
196-
205+
197206
strongSelf.networkReachabilityStatus = status;
198207
if (strongSelf.networkReachabilityStatusBlock) {
199208
strongSelf.networkReachabilityStatusBlock(status);
200209
}
201-
210+
202211
};
203-
212+
204213
id networkReachability = self.networkReachability;
205214
SCNetworkReachabilityContext context = {0, (__bridge void *)callback, AFNetworkReachabilityRetainCallback, AFNetworkReachabilityReleaseCallback, NULL};
206215
SCNetworkReachabilitySetCallback((__bridge SCNetworkReachabilityRef)networkReachability, AFNetworkReachabilityCallback, &context);
207216
SCNetworkReachabilityScheduleWithRunLoop((__bridge SCNetworkReachabilityRef)networkReachability, CFRunLoopGetMain(), kCFRunLoopCommonModes);
208-
217+
209218
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),^{
210219
SCNetworkReachabilityFlags flags;
211220
if (SCNetworkReachabilityGetFlags((__bridge SCNetworkReachabilityRef)networkReachability, &flags)) {
@@ -218,7 +227,7 @@ - (void)stopMonitoring {
218227
if (!self.networkReachability) {
219228
return;
220229
}
221-
230+
222231
SCNetworkReachabilityUnscheduleFromRunLoop((__bridge SCNetworkReachabilityRef)self.networkReachability, CFRunLoopGetMain(), kCFRunLoopCommonModes);
223232
}
224233

@@ -240,9 +249,9 @@ + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key {
240249
if ([key isEqualToString:@"reachable"] || [key isEqualToString:@"reachableViaWWAN"] || [key isEqualToString:@"reachableViaWiFi"]) {
241250
return [NSSet setWithObject:@"networkReachabilityStatus"];
242251
}
243-
252+
244253
return [super keyPathsForValuesAffectingValueForKey:key];
245254
}
246255

247256
@end
248-
#endif
257+
#endif

Tests/Tests/AFNetworkReachabilityManagerTests.m

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,32 @@ @implementation AFNetworkReachabilityManagerTests
3333

3434
- (void)setUp {
3535
[super setUp];
36-
36+
3737
//both of these manager objects should always be reachable when the tests are run
3838
self.domainReachability = [AFNetworkReachabilityManager managerForDomain:@"localhost"];
39-
39+
4040
//don't use the shared manager because it retains state between tests
4141
//but recreate it each time in the same way that the shared manager is created
42+
#if (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 90000) || (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101100)
43+
struct sockaddr_in6 address;
44+
bzero(&address, sizeof(address));
45+
address.sin6_len = sizeof(address);
46+
address.sin6_family = AF_INET6;
47+
self.addressReachability = [AFNetworkReachabilityManager managerForAddress:&address];
48+
#else
4249
struct sockaddr_in address;
4350
bzero(&address, sizeof(address));
4451
address.sin_len = sizeof(address);
4552
address.sin_family = AF_INET;
4653
self.addressReachability = [AFNetworkReachabilityManager managerForAddress:&address];
54+
#endif
4755
}
4856

4957
- (void)tearDown
5058
{
5159
[self.addressReachability stopMonitoring];
5260
[self.domainReachability stopMonitoring];
53-
61+
5462
[super tearDown];
5563
}
5664

@@ -73,17 +81,17 @@ - (void)verifyReachabilityNotificationGetsPostedWithManager:(AFNetworkReachabili
7381
status = [note.userInfo[AFNetworkingReachabilityNotificationStatusItem] integerValue];
7482
BOOL reachable = (status == AFNetworkReachabilityStatusReachableViaWiFi
7583
|| status == AFNetworkReachabilityStatusReachableViaWWAN);
76-
84+
7785
XCTAssert(reachable,
7886
@"Expected network to be reachable but got '%@'",
7987
AFStringFromNetworkReachabilityStatus(status));
8088
XCTAssertEqual(reachable, manager.isReachable, @"Expected status to match 'isReachable'");
81-
89+
8290
return YES;
8391
}];
84-
92+
8593
[manager startMonitoring];
86-
94+
8795
[self waitForExpectationsWithTimeout:5 handler:nil];
8896
}
8997

@@ -98,20 +106,20 @@ - (void)testDomainReachabilityNotification {
98106
- (void)verifyReachabilityStatusBlockGetsCalledWithManager:(AFNetworkReachabilityManager *)manager
99107
{
100108
XCTestExpectation *expectation = [self expectationWithDescription:@"reachability status change block gets called"];
101-
109+
102110
typeof(manager) __weak weakManager = manager;
103111
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
104112
BOOL reachable = (status == AFNetworkReachabilityStatusReachableViaWiFi
105113
|| status == AFNetworkReachabilityStatusReachableViaWWAN);
106-
114+
107115
XCTAssert(reachable, @"Expected network to be reachable but got '%@'", AFStringFromNetworkReachabilityStatus(status));
108116
XCTAssertEqual(reachable, weakManager.isReachable, @"Expected status to match 'isReachable'");
109-
117+
110118
[expectation fulfill];
111119
}];
112-
120+
113121
[manager startMonitoring];
114-
122+
115123
[self waitForExpectationsWithTimeout:5 handler:^(NSError *error) {
116124
[manager setReachabilityStatusChangeBlock:nil];
117125
}];
@@ -125,4 +133,4 @@ - (void)testDomainReachabilityBlock {
125133
[self verifyReachabilityStatusBlockGetsCalledWithManager:self.domainReachability];
126134
}
127135

128-
@end
136+
@end

0 commit comments

Comments
 (0)