title: I want to connect to AWS IoT core from my iOS App. (MQTT over Websockets)
body: <p>I have an iOS app that collects accelerometer data from the iPhone sensors. I want to connect to the AWS IoT Core using MQTT over WSS from my ios app.</p>
  <p>Question -&gt; Is it mandatory to have an AWS IoT certificate in case I'm using Cognito?</p>
  <p>I tried the below code using AWS SDK AWSMobileClient as mentioned on the link no luck though. The authenticated role has AWSIoTFullAccess,  IoT policy to attached to identity ID as well using CLI command attach-policy</p>
  <p><a href="https://docs.amplify.aws/sdk/pubsub/working-api/q/platform/ios#aws-credentials-based-authentication" rel="nofollow noreferrer">https://docs.amplify.aws/sdk/pubsub/working-api/q/platform/ios#aws-credentials-based-authentication</a></p>
  <pre><code>import Foundation
  import UIKit
  import AWSMobileClient
  import AWSIoT
  import AWSCore
  
  class bulbViewController: UIViewController {
      
      override func viewDidLoad() {
          super.viewDidLoad()
          
          AWSMobileClient.default().initialize { (userState, error) in
              if let error = error{
                  print(error.localizedDescription)
              }
              
              if let userState = userState{
                  print(&quot;User state: \(userState.rawValue)&quot;);
              }
          }
          
          //getting JWT ID and ACCESS tokens
          AWSMobileClient.sharedInstance().getTokens { (tokens, error) in
              if let error = error {
                  print(&quot;Error getting token \(error.localizedDescription)&quot;)
              } else if let tokens = tokens {
                  print(&quot;========= Access Token ========&quot;)
                  print(tokens.accessToken!.tokenString!)
                  print(&quot;========= ID Token ========&quot;)
                  print(tokens.idToken!.tokenString)
              }
          }
          //Create Logins map with the ID Token  to generate identity ID.
          
          AWSMobileClient.sharedInstance().getIdentityId().continueWith { task in
              if let error = task.error {
                  print(&quot;error: \(error.localizedDescription) \((error as NSError).userInfo)&quot;)
                  print(error)
              }
              if let result = task.result {
                  print(&quot;identity id: \(result)&quot;)
              }
              return nil
          }
          
          // Initialize the AWSIoTDataManager with the configuration
          let iotEndPoint = AWSEndpoint(
              urlString: &quot;wss://axxxxxxxazy1-ats.iot.us-west-2.amazonaws.com/mqtt&quot;)
          let iotDataConfiguration = AWSServiceConfiguration(
              region: AWSRegionType.USWest2,
              endpoint: iotEndPoint,
              credentialsProvider: AWSMobileClient.default()
          )
  
          AWSIoTDataManager.register(with: iotDataConfiguration!, forKey: &quot;MyAWSIoTDataManager&quot;)
          let iotDataManager = AWSIoTDataManager(forKey: &quot;MyAWSIoTDataManager&quot;)
          
          func mqttEventCallback(_ status: AWSIoTMQTTStatus ) {
              print(&quot;connection status = \(status.rawValue)&quot;)
          }
  
          iotDataManager.connectUsingWebSocket(withClientId: &quot;iospubsubthing&quot;,
                                               cleanSession: true,
                                               statusCallback: mqttEventCallback)
          
          iotDataManager.publishString(
          &quot;Hello to all subscribers!&quot;,
          onTopic: &quot;myTopic&quot;,
          qoS:.messageDeliveryAttemptedAtMostOnce)
          
          
      }
      
  }
  
  </code></pre>
  <p>output</p>
  <pre><code>User state: signedIn
  identity id: us-west-2:878a197f-2dd9-4604-ae17-beXXXXX2747
  
  connection status = 1
  connection status = 5
  connection status = 5
  connection status = 1
  connection status = 1
  connection status = 5
  connection status = 5
  connection status = 5
  connection status = 1
  
  </code></pre>
  <hr />
  <p>Corresponds to below.</p>
  <pre><code>
  typedef NS_ENUM(NSInteger, AWSIoTMQTTStatus) {
      AWSIoTMQTTStatusUnknown,
      AWSIoTMQTTStatusConnecting,
      AWSIoTMQTTStatusConnected,
      AWSIoTMQTTStatusDisconnected,
      AWSIoTMQTTStatusConnectionRefused,
      AWSIoTMQTTStatusConnectionError,
      AWSIoTMQTTStatusProtocolError
  };
  
  </code></pre>
  <p>--------- AWS CW IoT Logs -----------------</p>
  <pre><code>{
      &quot;timestamp&quot;: &quot;2021-05-17 06:56:33.660&quot;,
      &quot;logLevel&quot;: &quot;ERROR&quot;,
      &quot;traceId&quot;: &quot;ada53b6c-b019-64c4-c074-66fe4XXX1d&quot;,
      &quot;accountId&quot;: &quot;635XXXXXX&quot;,
      &quot;status&quot;: &quot;Failure&quot;,
      &quot;eventType&quot;: &quot;Connect&quot;,
      &quot;protocol&quot;: &quot;MQTT&quot;,
      &quot;clientId&quot;: &quot;iospubsubthing&quot;,
      &quot;principalId&quot;: &quot;AROXXXXIICG6AYBSVWS:CognitoIdentityCredentials&quot;,
      &quot;sourceIp&quot;: &quot;103.XX1.1X0.20X&quot;,
      &quot;sourcePort&quot;: 54036,
      &quot;reason&quot;: &quot;AUTHORIZATION_FAILURE&quot;,
      &quot;details&quot;: &quot;Authorization Failure&quot;
  }
  </code></pre>
  <p>Can you please tell us if I am missing anything over here?</p>