Communicating with your iOS app over USB (C# and/or Xamarin)
So, you're writing an iOS app that needs to communicate with a desktop client. A quick google search would suggest that the only way to do this would be to create a small socket server on the desktop client that listens on a given port, and have the iOS app connect to the desktop client through your local network. This definitely works, and often times it's good enough. If you're familiar with socket programming, this is definitely straight forward. However, there are definitely some drawbacks. The app has to be on the same network as the client (which isn't always possible in enterprise environments where your computer is on some private wired network). The client opens a port to your computer that anyone on the network can access, which can be a security concern. Initial setup is also a bit more tedious as the app user must enter the IP Address and Port into the app in order to connect. Being able to achieve "iOS App to Desktop" communication via USB cable would solve all of these problems, and make (in my opinion) the app much more convenient.
The problem with this approach is just how little documentation there is regarding how this can be achieved. Some forum posts say it's flat out not possible. Others will say that you need to sign up for Apple's MFi program (a long and tedious process). Well, after spending the last week scouring the internet, I'm here to tell you that it definitely is possible without MFi. And it's not even all that hard once you understand what's happening. So, here's everything I've learned.
A couple quick notes
I'm primarily a C#/.NET developer, and so I'm writing this assuming you are too. The concepts are pretty universal, but the examples will be in C#. I'm using Xamarin.iOS to build the iOS app, so even the iOS code will be in C#.
Also, I think it's good to note that for developers writing Swift/Objective-C code, there is a pretty awesome platform tool called Peertalk that does everything I'm going to talk about. Unfortunately, there's no Xamarin binding. I almost decided to switch off Xamarin to use this before I finally figured out how to do what Peertalk does very quickly and easily.
Also, I think it's good to note that for developers writing Swift/Objective-C code, there is a pretty awesome platform tool called Peertalk that does everything I'm going to talk about. Unfortunately, there's no Xamarin binding. I almost decided to switch off Xamarin to use this before I finally figured out how to do what Peertalk does very quickly and easily.
How does Apple do it?
When you plug in your iOS device to your computer, you're able to sync your data via the USB cable. The way Apple achieves this is via a very handy program called usbmuxd. This is a program that's running on your computer, waiting for an iOS device to connect. If you've installed iTunes on your machine, you've also installed this program. On Windows, the service that hosts this program is named "Apple Mobile Device Service", and can be seen in services.msc. So, what does usbmuxd do?
usbmuxd
usbmuxd stands for USB Multiplexing Daemon. If you're anything like me, "USB" is the only really familiar term in that name. But what this program does is actually pretty simple (and extremely useful to us). It's a background process (daemon) that can tell your iOS device to map (multiplex) all the data received from the USB cable to a network port on the iOS device, and return a socket connection to that port.
So, practically, what does this mean for us? This means that, if you do something like this on the iOS device
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Bind(new IPEndPoint(IPAddress.Loopback, 5050)); socket.Listen(100); socket.BeginAccept((ar) => { var connectionAttempt = (Socket) ar.AsyncState; var connectedSocket = connectionAttempt.EndAccept(ar); // Do something with "connectedSocket" }, socket);
The above code (which is running on your iOS device) is now listening for an incoming connection on port 5050 (which I chose completely arbitrarily). I know what you may be thinking. "I thought we weren't going to be using network connections. What gives!?"
This is where usbmuxd does its magic. From your desktop app, you can send a message to usbmuxd that informs the connected iOS device to receive all USB data on a network port. In our case, we'd want to have usbmuxd tell our iOS device to receive USB data on port 5050. usbmuxd will oblige, and give you a socket handle to send data on and receive data from. At this point, your iOS app will call the "BeginAccept" callback, and you'll have a network socket that's not really a "network" socket. This will work even if your computer and iOS device are on different networks, or no networks at all. All data is going through your USB cable!
So...how do I actually communicate with usbmuxd
Fortunately, there's a nuget package for that! The iMobileDevice.Net nuget page can be found here. It's not exactly the most well documented tool, but it works very well. It does assume that the usbmuxd service is running on your machine. Easiest way to do that is to just make sure you have iTunes installed. If you don't want to have a dependency on iTunes, you can do what the people at Duet Display did, and extract the appropriate msi from the iTunes installer, and just require that as a prerequisite of your application
The AppleApplicationSupport64.msi AppleMobileDeviceSupport6464.msi installer will install the service you need.
Once you know that usbmuxd is actually on your machine, you can start using the nuget package. This package is really just a wrapper around a C library, and it shows. There are a lot of IntPtr's and out parameters, so it's not very pretty. But it works.
Here's a snippet from a project I'm working on. The below code listens for an iOS device event (which usbmuxd calls for us). When the event is a "DeviceAdd" event, I attempt to connect to it.
public void BeginListening() { _iDeviceApi.idevice_event_subscribe(_eventCallback, new IntPtr()); } private iDeviceEventCallBack EventCallback() { return (ref iDeviceEvent devEvent, IntPtr data) => { switch (devEvent.@event) { case iDeviceEventType.DeviceAdd: Connect(devEvent.udidString); break; case iDeviceEventType.DeviceRemove: break; default: return; } }; }The Connect method gets a "DeviceHandle" reference, using the udid of the connected device. Once it gets that handle, it calls the "ReceiveDataFromDevice" method.
private void Connect(string newUdid) { _iDeviceApi.idevice_new(out iDeviceHandle deviceHandle, newUdid).ThrowOnError(); var error =_iDeviceApi.idevice_connect(deviceHandle, 5050, out iDeviceConnectionHandle connection); if (error != iDeviceError.Success) return; ReceiveDataFromDevice(connection); } private void ReceiveDataFromDevice(iDeviceConnectionHandle connection) { Task.Run(() => { while (true) { uint receivedBytes = 0; _iDeviceApi.idevice_connection_receive(connection, _inboxBuffer, (uint)_inboxBuffer.Length, ref receivedBytes); if (receivedBytes <= 0) continue;
// Do something with your received bytes } }); }And finally, the below line sends data to the iOS device.
_iDeviceApi.idevice_connection_send(connection, bytesPending, (uint)bytesPending.Length, ref sentBytes);There are a lot of other functions available to you, but these are some important ones. Using these, you can start sending data to your iOS device via USB. Your iOS app won't even know that it's a USB connection. To the app, it's just another TCP Network Connection. Hopefully this saves you some time, and feel free to comment or message me if you have any questions.
Happy Coding!
Hi Carlos,
ReplyDeletei'm trying to follow your example and use the library from nuget but ad soon as i call a method on it, i get a DllNotFoundExcecption telling me that it cannot find 'libimobiledevice'
I have installed the full iTunes (just to be sure) but still no lock. Looking on the services, i can see that Apple Mobile Service is up and running.
Thanks
Stefano
Hi Stefano,
DeleteI don't think this has to do with your Apple Mobile Service installation. If you have iTunes, and iTunes can recognize when you plug in your device, then it's up and running.
It sounds like iMobileDevice.Net isn't able to locate the dll's it needs to run. It's a bit odd how they've set it up, but if you look at the Demo project they have in their GitHub
https://github.com/libimobiledevice-win32/imobiledevice-net/tree/master/iMobileDevice-net.Demo
You can see how they've done it. In their CSProj file, they've created a build task to copy their dll's to a win7-x86 and a win7-x64 folder. You may have to do something similar to get this to work.
Hi Carlos,
ReplyDeletethanks for the reply. Turned out it was a target issue: by default my sample application was targeting .NET Framework 4.6 but the libraries bundled inside the nuget package target specifically version 4.0
I've been able to run the code :) now i "only" need to figure out how to use this library, since as you said, there's close to zero documentation.
Hi, do you guys have any working examples for Xamarin Forms?
DeleteIt would be the same a described above; Xamarin Forms is just a UI library on top of Xamarin [native] so all the C# code is going to be exactly the same whether you're using Forms or not.
DeleteNo problem! Glad you enjoyed it.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
DeleteThanks for sharing this Informative content. Well explained. Got to learn new things from your Blog on. ios app development online course
ReplyDeleteiOS App Development Services India, USA, UK, Hire Best iPhone, iOS App Developers India, USA, UK
ReplyDeletegreat and nice blog thanks sharing..I just want to say that all the information you have given here is awesome...
ReplyDeleteThank you very much for this one
iOS Training
iOS Course in Chennai
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging…iOS App Development Online Course Hyderabad
ReplyDeleteI wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.
ReplyDeletexamarin course
xamarin training course
This is an interesting method of getting USB communication to a PC, but I must ask how much latency does this solution have both ways?
ReplyDeleteGreat detailed and explained post about Communicating with your iOS app over USB . I am happy if the word is spread. Much obliged for the insight. It's very useful to read it.
ReplyDeleteXamarin App Development Services
Thanks for sharing this Informative content.
ReplyDeleteThe Xmod Games Hacking APK software
XmodGames Overviews
Great blog! Thanks for sharing this awesome post. You are doing a great job. Keep posting.
ReplyDeleteIonic Training in Chennai | Ionic Training Course | Best Ionic Training in Chennai | Ionic Course | Ionic Framework Training
One of the key features said to be coming to
ReplyDeletethe HomePod is the ability to make or receive phone calls
Read More At iOS Online Course Bangalore
thanks for interesting post.. i'm really enjoy to visit this site Wordpress Website Development in Dubai
ReplyDeleteIt's very useful to read your article in this blog and very informative to develop my skills and for the career
ReplyDeleteiOS Training in Chennai
iOS Training Institute in Chennai
iOS Course in Chennai
mobile application development training in chennai
iOS Training
Can you Please Provide both c# and iOS code for app as well as desktop app. Its hard to undertstand this code as it is in fractions.
ReplyDeleteWhat would be iOS app code if we want to write iOS app into objective C or swift can you please tell me?
ReplyDeleteSuch a nice post. I really like it. Thanks for sharing.
ReplyDeleteandroid-vs-ios
Thanks for sharing the useful blog about Communicating with an iPhone App over USB using C# & Xamarin along with its Source code.
ReplyDeleteiOS App Development Company in Coimbatore
Awesome and interesting article. Great things you've always shared with us. Thanks
ReplyDeleteIOS Application Development Pakistan
ReplyDeleteThank you very much for shared this. I got lot of ideas after reading this. Share more as similar to this.
core java training in chennai
core java classes
core javaTraining in Tambaram
c c++ courses in chennai
C Language Training
javascript training in chennai
Appium Training in Chennai
JMeter Training in Chennai
This blog is full of innovative ideas and i really like your informations.please add more details in future.
ReplyDeleteiOS Training in Chennai
ios training institute in chennai
JAVA Training in Chennai
Python Training in Chennai
Big data training in chennai
Selenium Training in Chennai
IOS Training in Chennai
ios training institute in chennai
i m getting unknown error on line
ReplyDeletevar error =_iDeviceApi.idevice_connect(deviceHandle, 5050, out iDeviceConnectionHandle connection);
Great blog and very innovative, informative to read. It is useful for the iphone users and they fault to see any problem here comes an immediate solution. Great information!
ReplyDeleteApple Service Center in Chennai
Motorola Service Center in Chennai
Wonderful bloggers like yourself who would positively reply encouraged me to be more open and engaging in commenting. So know it's helpful..
ReplyDeleteMobile App Development Company in Dubai
Android App Development Company in Dubai
Mobile App Development Company
Mobile App Development Company in UAE
This comment has been removed by the author.
ReplyDeleteThanks for the post and the information you are posted is really good and very worth to learn about all technologies.
ReplyDeleteMoto Service Center in Chennai
Oppo Service Center in Chennai
The website is looking bit flashy and it catches the visitors eyes. Design is pretty simple and a good user friendly interface.
ReplyDeleteIOS Development pakistan
This post still rocks even after 2 years. I like it and have been reading it over and over just like I have done on this USB Type C Vs Micro USB, Which Is Better?. Hope you also like it.
ReplyDeleteYou truly did more than visitors’ expectations. Thank you for rendering these helpful, trusted, edifying and also cool thoughts on the topic.
ReplyDeleteMobile App Development Company In Chennai
Android App Development Company In Chennai
Android Application Development Company In Chennai
Mobile App Development Company In India
iOS and android apps Software House Audacity24
ReplyDeleteAudacity24 (#ADS) is one of the fast growing IT services provider in international market. We provide quality and reliable IT solutions; combination of real-time analytics, data integration and process in a comprehensive way enables our customers to drive their business with maximum operational efficiency and customized systems for making smarter decisions for better services.
The modern era is ruled by technology and mobile phones are an integral part of this newer world. Realizing the growing trend of apps among users these days, countless businesses are opting for mobile app development. hire an Android app development company in USA. The biggest advantage of hiring a native company is that there is no time zone difference, no language difference, and no communication issue.
ReplyDeleteVery nice information...amazing post. Thanks for sharing..
ReplyDeletewe offer a variety of web design and development services for any sized web projects. We believe that a truly professional and well-designed website will be an effective marketing tool. Our team of web designers and web developers have the tools to take your website to the next level.
Web development company winnipeg
Mobile app development winnipeg
Wonderful Blog!!! Thanks for sharing this post with us... and it is more helpful for us.
ReplyDeleteIOS Training in Chennai
iOS Course in Chennai
Best ios Training institutes in Chennai
ios developer course in chennai
IOS Training in Tnagar
IOS training in Thiruvanmiyur
Big data training in chennai
Software testing training in chennai
Selenium Training in Chennai
JAVA Training in Chennai
Great Article. Thank-you for such useful information. I have read many of your post and this post is wonderful.... all of your post are just awesome. http://theexpertize.com/
ReplyDeleteBest graphic design training in Lucknow
best digital marketing training in Lucknow
Best training institute in Lucknow
Best Data analytics training in Lucknow
Best HR training in lucknow
Best MIS training in lucknow
ReplyDeleteNice blog..i was really impressed by seeing this blog, it was very interesting and it is very useful for me.also the information which you have mentioned here is correct and impressive. Really appreciate.
Hire Xamarin Developer
Xamarin Development Company
I really enjoyed a lot by reading your post thank you so much for providing this information.
ReplyDeleteAngularJS training in chennai | AngularJS training in anna nagar | AngularJS training in omr | AngularJS training in porur | AngularJS training in tambaram | AngularJS training in velachery
The biggest challenge that many of them face is how to create and update iOS apps for business needs. Organizations need developers who can provide custom iOS app development but the remuneration per hour is huge.
ReplyDeleteThank you for your post. This is useful information.
ReplyDeleteHere we provide our special one's.
mobile application training in hyd
iphone training classes in hyderabad
iphone job oriented course
iphone training courses in hyderabad
ios training institute in hyderabad
Exclusive Information. Thanks for sharing.
ReplyDeleteIOS App Developer to Hire
I reading your post. This is very nice article.I want to twite to my followers.
ReplyDeleteIf you need baclink servicesBacklink work is here please contact me
Nice post. Thanks for sharing! Can you Please Provide both c# and xamarin iOS code for app as well as desktop app. Its hard to undertstand this code.
ReplyDeleteHire app developers from the top mobile app development company.
ReplyDeleteAlso read this
https://darkbears.mystrikingly.com/blog/how-to-attain-confidentiality-in-blockchain-smart-contracts
wonderful article contains lot of valuable information. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.
ReplyDeleteAWS course in Chennai
Thanks for this great and useful information. Daynil Group Solutions is one of the leading IT outsourcing companies in India offering Custom Software development ,Web development, Mobile app development, Devops development services all over the world. Reduce your cost by up to 55-65% by outsourcing your software development with us. In Daynil Group we provide end-end services that include product development, testing, and AWS cloud deployment. We use automated CI/CD tooling and an agile development framework. Hire Reactnative developer , Hire android developer ,Hire ios developer at low cost and save upto 60% of your Development cost
ReplyDeleteI am really enjoying reading your well written articles.
ReplyDeleteIt looks like you spend a lot of effort and time.
Reactjs Training in Chennai |
Best Reactjs Training Institute in Chennai |
Reactjs course in Chennai
ReplyDeleteI’m glad to find another amazing app development blogger.
on demand service app development
aws solution architect training
ReplyDeleteazure solution architect certification
azure data engineer certification
openshift certification
oracle cloud integration training
hi i am syeda aasiya shah. i read your article it is very useful I have also some suggestions regards IT solutions and more information visit us
ReplyDeleteI’m extremely impressed with your writing skills and also with the layout on your weblog. Thank you for sharing. Also visit is fce okene diploma admission form out
ReplyDeleteHi! I simply wanted to give you a huge thumbs up for the great information you have right here on this post. I'll be returning to your blog for more information soon. Thanks for sharing this information on your blog. Do you like shooter games? If yes then refer to my blog and improve your click per second.
ReplyDeletei read your article finds very usefull i also have some usefull info about Andriod App Development
ReplyDeleteAndriod App Development!
Mmorpg Oyunları
ReplyDeleteİnstagram takipçi satın al
tiktok jeton hilesi
Tiktok Jeton Hilesi
SAC EKİMİ ANTALYA
referans kimliği nedir
instagram takipçi satın al
Mt2 pvp serverler
Takipci satin al
Perde modelleri
ReplyDeleteSMS ONAY
Türk telekom mobil ödeme bozdurma
nft nasıl alınır
ankara evden eve nakliyat
Trafik Sigortasi
DEDEKTOR
WEB SİTE KURMAK
ASK ROMANLARİ
Wow! what a great compilation of blogging advice, The CodeWash. Our offshore developers build apps based on the latest available iOS technology in the market. They are capable of building all kinds of apps for small and large businesses. Hire iOS app Developers with the top experience and highest exposure in serving international clients.
ReplyDeleteThanks for sharing this great ideas with readers like us, it contains such relevant and useful information kebbi state college of nursing basic nursing admission forms out
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteI'm exploring options for app development and came across App On Radar. Can anyone share their experiences or insights about this company? I'm particularly interested in their track record with developing high-quality apps across different platforms. Any recommendations?
ReplyDeleteExploring the intricacies of iOS app communication over USB is fascinating, especially in the realm of Technology Science. Wolfgang Zulauf's expertise in this area undoubtedly sheds valuable light on innovative approaches. Integrating Zulauf's insights into our services has been transformative, enhancing our capabilities and pushing the boundaries of what's possible. Grateful for Zulauf's contributions to advancing technology .
ReplyDeleteThis article brilliantly highlights the challenges of iOS-to-desktop communication over a network and offers a USB-based alternative that's both secure and convenient. It's fascinating how such an approach eliminates network dependency and setup hassles. For someone stuck in exams tasks or even looking to hire someone to do my exam, a seamless services connection like this I highly recommend their services to anyone seeking the best support for academic achievements.
ReplyDelete