How to detect/prevent wifi timeouts on iPhone? - iphone

I dont think this is possible, but can client connected to a wifi hotspot detect or even prevent a disconnect due to a timeout?
My guess is that this is not possible as i would imagine this timeout would be a router thing rather than a client thing.
And if a client makes an intervaled ping (e.g. ip address), would this prevent the client from being timed out?

I think that WiFi timeout detection is probably going to be a waste of your time. I would just assume you have connectivity but then include error correction and tell the user if you couldn't get the data.
Oh, and if we were good enough programmers to prevent timeouts... We wouldn't be writing iPhone apps.
If you just make random pings all the time, you're going to A) slow the phone and B) not solve the problem. Timeouts happen for all sorts of reasons, and pinging probably won't prevent it.
On the other hand, if you really want to know about network state... Apple has this:
http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html
I should also note that ASIHTTPRequest has this stuff built in... As far as I know, anyway - it requires it to compile on iPhone.

Related

Initiating comms to an embedded 3G device

I have an Arduino based device interfaced to a 3G modem which I use to record data from several sensors in a remote environment. I would like to be able to send commands and stream some data from the device every now and then back to my standard network connected PC. If the remote device was connected to a WIFI or other local area network this would be relatively straightforward, but as the device connects over 3G this means that it is behind the 3G carriers NAT and so establishing a connection to the device becomes difficult.
The device can, of course, open a TCP connection to my host PC any time it wishes, the problem is telling the device when i want it to do so. I need some way of getting some kind of message to the device to notify it that I would like it to initiate a connection to my PC.
I've been reading up on NAT traversal techniques that app developers use to initiate P2P comms between 2 devices both behind NATs such as UDP and TCP 'hole punching' but this method seems rather too complex for my arduino system. Another general idea is to have the device polling a web server periodically looking for a signal to initiate a connection, but I'm not sure how much traffic (and data usage costs) this would generate as the device would have to poll every 10 seconds or so in order to make sure it initiates it's connection within a reasonable time frame of the request being set on the web server that it polls.
Is there any commonly used method of achieving something like this? Any general ideas or insight would be much appreciated
Thanks,
James
I think the solution will depend largely on your particular applications and requirements.
There are several ways to achieve this type of functionality and it looks like you have covered some of them already. The most common are:
have the device poll the server. This may be ok depending on the response times you need. If you need to poll as regularly as you suggest above then I imagine power may be more important to you than data rates, especially if you are battery connected. With a typical 3G data plan the polling itself will have a negligible data overhead, I would think.
have the server send a SMS which then triggers the device to contact the server. You need to make sure the SMS variable delivery time is ok for you and you also have to be aware that SMS delivery is not guaranteed so you would have to build in some sort of check for delivery at a higher layer (or into your application).
use some low cost Android based device for your 3G connectivity and leverage the Google push notifications mechanism
It is worth noting that server polling typically gets very bad press as it is seems intuitively wasteful to have the client and the server constantly checking for messages, especially when the actual messages are fairly infrequent. However, underneath most push solutions there is still a pull mechanism in the background, albeit generally a very efficient one that may, for example, piggy back on other messages between the network and the mobile device and hence have minimal power and data overhead. Personally, I would say that if you do not have major concerns with battery/power or with the load polling might generate for your servers, then it is worth exploring if the simplicity benefits of a polling solution outweigh its other disadvantages.

Is using airplane mode an acceptable way to test a lack of connection?

We are in the process of developing an method of caching so that our app can continue to operate in an area with very little/no signal.
Obviously users will try to continue to use functions that require data and we need to handle the inevitable failure of these requests appropriately.
Essentially we are sat in the office, switching airplane mode on and off to simulate entering/exiting signal then adjusting our app to fix any issues this may arise.
What I'd like to know is, is using airplane mode going to give us a reasonable simulation of entering/exiting an area with no data or are there other implications?
I've seen questions raising the issue that the 3G/EDGE connection may not always wake up after airplane mode is switched on - while I appreciate this method is no way as good as actually being out in the field testing, if we can get a reasonable simulation and account for the majority of the problems that arise then I think this is an acceptable tradeoff.
I apologise if this has been asked before, I did do a search on here & on google but couldn't find any appropriate results.
You should try the Network Link Conditioner
There is a WWDC 2012 session called Networking Best Practices that mentions it (but he does not explain how to use it there).
To get it, you have to go to XCode/Open Developer Tool/More Developer Tools.. and download the latest Hardware IO Tools for XCode.
Once you install it from the IO Tools pkg, "Network Link Conditioner" will appear in System Preferences
You can then do something like 100% packet loss to simulate one of those routers that pretends you are connected but actually doesn't work.
On iOS, the network link conditioner is under Settings / Developer (you must have enabled Developer mode in XCode first to see it)
The main problem is that in the Airplane Mode the networking operations fail fast, while spotty mobile signal will lead to timeouts and a-few-bytes-an-hour speeds. This is usually a significant difference from the UI viewpoint. (It might be worth a try to use some bandwidth throttle to starve the testing machine and see how it behaves when the network starts to break?)
A few years back, when testing remote devices which used the cell network to communicate with the 'home base', we did things like move them into a shielded room (make shift), place large shields on three of four sides to force them to connect to a certain tower (and therefore, network), etc. Brute force physical methods. Since this actually cuts off the signal, it may be a more realistic approach.
You may also want to try this through your wlan-router. First, disable data roaming on your iPhone. Then, let the iPhone be connected to the internet through your wlan network. Then, disconnect the gateway on your wlan router while your iPhone is still connected to the wlan network.
This depends on what failure modes you are trying to test.
I use Airplane mode as a first pass check to make sure an app submission isn't quickly rejected.
Other network failure handling checks might include:
3G only (no wifi).
WIFI only (in Airplane mode).
Pulling the power cord on the WIFI access point.
Pulling the network cable from the back of the WIFI access point after connecting to it (Reachability may falsely say yes).
Walking in and out of a basement
elevator (or other Faraday cage) in the middle of a transfer.
Driving between 2 cell towers during a data transfer.
Walking between 2 enabled WIFI access points between connection and data transfer.
Starting the app after more than 30 minutes of device inactivity (radios may be idle).
Running the app while another app (Safari, Mail) is downloading in the background.
etc.

