Swift로 AWS IoT 연동 구현

2017. 5. 12. 18:52아이폰 개발

가장 쓸만한 샘플 소스는 다음과 같다.

https://github.com/awslabs/aws-sdk-ios-samples/tree/master/IoT-Sample/Swift


* 주의할 점은, 샘플 소스의 경우 인증서를 사용하여 MQTT 브로커에 접속하도록 되어 있는데 AWS IoT를 비인증 접속하여 사용할 수 있도록 했다면 connectUsingWebSocket 메소드로 접속을 하도록 변경하면 된다.

iotDataManager.connect( withClientId: uuid, cleanSession:true, certificateId:myImages[0], statusCallback: mqttEventCallback)


iotDataManager.connectUsingWebSocket(withClientId: uuid, cleanSession: true, statusCallback: mqttEventCallback)


  1. The AWS Mobile SDK for iOS is available through CocoaPods. If you have not installed CocoaPods, install CocoaPods:

     sudo gem install cocoapods
     pod setup
    
  2. To install the AWS Mobile SDK for iOS, simply add the following line to your Podfile:

     pod 'AWSIoT'
    

    Then run the following command:

     pod install
    
  3. In the Amazon Cognito console, use Amazon Cognito to create a new identity pool. Obtain the PoolID constant. Make sure that the policy attached to the unauthenticated role has permissions to access the required AWS IoT APIs. More information about AWS IAM roles and policies can be found here.

  4. Open IoTSampleSwift.xcworkspace.

  5. Open Constants.swift and update the following lines with the appropriate constants:

    let AwsRegion = AWSRegionType.Unknown
    let CognitoIdentityPoolId = "YourCognitoIdentityPoolId"
    let CertificateSigningRequestCommonName = "IoTSampleSwift Application"
    let CertificateSigningRequestCountryName = "Your Country"
    let CertificateSigningRequestOrganizationName = "Your Organization"
    let CertificateSigningRequestOrganizationalUnitName = "Your Organizational Unit"
    let PolicyName = "YourPolicyName"
  6. Build and run the sample app.

  7. The sample application will allow you to connect to the AWS IoT platform, and then publish or subscribe to a topic using MQTT. You can configure the topic name under the 'Configuration' tab; it's set to 'slider' by default. You can use another instance of this application so that one instance publishes while the other subscribes, or you can use the MQTT client in the Amazon AWS IoT console to interact with your application.

  8. You can also configure the sample application to use an existing AWS IoT identity. To do this, create a certificate and private key in the Amazon AWS IoT console and associate it with a policy which allows access to 'iot:*'. Use the following command to create a PKCS #12 archive from the certificate and private key (NOTE: the filename must use the .p12 suffix):

openssl pkcs12 -export -in certificate.pem.crt -inkey private.pem.key -out awsiot-identity.p12

Drop the PKCS #12 archive you just created (named awsiot-identity.p12 in this example) in the 'Supporting Files' folder of the project, and when prompted by XCode, select all targets you want to import the identity into. Build and run the application, and it will use this identity rather than creating one dynamically. Note that when using your own certificate and private key, the "Delete" option under the "Configuration" tab only deletes them from the keychain; they remain in the application itself and will be re-added into the keychain the next time you connect.



큰 도움이 될 것이라고 생각했던 다음 소스는 큰 도움이 되지 않았다. 게다가 Swift로도 되어 있지 않다.

https://github.com/aws/aws-sdk-ios