How to read files of External Mass Storage Devices Without DocumentPicker? (iOS) - 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?)

Related

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

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

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

How to access all the files(pdf, doc etc) in iOS?

How can I find out programmatically all the files present in iOS device and list them out to user? Also I need to download the file in my local application database?
How this can be achieved with Objective-C.
You cannot access all files in an iOS device outside the sandbox of the app. But there are other ways you can achieve this. What you can do is:
Access all the files in Gallery using UIImagePickerController.
Access all the user files in network- Dropbox/Google drive/iCloud. They all have separate public APIs that can be downloaded and used to download/upload files in network.
In this way, you can access most of the user files and use the same in your application.
Simple answer : you can't (on a non-jailbroken device).
Each app is launched is in own sandbox and can't interact with the others apps, expect using API provided by Apple for app-communication (ex: extension)
You should look at that Apple guide about App Sandboxing, which is valid for OSX and iOS apps.
The main difference between these two OS, as quoted directly from the doc :
iOS Note: Because it is not for user documents, an OS X container
differs from an iOS container—which, in iOS, is the one and only
location for user documents. In addition, an iOS container contains
the app itself. This is not so in OS X.
You cannot access all the files outside the app sandbox. But, if they are available at some shared location(network), you can use third party libraries to access them. For eg.
1. For iCloud- iOS provides inbuilt framework of UIDocument
2. For dropbox- DropBoxAPI is available
3. For Google drive- there is a separate library to access files in the drive.
Hope this helps :)

How to transfer a file created in an ios app to the mac

i'm starting the implementation of a feature for transfer some files (lets say csv files) created in an ios app to the mac.
But i've made some reading about the subject without anything conclusive.
So, can anyone point me a direction to follow in order to achieve my goal ? I need, somehow to access the files created on my app throw the mac
ITunes file sharing.
iCloud.
Network transfer. (This third party library works with iOS and OSX)

Does iOS have any equivalents to DLLs/IPC/process spawning?

Are separate apps able to share the same binary in a form like a DLL? I know its possible to compile static libraries but I wouldn't count these as the same as a DLL (i.e. a dll is one copy of a binary shared by multiple apps, whereas static libraries are separately included by any using app).
Is each app totally separate from each other, are there any IPC or file sharing mechanisms available for differing apps to communicate and share data?
Is it possible for an app to create a new process in addition to a new thread (I guess not)?
You can't share executable files between applications. Apple requires that all apps function standalone. However you can use a UIDocumentInteractionController to get another program to deal with files you don't understand, and a 'quick view' may be available. That's how Mail works, for example.
Programs from the same vendor can share the keychain and, I think, iCloud storage as of iOS 5, but can't share storage on disk. As they can declare supported file types, UIDocumentInteractionController can be used to push temporary access to a file from one app to another. A custom URL scheme can be used in a similar way to pass fragments of data if that helps.
As a general rule, only one user process may be active at once in iOS - e.g. background processing is essentially event based. So you can't create a second process for yourself.
You can do this if you are developing for a jailbroken phone. Not
otherwise.On jailbroken phones, you can create .dylibs or shared libraries that can be
loaded via the DYLD_INSERT_LIBRARIES environment var (much like on
MacOS)
Apps are sandboxed. However, there are some ways of communicating between apps. You can use
(a) customURL scheme (also mentioned by Tommy above)
(if any) associated with an app to launch an app and send some
parameters to the launching app
(b) If you control communicating
apps, then you can use Message ports (CFMessagePortCreateRemote)
(c) If you control communicating apps, you could use Darwin
NOtification center for distributed apps.
Of course the expectation for (b) and (c) is that the
communicating apps are all running. On iOS since there is only one foreground
process, you'd have to have the other as a background app and that's
restricted to certain kinds of apps on the iOS platform.
Basically, no to all of the above, unless you're targeting jailbroken phones and are bypassing Apple's App Store. If you jailbreak, I believe you have similar options to those you have in Mac OS X.

Resources