is it possible to turn off wifi or switch iPhone to offline mode in codes in swift 4? - ios

I want to know is there any way to turn on or off the iPhone wifi or switch the device to offline mode in codes in swift 4 or not
I know that apple may not allow the app can do such things to be distributed in App Store But it's Not important I just want to know is there any codes to do that or not (Just in swift 4)

5 Solutions Collect From Internet About “How to turn off internet connection, bluetooth and WIFI programmatically?”
Ans
You can’t. Apple does not allow 3rd party apps to change global system settings like that.
There is no API available to control cellular data,wi-fi,bluetooth within in an app , User have to go to settings to enable or disable cellular data,wi-fi and bluetooth.
This is not possible in iOS unless you jailbroke your device. Apple is not allowing any apps developer to access wifi/bluetooth. You can only check wifi/tooth is connected or not.
You can’t do that using the iOS application. Apple not allowing it.
Legally there is no way to do it. Even somehow if you are manage to do it, Apple will reject your app while submitting to AppStore.
update
I am not sure the following code will work or not, try once, initially add the header file to your project folder and create the bridging header to your code ,then use the below code where you need , for ref purpose I taken the answer from here
var tempSBWifiManager: SBWiFiManager = objc_getClass("SBWiFiManager")
// Steal a class from SpringBoard
tempSBWifiManager.sharedInstance().isWiFiEnabled = false

Related

How to read files of External Mass Storage Devices Without DocumentPicker? (iOS)

Just to give some context, there is an iOS app I'm building (in Xamarin) that requires the ability to fetch files (in an automatic way without having the user to navigate to the files and select them manually) that are stored on External Storage Devices (USB Sticks), and are connected (paired) to an iPhone/iPad.
Users connect a lighting cable to the iOS Devices, and plug their USB Sticks into this lighting cable. Here is an example of the cable that end users use to pair the USB Sticks with their iPhone/iPad, and the app then auto fetches these files from the USB Storage Devices.
The app then performs 2 functions:
It listens to notifications, for when a usb stick is paired with the iOS Device.
When it receives this notification, it then proceeds with querying the files on the USB Stick, and reads and processes the files. The app reads these files automatically and the user does not need to manually select these files
I've tried using External Accessory Framework, however that's only suitable for devices that you register with the MFi program.
https://developer.apple.com/documentation/externalaccessory
Notifications Center never seems to work (the Notifications that handle when a Device gets Connected and Disconnected, the delegates never get called), and I've tried using the Microsoft Helpers.
https://learn.microsoft.com/en-us/dotnet/api/externalaccessory.eaaccessorymanager.notifications.observedidconnect?view=xamarin-ios-sdk-12
I've also tried some 3rd party libraries, but haven't found anything useful.
It doesn't look Apple has any Api Available to auto query and read the files, without having to use a DocumentPickerController. I know this is because of the App Sandbox, and I cannot directly access the Removable Storage Devices.
Now for my questions:
Are there any 3rd party libraries anybody can recommend, that can help achieve most of the heavy lifting for this task? I'd prefer a library that's compatible with Xamarin, however if it's a native library (Swift or Objective-C Library, Cocoapods) I'm sure there is a binding I can use on nuget.
Does anybody have any snippets, or documentation, or can point me in the right direction here (Please feel free to post Swift, or Objective-c solutions here if you like)? Where should I look, which Apple Framework (iOS SDK) is most suitable to deal with this situation. And to summarise, is what I'm asking for at all possible on iOS, without having to jailbreak, or get around the App Sandbox?
Update
So I've tried the solution that #Saamer suggested:
Detect if USB is connected to iPhone device
Here is an example I wrote just to verify if the callback gets invoked, and the app can detect if a usb is plugged in.
CFNotificationCenter.Darwin.AddObserver("com.apple.mobile.lockdown.host_attached", null, (e, s) =>
{
MainThread.BeginInvokeOnMainThread(async () =>
{
var picker = await Xamarin.Essentials.FilePicker.PickAsync();
});
}, CFNotificationSuspensionBehavior.DeliverImmediately);
A file picker should immediately get invoked, and open up, once I plug the USB in, however this doesn't happen.
I'm happy for a solution right now which opens up a file picker, when the user plugs in a USB Device, and navigates to the root directory to start off with. So when the File Picker opens up the user should see this. Then they can select the files they want to transfer into the app.
I thought you needed to jailbreak but it seems unlikely, a solution is possible for <iOS 12
CFNotificationCenter's AddObserver seems to be usable for detecting when a device is connected using host attached and detached as shown here
Or if you don’t submit it to the App Store, you can use this
According to the Apple Docs
You can use the Files app and other supported apps to access files stored on external devices, such as USB drives and SD cards, connected to your iPhone.
Essentially you have to make your a "supported app". I have downloaded a free app called "Clockology" that I recommend downloading and playing with, which allows users to see data within the app as you can see below:
You generally use UIDocumentPickerViewController along with the right uttype to get the files from the Files apps or iCloud Drive, and you require activating the Key-value storage and iCloud Documents from iCloud entitlements capability. There's a lot of tutorials on UIDocumentPickerViewController usage, but you specifically want the capabilities that became available from iOS 13 onwards
This video from WWDC is the best example of getting to where you want. I also didn't find any 3rd party libraries that can help with this
——————-
Edit: If the app does not need to be distributed through TestFlight or the App Store, you can use IOKit and distribute through AppCenter, as long as you have the UDIDs of all the devices you need the app installed on (up to 100?)

