将应用程序连接到AWS IoT Core时遇到一些问题。当我尝试连接时会导致connectionError。代码无法在此处建立连接。任何帮助人员,在此先感谢
private func initializeConnectionAwsIotCore() {
guard let awsIotConnectCredentials = getAwsIotConnectCredentials(),let iotCoreEndPoint = awsIotConnectCredentials.iotCoreEndPoint ,let accessId = awsIotConnectCredentials.awsAccessId,let secretKey = awsIotConnectCredentials.awsSecretAccessKey else {
return
}
AWSMobileClient.default().initialize { (userState, error) in
if error != nil {
AppDelegate.getAppDelegate().log.debug("Failed to Initialize AWSMobileClient. Error : \(error!.localizedDescription)")
return
}
}
let awsCredentialProvider = CustomAwsCredentialProvider(accessKey: accessId, secretKey: secretKey)
let iotEndPoint = AWSEndpoint(urlString: iotCoreEndPoint)
let iotDataConfiguration = AWSServiceConfiguration(region: .USEast1,
endpoint: iotEndPoint,
credentialsProvider: awsCredentialProvider)
AWSIoTDataManager.register(with: iotDataConfiguration!, forKey: AwsIotEventServiceConnection.AWS_IOT_DATA_MANAGER)
self.awsIotDataManager = AWSIoTDataManager(forKey: AwsIotEventServiceConnection.AWS_IOT_DATA_MANAGER)
mqttConnection = AwsIotEventServiceConnection(iotCoreEndPoint: iotCoreEndPoint, cleanSession: getCleanSession(), clientId: getClientId(),newMessageCallback: { (payload) in
self.onNewMessageArrivedFromAWSIotCore(payload: payload)
}, awsIotDataManager: self.awsIotDataManager, mqttCallback: self)
connectToMqtt()
}
private func connectToMqtt() {
self.mqttConnection!.connect()
}
func connect() {
guard let clientId = self.clientId else { return }
awsIotDataManager?.connectUsingWebSocket(withClientId: clientId, cleanSession: cleanSession, statusCallback: { (status) in
self.handleMqttCallback(status)
})
}
这是连接到AWS IoT Core的正确方法。尝试使用发布在媒介上的示例代码