SFSpeechRecognizer online: at which url is sent the request? - ios

I'm developing an iOS application in Swift that uses SFSpeechRecognizer (package: Speech).
This application is developed for an organization that uses VPN and blocks every request from/to the external network.
This is partial code:
let speechRecognizer = SFSpeechRecognizer(locale: Locale.init(identifier: self.userLanguageExt))
let recognitionRequest = SFSpeechAudioBufferRecognitionRequest()
let recognitionTask = self.speechRecognizer?.recognitionTask(with: self.recognitionRequest!,resultHandler: { (result, error) in
if let result = result{
...
}
else if error == nil {
...
}
})
I need to know from/to which url is received/sent the RecognizerRequest so I can comunicate it to network team of the organization and they will open the connection to those url. On docs I couldn't find a lot.

Answer
17.250.13.5 (if you check the ip lookup on https://www.iplocation.net/ you find that is an Apple domain)
How
I've done tcpdump on my device following this documentation and catched the packets traffic: https://developer.apple.com/documentation/network/recording_a_packet_trace

Related

SwiftUI: URL Request over specific browser

I want to retrieve a json from an intranet from outside the intranet in an iPad app. I have a VPN connection configured for this, but it only runs through the VMWare web browser. Safari uses the normal connection.
Is it possible, similar to the browser schemas, to use awbs:// instead of https:// and trigger the VPN configuration from an app? I don't want to open the browser though, I just want to use it. ;)
func getData() async {
#State var value: [responsejson]?
guard let url = URL(string: "https:/...") else {
print("Invalid URL")
return
}
let request = URLRequest(url: url)
URLSession.shared.dataTask(with: request) {data, response, error in
if let data = data {
if let decodedResponse = try? JSONDecoder().decode(responsejson.self, from: data) {
DispatchQueue.main.async {
self.response = decodedResponse.text
}
return
}
}
print("Error: \(error?.localizedDescription ?? "Unknown error")")
}.resume()
}
I tried to replace here https with awbs, but unfortunately it did not work
Task <1039D056-FDC4-46EE-9635-373AF8723409>.<1> finished with error [-1002] Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL, NSErrorFailingURLStringKey=awbs://(...)
http,https or awbs is not just a text, but a definition of the protocol. Protocol defines how the data will be transferred, and there is a specific list of protocol that iOS support. see https://en.wikipedia.org/wiki/Internet_protocol_suite (don't be confused with app-scheme, when you open a link and specific app response to it, these are two different things).
So when you try to create an instance of a URL object it actually parses a protocol and uses instructions to make a Request.

Unable to connect to remote RabbitMQ server using Swift

I am using RabbitMQ in my app for chat module. It works fine with local server but somehow I am not able to connect to remote RabbitMQ server. I keep getting this error when I try to send a message.
Received connection: <RMQConnection: 0x6000022e2eb0>
disconnectedWithError: Error Domain=GCDAsyncSocketErrorDomain Code=7
"Socket closed by remote peer" UserInfo={NSLocalizedDescription=Socket
closed by remote peer}
My swift code looks like this:
func getRabbitMQUrl() -> String{
var components = URLComponents()
components.scheme = "amqps"
components.host = "[broker-id].mq.[region].amazon.aws.com"
components.user = "[username]"
components.password = "[passowrd]"
components.port = 5671
let url = components.url?.absoluteString ?? "-"
print("RabbitMQ URL", url)
return url
}
let uri = getRabbitMQUrl()
let conn = RMQConnection(uri: uri, delegate: RMQConnectionDelegateLogger())
conn.start()
let ch = conn.createChannel()
let q = ch.queue(UUID().uuidString, options: .durable)
let exc = ch.direct("my-exchange-name-here")
q.bind(exc, routingKey: "my-routing-key")
q.subscribe({(_ message: RMQMessage) -> Void in
print("Message received")
})
While using the local server, I set the uri "amqp://[username]:[password]#localhost:5672" and this works as expected.
PS: when I set this subscriber I do not get any error message regarding connection or anything. so I think it is connecting to the server without any issue.
But, when I send a message from the iOS app, the backend publish it and so the iOS app should receive it back. Exactly at this time, it gives me the above error!
EDIT: Though the C# backend is able to publish and subscribe messages successfully with RabbitMQ remote server. It is just the iOS client who fails!
Any help would be appreciated!
After going through a lots of links, slack channels and Github issues, finally the issue has been resolved! The solution was unexpected.
The problem was, my C# backend has set the vhost to a slash / and in my Swift code I was passing an empty string instead. I got hint from here
I made these 2 changes in my code:
In server uri I added %2f(a slash /) as the vhost at the end.
I set the options of the exchange also to .durable just like the queue
Here is the complete working code:
func getRabbitMQUrl() -> String{
var components = URLComponents()
components.scheme = "amqps"
components.host = "[broker-id].mq.[region].amazon.aws.com"
components.user = "[username]"
components.password = "[passowrd]"
components.port = 5671
components.path = "/%2f" //1st change
let url = components.url?.absoluteString ?? "-"
print("RabbitMQ URL", url)
return url
}
let uri = getRabbitMQUrl()
let conn = RMQConnection(uri: uri, delegate: RMQConnectionDelegateLogger())
conn.start()
let ch = conn.createChannel()
let q = ch.queue(UUID().uuidString, options: .durable)
let exc = ch.direct("my-exchange-name-here", options: .durable) // 2nd change
q.bind(exc, routingKey: "my-routing-key")
q.subscribe({(_ message: RMQMessage) -> Void in
print("Message received")
})
Your URI is AMQP on the local host but AMQP is the example code.
You should connect to port 5671 if you are using AMQPS (And 5672 if you are on AMQP) Try that!

How to set AWS Appsync request timeout limit || AWSAppSync Client not giving callback

I'm using AWS Appsync for the current App I'm developing and facing a serious issue that is Whenever I fire queries in Appsync client, when there is slow internet connection the request never end with a callback. I checked over internet there is limited source of information on this topic and also found this issue that is still open.
This is the code I used to get the response
func getAllApi(completion:#escaping DataCallback){
guard isInternetAvailabele() else {
completion(nil)
return
}
// AppSyncManager.Client() is AWSAppSyncClient Object
AppSyncManager.Client().fetch(query: GetlAllPostQuery(input: allInputs), cachePolicy:.fetchIgnoringCacheData) {
(result, error) in
var haveError:Bool = error != nil
if let _ = result?.data?.getAllPostings?.responseCode {haveError = false} else {haveError = true}
if haveError {
print(error?.localizedDescription ?? "")
completion(nil)
return
}
if result != nil{
completion(result)
}else{
completion(nil)
}
}
}
The code works fine with internet connection and I have already checked at the top if there is no internet but when there is slow internet connection or the wifi is connected to a hotspot that I created with my mobile with internet data disabled the request doesn't return any callback it should give failed alert like we get in other apis when the request time out.
Is there any support for request for request time out or did I miss something?
Note : I recieved these logs in Terminal
Task <06E9BBF4-5731-471B-9B7D-19E5E504E57F>.<45> HTTP load failed (error code: -1001 [1:60])
Task <D91CA952-DBB5-4DBD-9A90-98E2069DBE2D>.<46> HTTP load failed (error code: -1001 [1:60])
Task <06E9BBF4-5731-471B-9B7D-19E5E504E57F>.<45> finished with error - code: -1001
Task <D91CA952-DBB5-4DBD-9A90-98E2069DBE2D>.<46> finished with error - code: -1001
Actually there could be two possible ways to fix the issue,
1) While configuring AWSAppSyncClientConfiguration, provide a custom URLSessionConfiguration and set the request timeout to your needs,
extension URLSessionConfiguration {
/// A `URLSessionConfiguration` to have a request timeout of 1 minutes.
static let customDelayed: URLSessionConfiguration = {
let secondsInOneMinute = 60
let numberOfMinutesForTimeout = 1
let timoutInterval = TimeInterval(numberOfMinutesForTimeout * secondsInOneMinute)
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = timoutInterval
configuration.timeoutIntervalForResource = timoutInterval
return configuration
}()
}
And pass this session configuration i.e URLSessionConfiguration.customDelayed when initializing AWSAppSyncClientConfiguration as it accepts the URLSessionConfiguration in the below constructor,
public convenience init(url: URL,
serviceRegion: AWSRegionType,
credentialsProvider: AWSCredentialsProvider,
urlSessionConfiguration: URLSessionConfiguration = URLSessionConfiguration.default,
databaseURL: URL? = nil,
connectionStateChangeHandler: ConnectionStateChangeHandler? = nil,
s3ObjectManager: AWSS3ObjectManager? = nil,
presignedURLClient: AWSS3ObjectPresignedURLGenerator? = nil) throws {
2) If the first doesn't work then you have another option to edit/unlock the pod files directly. There is a class AWSAppSyncRetryHandler where you can change the logic for retrying request. If you are able to fix the issue then you can fork the original repo, clone your repo, make changes in your repo and in pods file point this pod to use your repository. This should be done as changing the pod files directly is absolutely wrong until you are really stuck and want to find some solution.
Update: This issue has been fixed with AppSync SDK 2.7.0

