I want to check the user whether using whether wifi/3G connect or not, how can I check this behavior? thank you.
Use Apple's "Reachability" sample app. It's become the code most used for determining network connectivity.
http://developer.apple.com/iphone/library/samplecode/Reachability/Introduction/Intro.html
You can check once at launch time for network access, or set yourself up as a notification consumer of network connection change messages.
This is important these days because Apple will test your app in Airplane Mode, and if it requires network connections it can't get, but doesn't handle that nicely, they'll bounce your submission.
Related
Suppose I have a application which fetch data from network and then showing those data in application. But sometimes internet becomes disconnected so I am showing message that 'Internet not available'. But I want to keep user busy with the application. So I want to do following things but there are some problem in implementation.
1) When network disconnected I want to show message 'network disconnect' and I want to store all data in db.
Problem: How will I know network is not available without happening any event?
2) And when network is connected I want to fetch data and store in buffer.
Problem : Same, how will I know network is available rightnow?
This is quite common question and is best solved using Reachability class.
You can find a nice manual here: Testing Network Reachability
And a quick answer is the accepted answer here:
iPhone reachability checking
But be aware that it's best to check for host availabiility. I tested this code when device was connected to AdHoc WiFi (with no internet connectivity) and the result was: internet available via WiFi. So if you want to be sure - use host status.
EDIT: and this is probably the best example on SO (answer from iWasRobbed):
How to check for an active Internet connection on iOS or OSX?
I want to check the network requests an app is making from my iPhone. It's on the same WiFi network as my computer (or if it makes things easier, I can set it up to use an ad-hoc network). I don't want to see every packet, just the URLs which my iPhone is requesting. I don't care about the returned data all that much.
A simple solution would be much appreciated.
If you want to intercept the phone itself you'll need to point it at an http proxy you set up on a computer and watch the requests come through. Something like http://www.charlesproxy.com/ or there are most likely many free proxies.
Connect your computer to the rest
of your local network via Ethernet.
Turn on Internet Sharing from the
Sharing System Preference to share
your Ethernet connection via
AirPort.
Set your iPhone to
connect to the computer as its base
station.
Use Wireshark to
capture and analyze the packets.
I found a really nice repo on github named Wormholy https://github.com/pmusolino/Wormholy it will show every network request on your iphone, you only add it to your pod file and then on your app, you shake your phone and you will see all requests.
Easy to install
Transparent on your app usage
Overview and details of your request
Like so
Screenshot of wormholy usage
I have a requirement to promote 3G/GPRS over WiFi connectivity in the iPhone application I am developing. Please let me know if this is possible and if yes, how?
The problem is when I have a WiFi network which is available and connected but not logged in using Captive Portal, I cannot use that WiFi network to perform the tasks which require internet connection. What i have noticed is Reachability API detects the available WiFi but I cannot use it as I am not logged in and so I am not connected to internet using this WiFi hotspot until I login. So I want to use 3G/GPRS to perform the tasks requiring internet connectivity.
iPhone's default behavior is it uses WiFi when its available and if not then only 3G/GPRS.
So please let me know if both 3G/GPRS and WiFi are available but I am connected to internet not using WiFi but 3G/GPRS how to use 3G/GRPS to perform the tasks requiring internet connectivity.
Regards,
Third-party applications don't get control of where their data comes from. About all you're going to be able to do is ask your users to turn wifi off while your application does its thing. If you need to get back onto wifi after doing the initial data transfer, you could make some clever use of the iOS 4 multitasking and local-notification APIs: when your application enters the background, it could start a background-task handler (using -beginBackgroundTaskWithExpirationHandler: in your app delegate's -applicationDidEnterBackground:) to wait for the wifi connection to become unavailable, do its thing with the cell network, then create a UILocalNotification to tell the user it's okay to turn wifi back on again. That way, the user could get the benefits of whatever your app does and still keep the connection they started out on.
If you are creating an app which needs to access certain network resource(s) to function, then you should just ping, or try to connect to those resources directly instead of just depending on reachability for the decision. If you can't ping your server, then have the app do the same thing it would if reachability reported zero connectivity (even though it may not be).
For security reasons, Apple doesn't allow developer to do such settings pro grammatically, despite you GPRS network connection to achieve some purposes.
I need to make a network connection over WWAN (i.e. the mobile network) on an iPhone, even when the device is connected to a Wi-Fi network, however I can't find a way to do this.
I've tried going down to the socket level and iterating through the available interfaces, however when connected to Wi-Fi, the WWAN interface (pdp_ip0) disappears.
The solution needs to be App Store safe.
Enumerate the network interfaces, you will see that when you're connected on both wifi and 3g, there are 2 with different local ip addresses. You might be able to use one vs the other by forcing a bind of your socket on the right interface before sending the data. The kernel which tries to find the best interface to route your packet should be happy with your choice.
Disclaimer: I have not tried this, this is just a suggestion.
Perhaps you can use the Reachability code to determine if Wi-Fi is enabled, firing a UIAlertView to warn the users to quit the app, open the Settings app and switch off wireless manually. Not ideal, definitely.
There is no supported way to to do this. You need to tell the user to turn off the WiFi connection since "It's a non-negotiable operational requirement for the service the app needs to connect to."
In this scenario, the user is not likely to kill you with bad reviews if you are clear about why they have to disable WiFi.
-t
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.