Checking if Cellular Data has been turned off - ios

So I've done a lot of research and I haven't quite found the answer I have been looking for...
I want to make it so that, if cellular data is turned off for my app in particular, the app doesn't work or at least make it so a few buttons are hidden.
I have reachability put in place so that if there is no internet connection, the app does not run. However, if the user is connected to the internet but has cellular data turned off for my app, and they are not connected to wifi, then the app runs (which I do not want it to do).
Any help would be highly appreciated!

I had a similar problem and found this solution. Hopefully it helps.
func isCellularRestricted() {
let cellState = CTCellularData.init()
cellState.cellularDataRestrictionDidUpdateNotifier = { (dataRestrictedState) in
if dataRestrictedState == CTCellularDataRestrictedState.restricted { // State has changed
// your app doesn't have cellular data
} else if dataRestrictedState == CTCellularDataRestrictedState.notRestricted {
// your app does have cellular data
}
}
}
This is the function I implemented in my app delegate (called from didFinishLaunchingWithOptions). It gets called everytime user changes cellular data for your app and only for your app. If user allows cellular data for your app and has turned off cellular data in iOS, this will still return notRestricted.
In my case I made a constant which I set to true/false, depending if cellular data is allowed.
I found the answer somewhere on SO but I cannot find the link.
EDIT (found link): How do I know if cellular access for my iOS app is disabled?

Related

Is there any swift function call right before user turns off his internet?

Is there any event right before user turns off his internet? Like we have applicationWillTerminate.I have to call an API to update sever , internet is going off.
You can use NWPathMonitor like this - requires iOS 12
import Network
let monitor = NWPathMonitor()
monitor.start(queue: DispatchQueue(label: "NetworkMonitor"))
monitor.pathUpdateHandler = { (path) in
if path.status == .satisfied {
print("Connected")
} else {
print("Not Connected")
}
}
This allows more granular control as well .cellular, .wifi & .wiredEthernet etc. -
let cellMonitor = NWPathMonitor(requiredInterfaceType: .cellular)
If you still support iOS 11, you can use a 3rd party library like - Reachability
The answer is no. You can't detect the connection to the Internet is going until it has gone. If you think about it, the usual reasons for losing the Internet connection are things like routers going down or moving out of cellphone coverage. There's no way you can predict that.
The best you can do is detect that the network has gone down and try to mitigate the effects e.g. if you were trying to send something to a server, persist the data locally and resend it when the connection comes back. If you want the server to know it has lost contact with the device, you can ping it regularly, say every minute and if it doesn't hear from your device after a minute, it will know you have lost your connection.

iOS 10 - WLAN Access Setting Doesn't Appear In Some iOS Devices