TIC TCP Conn Failed when running Swift project on my phone

I am new to Swift. I built a simple application which works fine on the simulator. I am running the same on my device (iPhone 6s with iOS 11.0.2) and it's failing to connect to server.
getting these errors:
2017-10-26 18:16:02.489134-0400 myproj[1451:206438] TIC TCP Conn Failed [1:0x1c0176800]: 1:61 Err(61)
2017-10-26 18:16:02.489771-0400 myproj[1451:206438] Task <0C30ADDC-4A0E-4815-A701-2EF0A7CF5F04>.<1> HTTP load failed (error code: -1004 [1:61])
2017-10-26 18:16:02.490293-0400 myproj[1451:206440] Task <0C30ADDC-4A0E-4815-A701-2EF0A7CF5F04>.<1> finished with error - code: -1004
Please help me understand this error.
EDIT:
Here is the code making that call to the server:
func postRequest(postData: NSDictionary, postHeaders: NSDictionary, endPoint: String,
onComplete: #escaping ((NSDictionary)->Void), callbackParams: NSDictionary = NSMutableDictionary()) {
let url:URL = baseUrl.appendingPathComponent(endPoint)
let session = URLSession.shared
let request = NSMutableURLRequest(url: url)
request.httpMethod = "POST"
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
var paramString = ""
for (key, value) in postData{
paramString = paramString + (key as! String) + "=" + (value as! String) + "&"
}
request.allHTTPHeaderFields = postHeaders as? [String : String]
request.httpBody = paramString.data(using: String.Encoding.utf8)
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(data, response, error) in
guard let _:Data = data, let _:URLResponse = response , error == nil else {
return}
let json: Any?
do {
json = try JSONSerialization.jsonObject(with: data!, options: [])
}
catch {
return
}
var serverResponse = json as? NSDictionary
DispatchQueue.main.async{
for (key, value) in serverResponse!{
callbackParams.setValue(value, forKey: key as! String)
}
onComplete(callbackParams)
}
})
task.resume()
}
EDIT:
Thanks!
Error -1004 is URLError.cannotConnectToHost. It cannot connect to the server for some reason.
In comments, you say the URL is http://127.0.0.1. That is localhost, the current machine. If you use that URL on a physical phone, it's going to look for the web server on the phone. Lol. It works on the simulator, because the simulator is your computer, the localhost.
You need a URL that your iPhone can resolve to the machine running your web service. For example, find out what the IP for the computer running the web service on your local network, make sure your iPhone is on wifi on the same network, and then use that unique IP number for your computer on the LAN (likely something more like 192.168.0.x).
I was getting the same problem when i was sending/ receiving socket messages! I am using Socket.io and Firebase for push and the error was at the node.js backend. The error was that the mongoose version was deprecated. Whenever this error arises at the iOS End, ask the node.js backend team to look for Red color crash logs. They will surely find the problem causing this error on frontend. Hope this helps !!
In my case i'm not changes the Local host IP address.
Example : #"http://Localhost/Host name/index.pnp/apiname?";
initially i'm used above URL, so i'm got the same error...
Later i changed in to Localhost to 190.177.0.22
Example : #"http://190.177.0.22/Host name/index.pnp/apiname?";
Another corner case if you using something like ngrok and it's still not working is the phone isn't connected to a service provider network or wifi.
In my case my main phone was working the second out of service phone was not.