Modify the BLE device name

I am working on an iPhone app and this app send command to a board through BLE module. the board controls some motor to move around.
I do code on iOS app and another guy do the code on Arduino. There is a BLUGiga 112 module on the board.
Now, in my iOS app, I can search out available BLE devices, but the detected device name is not always what I want.
The device name defined in gatt.xml file can always be detected on the iOS app. But I want user to be able to change the name by iOS app while the name in xml is static. So I let another hardware engineer write some code to accept new name from iOS and store in flush and then broadcast back to iPhone. I can pass the name from iOS app and the name is stored in device successfully. But next time, when I open iOS app and try to search around, the new name is not always be detected.
Anybody can tell me why? or what should I do to get the rename function done?
iOS prevents that Service from being usable by a developer. It also caches that data and it is hard to clear that cache.
I ended up making a custom Characteristic that an app could write to. The firmware would handle that write request and then update the device name and advertising packet. It's seems hacky, but I wasn't able to find a better solution.
Section 3.12 (page 24) documents the fact iOS does not let a developer use that Service (and a couple others) https://developer.apple.com/hardwaredrivers/BluetoothDesignGuidelines.pdf

Is there a way to access and modify iOS settings with Swift?

Are there any classes or frameworks out there that allow my app to access and modify iPhone settings of the device used to visit my app? I know there's plenty of apps out there which (for example) can change the screen brightness of the device, so I was wondering if it's also possible to modify other settings you can manage in the Settings app on your device (i.e. automatic device lock, time and date, etc.).
No this can't be done... What you can do is to open the settings and that's it. Apple does not allow you to change anything outside your application.

How Apple TV is used to control Homekit Accesory

I have created an application for Homekit which works fine when both accessory and device is in same network.
I read somewhere that Apple TV is required to control accessories remotely.
Can anybody please tell how to achieve this exactly?
No you can not pair homekit with the appleTV. You can use the remote app either from you iPhone or iPad
If you have Apple TV try to set it up.
I have given the details.
I have done a much workaround for this. First of all handling things by our own is not preferable for multiple users.
I had this query that how all configuration done on one device will reflect on another device for same home. Unfortunately apple didn't provided much information on this and they also didn't clarify that Apple TV will require or not(As I have referred to some reports that says Apple TV will use as intermediator between iOS devices.
Now at application side i have tried to add User to home. There is a HMUser class and we can add user to any home that we want to sync. I was running my app in iOS 8.1 in both the devices, I was also logged in to icloud and keychain access was also enabled.
Now i have tried to add user using method of HMHome class
-addUserWithCompletionHandler()
I shows a popup in which we need to enter the email id of user to which we want to give access to current home.
But it didn't work. I always get error "notification not enable at remote side"
I think that login to Apple TV is require for adding user. I am not sure about this, Apple hasn't clarify on this too. I don't have Apple TV if you have try to login in it and use two other iOS devices to give access to any icloud account.
Also revert me if it works.
I have been working on HAP Over iCloud concept since last 2 to 3 hours. The beauty in this concept is according to the documentation we should need the following things.
1. Apple tv 3rd generation.
2. Have to use same iCloud account in both apple tv and iOS device.
3. This is most important that Apple tv and Accessory should be on same network.
Eventhough you are on different network it is able to pass the switch on/off,brightness increase/decrease ... actions. For this you can use siri commands or programmatically call the accessory write value functionality
Note : As I know I think when you are passing the commands to the Accessory it checks for the iCloud account in the apple tv and from there it is passing the commands to the Accessory.

Accessing Settings app values and opening it if needed

I have seen that there are known apps, such as Twitter and Facebook, that display a "Turn Off Airplane Mode or Use Wi-Fi to Access Data" message within an alert view, with a button that switches to the Settings app, when no network is detected as the app goes foreground. This message is the same in all apps where I saw it, is this alert view a kind of predefined one that you can use? Similar to the one displayed when checking locationServicesEnabled...
I found some posts dealing with this issue some time ago, for example:
iOS UIAlertView button to go to Setting App, and it seems (or seemed) to be a way to read the values in the iOS' Settings app, but I couldn't find any of this in the Apple Developer's documentation... is there any public API for accessing those values? Would an app be rejected if accessing them as in the post I linked?
Thanks in advance
Till iOS 5.0, you can use the URL scheme to open the Settings App from third-party apps using the URLScheme like:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"prefs:root=WIFI"]];
Unfortunately, iOS 5.1 and iOS 6 not supports this feature.
You can use the Reachability for checking the Wi-Fi state.
For displaying the default alertview when the Wi-fi is off or in AirPlane mode you can use the Application uses Wi-Fi flag in the info.plist
Refer InfoPlistKeyReference for details:
UIRequiresPersistentWiFi
“Application uses Wi-Fi”
Specifies whether this app requires a Wi-Fi connection.

Resources