Our app is using WLAN to communicate with a wireless device. When our app is installed in iOS 10. Sometimes, udp socket doesn't work. The reason for that is, in iOS 10 they added a new setting or permission under your app that allows the user to switch on or off the user of WLAN or cellular data.
The following would appear in the settings of the app:
When I tap on the Wireless... It will bring me to this UI:
After allowing WLAN use. The app would work fine.
Now, the problem is, sometimes, or in some devices running iOS 10, the settings that I just showed you doesn't appear(I am referring to the setting shown on the first image). So, is there anything I can do to make that settings always appear? It seems that sometimes iOS system doesn't recognize that my app is using wireless data. And it would result in my app would never get to use WLAN forever.
There is no user permission to use WIFI in iOS10.
The application in your screenshot (BSW SMART KIT) is using Wireless Accessories (WAC), i.e. is able to connect to wireless speakers. To accomplish this the Wireless Accessory Configuration capability is required. This is what you can dis/enable in the systems settings (your screenshot).
The switch in the settings shows up after connecting to a device via WIFI through WAC. You can see this behaviour in your sample app (BSW SMART KIT) too.
This sample code might let you get the idea. Detailed information in Apples documentation.
After a time of researching. I ended up seeking help with Apple Code Level Support. Apple states that this problem would most probably occur when you reskin your app. They say that probably it's because of the Image UUID of the main app and the reskinned app are the same. When you install both of them in your phone, the system will treat the reskinned app as the same app compared to the main app. So, if the one app fails to access WLAN, then it will also affect the other one. According to them, this appears to be a bug in iOS. And currently, they don't have any solution for the developers. This is the radar bug number:
What I did to somehow lessen the occurrence of the problem is to add tracking to the Network Restriction by using the following code.
- (void)startCheckingNetworkRestriction
{
__weak AppDelegate *weakSelf = self;
_monitor = [[CTCellularData alloc] init];
_monitor.cellularDataRestrictionDidUpdateNotifier = ^(CTCellularDataRestrictedState state)
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^
{
NSString * statusStr;
switch(state)
{
case kCTCellularDataRestrictedStateUnknown:
{
statusStr = #"restriction status:Unknown";
}
break;
case kCTCellularDataRestricted:
{
statusStr = #"restriction status:restricted";
[weakSelf performUrlSession];
}
break;
case kCTCellularDataNotRestricted:
{
statusStr = #"restriction status:not restricted";
}
break;
default:
{
abort();
}
break;
}
NSLog(#"Restriction state: %#", statusStr);
}];
};
}
Take note that you have to import CoreTelephony to do this.
#import CoreTelephony;
when I detect that the network is restricted. I will open a URL session to force internet access attempt and would hope that the restriction alert dialog would pop out. Once the alert is pop out, then the WLAN Access Settings that I was talking about would definitely appear under the settings. There are times that this doesn't work. If it happens, then you'll just have to rename the bundle ID, and make a couple of changes to your code and then rebuild it a couple of times (Well, that's what I did when I was experimenting this). Reinstalling the app won't do a thing. Restarting and resetting the phone won't do either.
Hope this helps.

Is it possible to detect the user's moving activity on the background?

I want to develop an app that detecting the user's moving way (walking, cycling, driving etc...) and send a specific UILocalNotification for each activity type.
My question is: is it possible to detect it on the background (when the app is completely closed) without draining the device's battery? What will be the best way to do it?
Thank you!
There is coprocessor m7(+) in iPhones upper 5s.
It gives you possibility to get device motion.
Just
import CoreMotion
in your file.
Create a CMMotionActivityManager object:
let motionActivityManager = CMMotionActivityManager()
Check if it`s available on your device:
motionActivityManager.isActivityAvailable()
Use this method:
motionActivityManager.startActivityUpdates(to: OperationQueue.main) { (activity) in
if (activity?.automotive)! {
print("User using car")
}
if (activity?.cycling)! {
print("User is cycling")
}
if (activity?.running)! {
print("User is running")
}
if (activity?.walking)! {
print("User is walking")
}
if (activity?.stationary)! {
print("User is standing")
}
if (activity?.unknown)! {
print("Unknown activity")
}
}
It would return you types of user activity.
Regarding the user activity which can be handled in background tasks are the below once which does not mention about (walking, cycling,driving etc...)
Implementing Long-Running Background Tasks
For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background:
Apps that play audible content to the user while in the background,
such as a music player app
Apps that record audio content while in the background.
Apps that keep users informed of their location at all times, such as
a navigation app Apps that support Voice over Internet Protocol
(VoIP)
Apps that need to download and process new content regularly
Apps that receive regular updates from external accessories
Yes it´s possible to do that!
If your iOS app must keep monitoring location even while it’s in the
background, use the standard location service and specify the location
value of the UIBackgroundModes key to continue running in the
background and receiving location updates. (In this situation, you
should also make sure the location manager’s
pausesLocationUpdatesAutomatically property is set to YES to help
conserve power.) Examples of apps that might need this type of
location updating are fitness or turn-by-turn navigation apps.
Read more here.

How to programmatically connect to a WiFi network given the SSID and password

I am trying to replicate an existing Android Application that I made to iOS. In my application, I should be able to connect to a WiFi network given the SSID and the Password. The SSID and the Password can be hardcoded or can be inputted by the user. I was going through the internet to research how this can be done on iOS, however, it seems that this behavior is highly discouraged and that there's no way on doing this using public/documented libraries that won't get your application rejected by the App Store.
The thing is I'll be using this application personally and I don't plan on submitting it to the App Store so it's okay if I use external libraries. If my friends would want to use it, I could export an enterprise ipa and give them instructions on how to install it.
Upon searching, it seemed that MobileWifi.framework was a good candidate. However, it does not seem that there's a straightforward way of using this library to connect to a WiFi network given the SSID and the Password.
Is there anyone who has successfully tried to connect to a Wifi Network given the SSID and Password?
With iOS 11, Apple provided public API you can use to programmatically join a WiFi network without leaving your app.
The class you’ll need to use is called NEHotspotConfiguration.
To use it, you need to enable the Hotspot capability in your App Capabilities (Adding Capabilities). Quick working example :
NEHotspotConfiguration *configuration = [[NEHotspotConfiguration
alloc] initWithSSID:#“SSID-Name”];
configuration.joinOnce = YES;
[[NEHotspotConfigurationManager sharedManager] applyConfiguration:configuration completionHandler:nil];
This will prompt the user to join the “SSID-Name” WiFi network. It will stay connected to the WiFi until the user leaves the app.
This doesn't work with the simulator you need to run this code with an actual device to make it work.
More informations here :
https://developer.apple.com/documentation/networkextension/nehotspotconfiguration
connect wifi networks in iOS 11. You can connect wifi using ssid and password like following.
Enable Hotspot on App Id configure services
After Enable Hotspot Configuration
Swift 4.0 Code for iOS 11 Only:
import NetworkExtension
...
let configuration = NEHotspotConfiguration.init(ssid: "SSIDname", passphrase: "Password", isWEP: false)
configuration.joinOnce = true
NEHotspotConfigurationManager.shared.apply(configuration) { (error) in
if error != nil {
if error?.localizedDescription == "already associated."
{
print("Connected")
}
else{
print("No Connected")
}
}
else {
print("Connected")
}
}
Contrary to what you see here and other places, you can do it. It's hard to make pretty enough for normal users, but if doesn't have to be then it's really easy to code. It's an enterprise admin thing, but anyone can do it. Look up "Connection Profiles." Comcast does this in their iOS app to setup their hotspots for you, for example.
Basically, it's an XML document that you get the device to ingest via Safari or Mail. There's a lot of tricks to it and the user experience isn't great, but I can confirm that it works in iOS 10 and it doesn't require jailbreaking. You use Configurator to generate the XML, and I suggest this thread for serving it (but people don't find it because it's not specifically about WiFi), and this blog for querying if it's installed which is surprisingly nontrivial but does indeed work.
I've answered this question many times, but either don't have enough rep or am too dumb to figure out how to close questions as duplicate so better answers can be found more easily.
UPDATE: iOS 11 provides APIs for this functionality directly (finally!). See NEHotspotConfiguration. Our Xamarin C# code now looks like:
var config = new NEHotspotConfiguration(Ssid, Pw, false);
config.JoinOnce = false;
Short answer, no.
Long answer :)
This question was asked many times:
Connect to WiFi programmatically in ios
connect to a specific wifi programmatically in ios with a given SSID and Password
Where do I find iOS Obj-C code to scan and connect to wifi (private API)
Programmatically auto-connect to WiFi iOS
The most interesting answer seems to be in the first link which points to a GitHub project: wifiAssociate. However someones in the third link explains that this doesn't work anymore with iOS8 so you might have hard time getting it running.
Furthermore the iDevice must be jailbroken.
Make sure both Network Extensions & Hotspot Configuration are turned on in Capabilities.
let wiFiConfig = NEHotspotConfiguration(ssid: YourSSID,
passphrase: YourPassword, isWEP: false)
wiFiConfig.joinOnce = false /*set to 'true' if you only want to join
the network while the user is within the
app, then have it disconnect when user
leaves your app*/
NEHotspotConfigurationManager.shared.apply(wiFiConfig) { error in
if error != nil {
//an error occurred
print(error?.localizedDescription)
}
else {
//success
}
}
my two cents:
if you got:
Undefined symbols for architecture i386:
"_OBJC_CLASS_$_NEHotspotConfigurationManager", referenced from:
objc-class-ref in WiFiViewController.o "_OBJC_CLASS_$_NEHotspotConfiguration", referenced from:
objc-class-ref in WiFiViewController.o ld: symbol(s) not found for architecture i386
simply it does NOT work under simulator.
on real device it does compile.
so use:
class func startHotspotHelperStuff(){
if TARGET_IPHONE_SIMULATOR == 0 {
if #available(iOS 11.0, *) {
let configuration = NEHotspotConfiguration(ssid: "ss")
configuration.joinOnce = true
NEHotspotConfigurationManager.shared.apply(configuration, completionHandler: { (err: Error?) in
print(err)
})
} else {
// Fallback on earlier versions
}
}// if TARGET_IPHONE_SIMULATOR == 0
}
We can programmatically connect wifi networks after IOS 11. You can connect wifi using ssid and password like following.
Swift
var configuration = NEHotspotConfiguration.init(ssid: "wifi name", passphrase: "wifi password", isWEP: false)
configuration.joinOnce = true
NEHotspotConfigurationManager.shared.apply(configuration) { (error) in
if error != nil {
//an error accured
print(error?.localizedDescription)
}
else {
//success
}
}
You can connect to wifi using Xcode 9 and swift 4
let WiFiConfig = NEHotspotConfiguration(ssid: "Mayur1",
passphrase: "123456789",
isWEP: false)
WiFiConfig.joinOnce = false
NEHotspotConfigurationManager.shared.apply(WiFiConfig) { error in
// Handle error or success
print(error?.localizedDescription)
}
I'm not sure if this will help anyone but there IS a Wifi QR code that Apple put into iOS since version 9 if I'm not mistaken. It's been there a while.
You can go here: https://qifi.org
Put your SSID and password in. Print out the QR code or just keep it on a screen. Point the iPhone at the QR code using the camera app. You will get a notification bar that if you press will setup your WIFI connection on the fly.
This 'should' work with Android but I haven't test it yet. It DOES work with iOS.
The nice thing with this solution is you can make your WIFI base 'hidden' on the network and folks can still connect to it which is very desirable in a high traffic area for security reasons.

How to tell if the user turned off cellular data for my app?

Although my app is usable without any internet connection, it may exchange data with a web server (in order to show some user statistics). So I advertise the app as "needs no internet connection". Some users subsequently have turned off cellular data for my app, which should be completely fine. But when my app tries to exchange data, these users are bugged with the "Cellular data is turned off for [App Name]." dialog.
This is an annoyance to them and I want to prevent these dialogs and simply skip the whole data exchange thing.
There is Apple's Reachability Sample Code.
But although I turned off WiFi for the whole device and cellular data for the app, Reachability confirms a positive internet connection. To be more specific, it reports
Reachability Flag Status: WR t------ networkStatusForFlags
no matter whether I activated cellular data or not. Of course, when cellular data is turned off, no internet connection is actually available, so the data exchange fails. But the user is presented with the cellular data dialog anyway.
Is there any way to detect whether a internet connection is available on iOS 7 and iOS 8, taking into account the cellular data setting for a specific app – all without bugging the user every time again with the cellular data dialog?
My app currently comes without any settings panel, so I want to avoid setting up a (second, in-app) switch "don't use cellular data". Also, I don't want to restrict data exchange to a WiFi connection since it's just a 2 KB of data per session which isn't a big thing for most users.
I think the only supported way in iOS8 is to send a Ping to a known server and bug the user with the alert panel a few times. On iOS8, Apple displays the panel only twice, then skips it even if the app is restarted, maybe it will show up a day later again. (This is really bad news for ad-supported apps.)
Apple says (https://devforums.apple.com/message/1059332#1059332):
Another developer wrote in to DTS and thus I had a chance to
investigate this in depth. Alas, the news is much as I expected:
there is no supported way to detect that your app is in this state.
Nor is there a way to make a "no user interaction" network connection,
that is, request that the connection fail rather than present UI like
this.
The following articles suggest ways to use ping:
http://www.splinter.com.au/how-to-ping-a-server-in-objective-c-iphone/
http://elbsolutions.com/projects/reachability-with-simpleping-wrapper/
Try using this git project.
How to install you can see inside the Readme on git.
I also used dispatch_once to be sure the Reachability will only be initialized once. This dispatch type is sometimes very useful!
Define variable in class
BOOL _online = NO;
Initialize the variable
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
Reachability *reach =
[Reachability reachabilityWithHostname:gameApiHost];
reach.reachableBlock = ^(Reachability*reach) {
NSLog(#"REACHABLE!"); _online = YES;
};
reach.unreachableBlock = ^(Reachability*reach) {
NSLog(#"UNREACHABLE!"); _online = NO;
};
[reach startNotifier];
});

Resources