Checking for internet connection or wifi connection with reachabilitySwift

I'm building an iOS app, at some point i needed to check the app's acces to internet, so i used the ReachabilitySwift library.
After some tests, it seems to me that the library only checks if the device has the wifi connected and not having an actual internet connection provided from my router.
lets say i disconnected my router from internet, if my iOS device is connected via wifi to the router, the reachability tells me that we have internet connection where in actuallity my router has no internet.
I tried the reachability with a particular host but still having the same result
var reachability = Reachability.init(hostname: "google.com");
Is the reachability library supposed to give feedback when the wifi connection is lost or the actual internet connection is lost ?
Thank you in advance.
I have had similar issues with Reachability where I was making web service calls to VPN protected network.
var reachability = Reachability.init(hostname: "google.com");
didnt work for me as it returned true when there is Wifi connection.
I used Alamofire response when a dummy call is made to the server
func networkIsAvailable(available: (Bool, String?) -> Void) {
var message : String = ""
DispatchQueue.main.async {
HUD.show(.progress)
Alamofire.request(Constants.baseUrl, method: .get, parameters: ["foo": "bar"])
.responseJSON(completionHandler: { (response) in
let error = response.result.error as? NSError
if error?.localizedDescription == networkAlert {
message = networkAlert
available(false, message)
} else if error?.code == cannotConnectServerCode || error?.localizedDescription == cannotConnectServerMessage {
message = cannotConnectServerMessage
available(false, anotherNetworkAlert)
} else {
available(true, nil)
}
})
}
}
At WWDC, Apple has said many many times that if you need to simply see if WiFi or cell is up - use Reachable. If you need to see if you can connect to - then don’t use Reachable; instead simply connect and handle the error.
Reachable doesn’t actually check to see if it can connect to that IP based resource and if you are going to get a result back. The only way to do that is to actually try.
Look for networking sessions with Quinn on Apple’s WWDC site to see the same advice.
In the Reachablility.h you can find a declaration:
typedef enum : NSInteger {
NotReachable = 0,
ReachableViaWiFi,
ReachableViaWWAN
} NetworkStatus;
Then, inside Reachability class you can find the method, which returns type:
- (NetworkStatus)currentReachabilityStatus;
So, use it. In your swift code you can do it like this:
if reachability.currentReachabilityStatus == .ReachableViaWiFi {
// Do things...
}

Resources