Specify Network Type for NSURLConnection & UIWebView - ios

I realize I can determine what type of connection the app has access to within iOS (I have WiFi access; I have Cellular access). However, if I have access to both can I inform a NSURLConnection or UIWebView to use one or the other?
Ideally, a few specific calls I make with NSURLConnection would use cellular network, and a few calls and webview loads would use WiFi.
I don't believe this is possible, but I would like to know concretely.

This isn't something you can specify. It's entirely down to the OS as to which connection to use (cellular vs WiFi) and it will choose the best available based on a number of things like connection strength, WiFi assist etc.

Related

Is there a cellular data usage API in iOS 7?

iOS 7 introduced a new user configuration to disable cellular data for specific apps. It can be configured in "Settings"->"Cellular" and then scrolling down.
You'll find a switch for each installed app and can see how much cellular data it has consumed.
How can I programmatically test if the switch is turned on for my app? Is there an API for that? Can I determine how much data my app has used over cellular?
I'm not asking to get the values for all apps. I'm only interested in my apps usage.
There is no API to detect your download consumption or whether cellular is active for your app.
If your app tries to connect to a website, but cellular is turned off, then iOS may ask the user to turn cellular back on. I'm not exactly sure how that works, but it is probably similar to the iOS 6 "no network connection" alert that would pop up if there is no connection but an app tries to access the internet.
You can check if the current internet connection is over WiFi or Cellular, but if Cellular is disabled you will just be told that there is no network connection.
More details here: iOS Detect 3G or WiFi
You can't check if the cellular data switch is turned on.
The closest thing is that you can check if a specific host is reachable over cellular connection using the SCNetworkReachability kSCNetworkReachabilityFlagsIsWWAN flag.
Additionally, you can enable/disable cellular data for specific connections using the NSURLRequest allowsCellularAccess property.
Reference: https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/NetworkingOverview/Platform-SpecificNetworkingTechnologies/Platform-SpecificNetworkingTechnologies.html
This answer suggests how data usage can be measured system-wide: iPhone Data Usage Tracking/Monitoring

How do I force an iOS app to use 3G instead of WiFi?

I want to use a messaging protocol that works fine over 3G, but not over some corporate firewalls. How can my app force the use of the cellular network when it fails to connect over WiFi even in the case that the WiFi network is reachable?
EDIT: After reading through the implementation of the Reachability class I remain unsure whether the two are indeed mutually exclusive. It could well be possible to discover both interfaces via gethostbyname(), which I might try just to see what comes out.
Use getifaddrs to enumerate the network interfaces. Look for ifa_name that starts with "pdp." This will be the cellular interface (e.g., pdp_ip0). Test (ifa_flags & IFF_UP) to make sure the interface is up, and get the IP address from ifa_addr. Then use bind() to bind your socket to that address.
Not sure how you activate the cellular interface if it is not already up. Normally, I just make a high level http call to get iOS to wake up the network, but not sure under what conditions the cellular service becomes active when wifi is also available. I suspect it is usually there as a fallback to the wifi.
http://iphonedevsdk.com/discussion/comment/120957
From the looks of that thread, it seems like its impossible. Your best bet may be to try using the protocol, and saying something like "please disconnect from wifi and retry" if it doesn't work.
This thread says just about the same thing:
how to programatically start 3g connection on iphone?
You can use the Reachability class which apple wrote in order to test whether the network connection is currently over 3G or WiFi, and even to check if a specific resource is available on the network.
You can use any of the socket library to force to use cellular. Find the cellular interface from getifaddrs, pass the interface to the socket function, socket bind will happen with cellular. e.g. CocoaAsyncSocket

How to make iOS believe there is Internet Connectivity

I am working on a web application for iOS that is going to be accesed from a local webserver in a network that has NO internet connectivity at all.
My problem is that everytime an iOS device is locked, it disconnects from the WiFi network, so when the device is unlocked again, it has to reconnect. Part of that reconnection process is determining if there is Internet connection (which there isn't). Until the process is not finished iOS does not allow any DNS resolution (so if I write http://10.0.0.1 it will go there, but not if I request http://something.local.com).
Since we control that network, we want to know how to does iOS verifies Internet connectivity so that we can fake the responses it expects.
I don't know if it's possible to resolve DNS without an internet connection on iOS, but if that's the case, that would be a way better solution since you don't need to mess with your router settings. Use my solution only if it really isn't possible with only code.
I'll suggest you to follow this guide: http://blog.jerodsanto.net/2009/06/sniff-your-iphones-network-traffic to check which actions your iPhone executes to detect an internet connection.
Using this information you could forward the is-there-internet-requests on your router to a local server which fakes the there-is-internet-responses.
This assumes Apple really uses an external server to detect this, which I'm not sure about. But it wouldn't hurt to give it a try!
Have you looked at the Reachability Class? You don't have to use the reachabilityForInternetConnection method, which checks if a default route is available. You can use the reachabilityWithAddress: method and check if your server is reachable.

How to specify that WiFi or 3G is required?

First of all, I know I can use UIRequiresPersistentWiFi to specify that my app needs WiFi.
But in fact, my app works fine with both WiFi or 3G. How can I express such a network requirement? It there any Info.plist key for this? Or is testing myself with Reachability the only way to go?
What exactly are you trying to do? If all you need is network access, then you don't have to do anything at all to make it work. Reachability can be useful, because it lets you quickly test if the network is available before attempting to connect anywhere, but that's only useful if you need to know if there is a network before even attempting to use it. Under normal circumstances, you can just attempt to use the network, and gracefully handle any errors that may occur.
My suggestion would be use SCNetworkReachabilitySetCallback (which gets called whenever the status of the network changes) and then throw an error if there's no WiFi or 3G connection.
b.t.w., all the keys UIKit supports in Info.plist can be seen at http://developer.apple.com/library/ios/#documentation/general/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html

Detecting Whether an iPhone has roamed from wifi to 3G or vice versa

G'day Guys,
I've been using the reachability API with reachability status callbacks to determine whether an application is connected over 3G or wifi. It's an application that acts as a voice extension for an existing piece of hardware and as such we're using the VoIP APIs to run in the background and accept calls etc.
Is there a definitive way other than using reachability status callbacks to determine whether you can access a particular IP endpoint or not? I could use an ASIHTTPRequest and then check if it timed out but that may cause potential problems for me in the long run.
I'm not looking for a programmatical answer but more any insights other developers would have on how to manage a roaming between the two in the background if you have a persistent connection. Basically if the device roams over to 3G I need to destroy the session on the device and if it roams back over to Wifi I need to recreate the session.
Any feedback or advice would be welcome.
The Reachability APIs will provide the connection change notifications to your app so that you can know when the connectivity changed from WWAN to wifi. It will not tell you if you've changed from Edge to 3G or LTE unfortunately. The Reachability API also has methods to test reachability to a specific host. So, in your app you can listen for the notifications that the connection method has changed, then when it does change test reachability to your target host and at that time make the decision whether to rebuild the session or leave it intact.

Resources