I am using AWS Mobile Hub for an app. I have implemented a sign in screen that works properly. I need the client ID to add to my database on DynamoDB. However, I can't seem to implement this in my app.
Here's the example code from AWS docs. There's an obvious error with printing the result because it thinks we're saving to the NoSQL table but please ignore that.
@IBAction func readButton(_ sender: Any) {
let dynamoDbObjectMapper = AWSDynamoDBObjectMapper.default()
//Create data object using data models you downloaded from Mobile Hub
let newsItem: News = News();
dynamoDbObjectMapper.load(
// Use AWSIdentityManager.default().identityId here to get the user identity id.
newsItem.setUserId("us-east-1:01234567-89ab-123c-4de5-fab678cde901"),
News.self,
hashKey: userId,
rangeKey: rangeKey,
completionHandler: {
(error: Error?) -> Void in
if let error = error {
print("Amazon DynamoDB Save Error: \(error)")
return
}
print("An item was saved.")
})
}
This is what I implemented in my ViewController:
let dynamoDbObjectMapper = AWSDynamoDBObjectMapper.default() let user: Users = Users();
dynamoDbObjectMapper.load(
let userId = AWSIdentityManager.default().identityId,
User.setUserId(userId),
Users.self,
hashKey: userId,
completionHandler: {
(error: Error?) -> Void in
if let error = error {
print("Amazon DynamoDB Save Error: \(error)")
return
}
print("An item was saved.")
})
}
Not sure how to find the user ID. Please help
In the code snippet, userId refers to the IdentityId that is fetched from the AWSIdentityManager. The IdentityId is given by the AWS Cognito Federated Identities that serves as the unique identifier for the user to connect to AWS Services.
@IBAction func readButton(_ sender: Any) {
let dynamoDbObjectMapper = AWSDynamoDBObjectMapper.default()
//Create data object using data models you downloaded from Mobile Hub
let newsItem: News = News();
dynamoDbObjectMapper.load(
// Use AWSIdentityManager.default().identityId here to get the user identity id.
let userId = AWSIdentityManager.default().identityId
newsItem.setUserId(userId),
News.self,
hashKey: userId,
rangeKey: rangeKey,
completionHandler: {
(error: Error?) -> Void in
if let error = error {
print("Amazon DynamoDB Save Error: \(error)")
return
}
print("An item was saved.")
})
}