xojo.net.TCPSocket Works in Simulator but not on iPad = WHY?

So I have a project which uses xojo.net.TCPSocket to print direct to a receipt printer, this works fine in Xojo and the Xcode Simulator.

When I build it for the iPad and run it the code will not get past the .isConnected after I try to do the .Connected

I though this was security related so Ive also included the “NSAppTransportSecurity” + “NSAllowsArbitraryLoads” gumph into my plist, but Im still stuck!!!

Sounds like a security issues on iPad but I cannot see what else to switch off or on?

Any ideas anybody… .PLEASE!

Sorry forgot to add from iPad im getting

kcferrordomaincfnetwork error 1

as the error description

Show some code please? It sounds like you expect the connection to be instant which may not be the case.

This is roughly what happens, I’ve changed code around a lot, most are global or local variables with a small letter before the variable name.

View - Event Handler - Open
ReceiptPrinter = New SelectedPrinterTill
ReceiptPrinter.Address = gSettingsMyPrinter
ReceiptPrinter.Port = 9100
ReceiptPrinter.connect

Then when I print
tPrinterData=""
tPrinterData=tPrinterData+aInitialisePrinter + CR
tPrinterData=tPrinterData+alogo
tPrinterData=tPrinterData+aaddress + CR
tPrinterData=tPrinterData+psizeM + sTitle + LF + CR
tPrinterData=tPrinterData+psizeS + sExtra + LF + CR
tPrinterData=tPrinterData+atimestamp+LF
tPrinterData=tPrinterData+aEndPrint + CR


If ReceiptPrinter.IsConnected=True Then
Dim m As xojo.Core.MemoryBlock = xojo.Core.TextEncoding.UTF8.ConvertTextToData(tPrinterData)
ReceiptPrinter.WriteData(m)
Else
MsgBox (“ERROR PRINTING”,“Printer (Receipt) Disconnected”)
end if

Ive also at the moment got this inserted at the **** point
do until ReceiptPrinter.IsConnected=true
loop

But it never gets out of the loop.

(NOTE: ReceiptPrinter is a xojo.net.TCPSocket Superclass)

As I said it works in the Simulator but not on the iPad itself

You should use the ReceiptPrinter events to do sending and checking if connected. If you loop, in the open event of the view by checking the isConnected flag it will block the entire application so the socket may never get connected. The simulator may be much faster in connecting.

You should use the event based principle to parse data and get the state.

Create a button that executes the connection state checking if not connected show a message. That may work for you

HI Derk - Yes I understand my only though is the receipt printer is not connecting on the open view, the print bits are on a button press already, so in my mind its not achieving the ReceiptPrinter.connect to start with.

Ive put a event on the connection state of the xojo.net.TCPSocket superclass but the connection never achieves the state (I put a message box), which then made me look at maybe the iPad IOS 13.x won’t do the connection due to security issues - BUT this may be totally a wrong assumption, as ive downloaded an app and that prints OK to the printer.

How does your plist look? And did you drag/paste (only!) one info.plist file in your project navigator?

This should be in it at least for local networking.

<key>NSAppTransportSecurity</key>  
 <dict>  
      <key>NSAllowsArbitraryLoads</key><true/>  
 </dict>
<?xml version="1.0" encoding="UTF-8"?> NSLocationWhenInUseUsageDescription We need to know the location of the iPad when running to change the prices automatically depending on location. NSAppTransportSecurity NSAllowsArbitraryLoads

This is an exact copy form my plist

[quote=477306:@Derk Jochems]How does your plist look? And did you drag/paste (only!) one info.plist file in your project navigator?

This should be in it at least for local networking.

[code]
NSAppTransportSecurity

NSAllowsArbitraryLoads

[/code][/quote]
Just FYI, NSAllowsArbitraryLoads is ignored in ios 10 or macOS 10.12 and higher, and you’ll need to use more specific keys:

https://developer.apple.com/documentation/bundleresources/information_property_list/nsapptransportsecurity?language=objc

Thanks Greg Ill look at those keys tomorrow

To answer your original question though… physical devices often are more locked down than a simulator ever would be, so it could just be that some other permission is not correct.

That said, @Jon Ogden just mentioned doing something just like this on another thread. You might want to reach out to him.

Damn tried the keys
NSAllowsArbitraryLoadsInWebContent / NSAllowsLocalNetworking / and all the rest still not working

I appreciate that the simulator may bypass some ‘restrictions’ but its just a simple TCP connection over my internal network, I can access websites and pull data in from them, mySQL in the cloud all working fine, just a damn silly little printer causing me sooooo much problem.

Ive read what Jon is doing my brain is fried and can’t work out if he has achieved what I want or not…

I really think this is the wrong area with the NS stuff, I can’t do the domain restrictions because the printer is only on an IP address 172 range (same as iPad and Mac) which then makes me wonder about another area, but it has to be IP based restriction.

Im going back to basics and make a new program with just working out a printer connection again

After reading this page: https://developer.apple.com/documentation/bundleresources/information_property_list/nsapptransportsecurity/nsallowslocalnetworking?language=objc
It seems that local networking is always allowed since iOS 10
Furthermore the error you get [quote]kcferrordomaincfnetwork error 1[/quote] is a Host not found error, doesn’t seem to be ATS related.
https://developer.apple.com/documentation/cfnetwork/cfnetworkerrors/kcfhosterrorhostnotfound?language=objc

Is it possible that the iPad isn’t connected to the same network as the printer?

[quote=477301:@Richard Bailey]

do until ReceiptPrinter.IsConnected=true
loop

But it never gets out of the loop.

(NOTE: ReceiptPrinter is a xojo.net.TCPSocket Superclass)

As I said it works in the Simulator but not on the iPad itself[/quote]
I wouldn’t loop on isConnected property, this might be actually blocking the main thread and preventing the connection to be made.

Could you try printing from the Connected event instead?

From my experience, iOS runs many things asynchronously without us knowing.
When you call Xojo.Net.TCPSocket.Connect it might actually connect only when the current method code has finished executing.

Jeremie - it seems the asynchronously running may be the issue, i removed all of ‘other’ things I was doing from my printing loop and works first time!!!

FYI Everybody - I am printing to an IP printer locally and within my loop I was also posting up the mySQL server on Xojo Cloud (which both work fine on simulator)

“From my experience, iOS runs many things asynchronously without us knowing.
When you call Xojo.Net.TCPSocket.Connect it might actually connect only when the current method code has finished executing.”

Well it solves my printing problem - LOL - Now have to fix the internet posting issue - Ah well Always like to loose hours of work for a silly thing …