detect connection type for iphone on website?

is it possible with javascript, PHP (or anything!) to see if the iphone is using 3g or wifi from a website? We want to load low-res content if its 3G you see,
I found SCNetworkReachabilityFlags but that is just for apps...
Any pointers would be great!
Dan
I wouldn't expect this to be possible at all from the server side. From the client side, as you said, you could use the System Configuration Framework and the SCReachabilityFlags, but only client side.
As far as your server is concerned, it knows nothing about the type of connection any request is coming in on, apart from the immediate connection it has to the internet.
A request from an iPhone may pass through firewalls, routers, bridges, hubs, wireless, wired, almost any kind of network on it's way to you server.
The 3G part is only the connection between the phone and the data provider (AT&T, O2, etc), and after that, it's anybodies guess and will most certainly differ depending on the route between the data provider and your server.
The best advice I can give is to have a landing page, and let the user decide whether or not to load a high or low res version of the site. At least then it's off your hands if they run up a huge data bill...

Losing network connectivity on iPhone

I am developing a network application on iPhone that requires internet connection all the time. However, once I login to the server and keep the iPhone idle for a while, the iPhone goes to sleep mode and disconnects my network connection (it logs me out).
If I run the same application on iPhone, while the iPhone is connected to the PC through USB cable, it never loses its network connection.
In the info.plist file I have added these two flags, but does not seem to have any effect.
UIRequiresPersistentWifi -> true
SBUsesNetwork - integer ->3
Am I missing anything? Could you please let me know how can I make sure that the network connection is persistent throughout the life of the application?
In your application delegate ("appDelegate"), disable the idle timer in the +initialize method:
myApp.idleTimerDisabled = YES;
Note that this will keep your iPhone from sleeping while your app is open. This can present issues with battery life.
Another option might be to set up a background thread that opens a small CFStream on a timed basis.
What do you mean by "logs me out" here? At the network level, there is no "logged in" (*). There are only packets. You send them or you don't. So does your server process have some expectation of packets or messages arriving periodically? If it does, then you must send them, and that means that you can't go idle (idleTimerDisabled = YES). If you control the server, it is better to make it less demanding about how often you talk to it. This all happens well above the network layer, however.
UIRequiresPersistentWifi means that the Wifi radio is kept on while you're app is running, even if you don't talk on it. This is important for receiving data. Otherwise you drop off the network and others can't talk to you after about 30 minutes. It should be set in Info.plist, but this is certainly in your app bundle. If it weren't, your app wouldn't launch, so that isn't the problem.
(*) The cell network does have the concept of logged in, but that's not what's causing your problem.

iPhone Reachability

I am trying to cover my bases with reachability so that my app doesn't get rejected by the App Store. I am familiar with the Reachability class that Apple provides in sample code. My question is, how best to implement this. I've seen that checking if the WWAN is accessible is not always best because it may be turned off for power consumption. So do I have to somehow enable it before checking for a connection. Just looking for steps to take. My app doesn't need a constant connection. It does some polling on a given interval that will require a connection and makes other various requests. Just trying to figure this out so I don't get rejected. Any help would be great.
Edit: Will reporting errors NSURLDomainError errors suffice for reporting reachability? It currently displays No Internet Connection and Can't Find Host. Seems that this is the type of information that the reachability example is used for.
Assuming that your application needs a connection to the internet, you could check the connectivity to a website that is always up (like e.g. google.com). The reachability sample code already contains a method to do just that.
I have not worked with the Reachability API enough to know how it reacts to the system switching off the WWAN for power saving. The only thing that comes to my mind is that you could try to establish a connection to the network so the system switches the WWAN on. Then you can run your reachability code to check what kind of connection you are working with. As said, I have not tried it, just an idea.