I am using Apple's reachability code, and am setting up both initial notifications on when network reachability changes and prior to initiating a server connection. The code works when I am on wi-fi and I turn the wi-fi access point off. However, when I start the app with wi-fi and the underlying broadband connection working, and then once the app is running, and then disconnect the wi-fi router from the broadband router (i.e. Wi-Fi is on but there is no internet connectivity), and I do a reachability check, the network status I get is ReachableViaWiFi. I have tried both reachabilityForInternetConnection and reachabilityWithHostName.
Any ideas on if Apple's reachability code can be used to detect a situation where the wifi is connected but there is no underlying network connectivity?
Thanks!
ok, I found out the answer to this - Apple's reachability does not test actual connectivity to the host. See the answer by #Zhami in the SO link below:
How to write a simple Ping method in Cocoa/Objective-C
Essentially, when you first launch the app and do a reachability check, iOS seems to do a DNS lookup, and if there is no internet, the check fails. So the first time you check reachability , it actually returns a meaningful value. However, if you are conected at app launch, and lose internet connectivity after some time (while still connected to WiFi/3G/4G but no underlying internet connectivity), further reachability checks return reachable even though the internet or your specified host is not reachable anymore.
So if you want to really check for connectivity in real time, consider using the following:
-(BOOL) isConnected
{
NSString* url = [NSURL URLWithString:#"http://www.google.com/m"];
ASIHTTPRequest* request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
[request setTimeOutSeconds:10];
//customize as per your needs - note this check is synchronous so you dont want to block the main thread for too long
[request setNumberOfTimesToRetryOnTimeout:0];
[request startSynchronous];
NSError *error = [request error];
if (error)
{
DLog(#"connectivity error");
return NO;
}
else
{
DLog(#"connectivity OK");
return YES;
}
}
This is a very old post, but could stay here for reference. In the reachability example class you can find the code below:
- (BOOL)startNotifier
{
BOOL returnValue = NO;
SCNetworkReachabilityContext context = {0, (__bridge void *)(self), NULL, NULL, NULL};
if (SCNetworkReachabilitySetCallback(_reachabilityRef, ReachabilityCallback, &context))
{
if (SCNetworkReachabilityScheduleWithRunLoop(_reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode))
{
returnValue = YES;
}
}
return returnValue;
}
This will keep your _reachabilityRef updated to network changes.
Related
I'm using the Multipeer Connectivity Framework to transfer files between devices. I'm using the standard MCAdvertiserAssistant and MCBrowserViewController to connect the devices. On the first try from device A to device B things work fine. Same things on the first transfer from device B to device A.
If you try either direction again, after MCBrowserViewController presents its dialog to choose a peer and you select one, the popup to accept the request on the other device never appears. No error messages, no calls to delegate methods - just nothing. Has anyone come across this and any ideas?
I had the same problem and solved it with initiating all the necessary components every time I start advertising or browsing for peers. It isn't the cleanest solution but in my case it works 100%.
The code below is how I implemented it, so this is without the build-in ViewController provided by Apple.
Please be aware that [session disconnect] is an async method which sometimes take a few seconds to complete.
- (void)startBrowsing
{
// Initiate new advertiser
isAdvertising = YES;
_peerID = [[MCPeerID alloc] initWithDisplayName:#"Wallet"];
_session = [[MCSession alloc] initWithPeer:_peerID];
_session.delegate = self;
_advertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:_peerID discoveryInfo:nil serviceType:#"made2pay"];
_advertiser.delegate = self;
// Start advertiser
[_advertiser startAdvertisingPeer];
}
- (void)stopBrowsing
{
[_advertiser stopAdvertisingPeer];
[_session disconnect];
_session = nil;
_peerID = nil;
_advertiser = nil;
isAdvertising = NO;
}
I use the SocketRocket library for Objective-C to connect to a websocket:
-(void)open {
if( self.webSocket ) {
[self.webSocket close];
self.webSocket.delegate = nil;
}
self.webSocket = [[SRWebSocket alloc] initWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"ws://192.168.0.254:5864"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20]];
self.webSocket.delegate = self;
[self.webSocket open];
}
Opening the connection works totally fine. The delegate is called after the connection was established.
-(void)webSocketDidOpen:(SRWebSocket *)webSocket {
NSLog(#"WebSocket is open");
}
But when I want to close the connection, nothing happens.
-(void)close {
if( !self.webSocket )
return;
[self.webSocket close];
self.webSocket.delegate = nil;
}
The delegate for successfully closing the connection is not called. Can anyone tell me why this happens?
Thank you for reading my question.
I figured out that the delegate is never called, because the websocket is never really closed. The closing of the websocket in the SRWebSocket happens in the method pumpWriting like this:
if (_closeWhenFinishedWriting &&
_outputBuffer.length - _outputBufferOffset == 0 &&
(_inputStream.streamStatus != NSStreamStatusNotOpen &&
_inputStream.streamStatus != NSStreamStatusClosed) &&
!_sentClose) {
_sentClose = YES;
[_outputStream close];
[_inputStream close];
if (!_failed) {
dispatch_async(_callbackQueue, ^{
if ([self.delegate respondsToSelector:#selector(webSocket:didCloseWithCode:reason:wasClean:)]) {
[self.delegate webSocket:self didCloseWithCode:_closeCode reason:_closeReason wasClean:YES];
}
});
}
_selfRetain = nil;
NSLog(#" Is really closed and released ");
}
else {
NSLog(#" Is NOT closed and released ");
}
All streams and an object to retain the websocket are closed or deleted there. As long as they are still open, the socket won´t be closed appropriately. But the closing never happened in my program, because when I tried to close the websocket, _closeWhenFinishedWriting was always NO.
This boolean is only set once in the disconnect method.
- (void)_disconnect;
{
assert(dispatch_get_current_queue() == _workQueue);
SRFastLog(#"Trying to disconnect");
_closeWhenFinishedWriting = YES;
[self _pumpWriting];
}
But when calling the closeWithCode method in SRWebSocket, disconnect is only called in one case and that is, if the websocket is in the connecting state.
BOOL wasConnecting = self.readyState == SR_CONNECTING;
SRFastLog(#"Closing with code %d reason %#", code, reason);
dispatch_async(_workQueue, ^{
if (wasConnecting) {
[self _disconnect];
return;
}
This means, if the socket is in another state, the websocket will never really close. One workaround is to always call the disconnect method. At least it worked for me and everything seems to be alright.
If anyone has an idea, why SRWebSocket is implemented like that, please leave a comment for this answer and help me out.
I think this is a bug.
When calling close, the server echo's back the 'close' message.
It is received by SRWebSocket, however the _selfRetain is never set to nil, and the socket remains open (the streams are not closed) and we have a memory leak.
I have checked and observed this in the test chat app as well.
I made the following change:
-(BOOL)_innerPumpScanner {
BOOL didWork = NO;
if (self.readyState >= SR_CLOSING) {
[self _disconnect]; // <--- Added call to disconnect which releases _selfRetain
return didWork;
}
Now the socket closes, the instance is released, and the memory leak is gone.
The only thing that I am not sure of is if the delegate should be called when closing in this way. Will look into this.
Once an endpoint has both sent and received a Close control frame, that endpoint SHOULD Close the WebSocket Connection as defined in Section 7.1.1. (RFC 6455 7.1.2)
The SRWebSocket instance doesn't _disconnect here because that would close the TCP connection to the server before the client has received a Close control frame in response. In fact, _disconnecting here will tear down the TCP socket before the client can even send its own Close frame to the server, because _disconnect ultimately calls _pumpWriting before closeWithCode: can. The server will probably respond gracefully enough, but it's nonconforming, and you won't be able to send situation-unique close codes while things are set up this way.
This is properly dealt with in handleCloseWithData:
if (self.readyState == SR_OPEN) {
[self closeWithCode:1000 reason:nil];
}
dispatch_async(_workQueue, ^{
[self _disconnect];
});
This block handles Close requests initiated by both the client and the server. If the server sends the first Close frame, the method runs as per the sequence you laid out, ultimately ending up in _pumpWriting via closeWithCode:, where the client will respond with its own Close frame. It then goes on to tear down the connection with that _disconnect.
When the client sends the frame first, closeWithCode: runs once without closing the TCP connection because _closeWhenFinishedWriting is still false. This allows the server time to respond with its own Close frame, which would normally result in running closeWithCode: again, but for the following block at the top of that method:
if (self.readyState == SR_CLOSING || self.readyState == SR_CLOSED) {
return;
}
Because the readyState is changed on the first iteration of closeWithCode:, this time it simply won't run.
emp's bug fix is necessary to make this work as intended, however: otherwise the Close frame from the server doesn't do anything. The connection will still end, but dirtily, because the server (having both sent and received its frames) will break down the socket on its end, and the client will respond with an NSStreamEventEndEncountered:, which is normally reserved for stream errors caused by sudden losses of connectivity. A better approach would be to determine why the frame never gets out of _innerPumpScanner to handleCloseWIthData:. Another issue to keep in mind is that by default, close just calls closeWithCode: with an RFC-nonconforming code of -1. This threw errors on my server until I changed it to send one of the accepted values.
All that said: your delegate method doesn't work because you're unsetting the delegate right after you call close. Everything in close is inside an async block; there won't be a delegate left to call by the time you invoke didCloseWithCode: regardless of what else you do here.
I use Reachability from App Developer Library to check the internet connection, the code is as below:
+(BOOL)reachable {
Reachability *r = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [r currentReachabilityStatus];
Reachability *r1 = [Reachability reachabilityForLocalWiFi];
NetworkStatus internetStatus1 = [r1 currentReachabilityStatus];
Reachability *r2 = [Reachability reachabilityWithHostName:FTPURL];
NetworkStatus internetStatus2 = [r2 currentReachabilityStatus];
DLog(#"%d,%d,%d",internetStatus,internetStatus1,internetStatus2);
if(internetStatus != NotReachable && internetStatus1 != NotReachable && internetStatus2 != NotReachable)
{
return YES;
}
return NO;
}
If the wifi is available, the three status are sure to be "Reachable". If I turn off the wifi manually (Network connection flag is off), all of the three status are"NOTReachable".
When I turn off the Modem, while keep the wireless router open (Network connection flag is on). Although I can not access internet, all of the three status are still "Reachable". I want to know why this happen and how to detect this UnReachable statu?
There is no distinction as far as your iPhone is concerned between a very small network (i.e. your wireless LAN) and the Internet. All it can say for certain is that it has a connection to a TCP/IP network. It has no way of telling how big that network is.
If you want to find out for certain if your server is reachable from any particular iPhone, you must try to connect to it. Even then, it might disappear between you testing the connection and sending real data. So whether you test for a connection or not before you start, you still have to handle loss of the connection gracefully during data transmission. You might as well not bother checking in advance and just assume it's there and have good code for handling transmission errors.
I'm trying to set up a TCP connection to a local server, and since pretty much everyone agrees that Asyncsocket is the way to go, I went for it, too. However, I'm running into problems at the most basic level: the Asyncsocket instance doesn't have a local or remote address. I don't have much code, but this is what I do have:
- (void)viewDidLoad {
[super viewDidLoad];
AsyncSocket *socket = [[AsyncSocket alloc] initWithDelegate:self];
[socket connectToHost:#"www.google.com" onPort:80 error:nil];
}
- (BOOL)onSocketWillConnect:(AsyncSocket *)sock {
NSLog(#"%#", sock);
return YES;
}
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port {
NSLog(#"%#", host);
}
The following shows up in the console (from the NSLog in onSocketWillConnect):
AsyncSocket 0x298de0 local nowhere remote nowhere has queued 0 reads 0 writes, no current read, no current write, read stream 0x299720 not open, write stream 0x299aa0 not open, not connected
Where it says "nowhere", of course it should have IP addresses, but it doesn't. From examples online it looks like my code is fine, yet I get this "nowhere" thing when running on two computers and my phone, so I'm probably doing something wrong. Anybody have an idea?
Thanks!
edit: For clarification: if I use error reporting (by passing an NSError instance to "connectToHost:onPort:error" and NSLogging that), all I get back is (null).
Wow, that's embarrassing. I needed to make an ivar to store the AsyncSocket, because without it, it went out of scope after viewDidLoad completed, which was why my onSocket:didConnectToHost:port wasn't called.
I try to retrive data from certain url with command:
-(NSMutableData *) callUrl: (NSString *)url withData:(NSMutableDictionary *)data delegate:(id) delegate {
NSURL *executeUrl = [NSURL URLWithString:<string>];
NSURLRequest *request = [NSURLRequest requestWithURL: executeUrl
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60];
NSMutableData *receivedData = nil;
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:delegate];
if (theConnection) {
receivedData = [[NSMutableData data] retain];
} else {
#throw #"Connection error";
}
return receivedData;
}
In delegate (both after connectionDidFinish and connectionDidFailWithError) I do:
//some uninvasive alerts
// release the connection, and the data object
[connection release];
[receivedData release];
Problem is when I provide bad url I got proper error - it's good part - but then I want to execute second url - good for sure, I've got 1003 error - NSURLErrorCannotFindHost.
After around 1-2 min I'm succesfully call url and get data.
I suspect some timeouts and ports business, but changing timeout in NSURLRequest doesn't change a thing.
UPDATE
As it turned out - Administrators had some issues with DNS server reached through WiFi network. Code is fine. Thanks for response.
If some has similiar problems: try ip address instead of hostname.
From Apple iOS developer documentation, 1003 error refers to when the host name for a URL cannot be resolved. To avoid DNS failures in wifi, overloaded DNS scenarios, it is preferable to resolve ip from hostname for subsequent use or to hardcode the ip address directly, if you do not intend to shift the hosting later on.
Apple documentation:
URL Loading System Error Codes
These values are returned as the error code property of an NSError object with the domain “NSURLErrorDomain”.
enum
{
NSURLErrorBadURL = -1000,
NSURLErrorTimedOut = -1001,
NSURLErrorUnsupportedURL = -1002,
NSURLErrorCannotFindHost = -1003,//****
NSURLErrorCannotConnectToHost = -1004,
NSURLErrorDataLengthExceedsMaximum = -1103,
NSURLErrorNetworkConnectionLost = -1005,
NSURLErrorDNSLookupFailed = -1006,
...
}
1003 NSURLErrorCannotFindHost
Returned when the host name for a URL cannot be resolved.
Available in iOS 2.0 and later.
Declared in NSURLError.h.
I did 2 things to fix this issue :
I used this before initiating my NSUrlConnection
[NSURLConnection cancelPreviousPerformRequestsWithTarget:self];
I changed my DNS to 8.8.8.8 in wifi settings which is google's public DNS server.
Don't know which one fixed it but the issue was resolved.
before making any new connection call cancel previous connection.
using
[self.connection cancel];