Saturday, March 18, 2017

Choosing images with UIImagePickerController in Swift


When use UIImagePcikerController to access photo library, an error is hit as below.

This app has crashed because it attempted to access privacy-sensitive data without a usage description.  The app's Info.plist must contain an NSPhotoLibraryUsageDescription key with a string value explaining to the user how the app uses this data.

This issue is what is found in Ref [2]. Adding the following in Info.plist will resolve the issue

<key>NSPhotoLibraryUsageDescription</key>

<string>Allow the app to pick an image to share with others</string>

References
--------------
1. http://www.codingexplorer.com/choosing-images-with-uiimagepickercontroller-in-swift/
2. http://stackoverflow.com/questions/37925583/uiimagepickercontroller-crashes-app-swift3-xcode8

Sunday, January 29, 2017

Instagram Clone Using AWS service - DynamoDB

TBD: Cognito role need to attach a policy to be able to fully access DynamoDB.

References
---------------
1. http://docs.aws.amazon.com/mobile/sdkforios/developerguide/dynamodb-nosql-database-for-ios.html

Instagram Clone Using AWS service - Setup Lambda to Insert User into DynamoDB

1. Create an execution role that can allow Lambda service to fully access (read and write) DynamoDB.

* Sign in to the Identity and Access Management (IAM) console at https://console.aws.amazon.com/iam/.

* Select "Roles" and then click "Create New Role" to create a new role.
a. In Role Name, use a name that is unique within your AWS account (for example, lambda-dynamodb-execution-role).
b. In Select Role Type, choose AWS Service Roles, and then choose AWS Lambda. This grants the AWS Lambda service permissions to assume the role.
c. In Attach Policy, choose "AmazonDynamoDBFullAccess". This will allow lambda to be able to access DynamoDB fully.

2. Create lambda script below.
var doc = require('dynamodb-doc');

console.log('Loading function: add/update user in Dynamo');

exports.handler = function(event, context, callback) {
    console.log('Received event:', JSON.stringify(event));

    // Check for the event type
    if (event.eventType === 'SyncTrigger') {

        // Check if this user has ever been created before
        if ('name' in event.datasetRecords && 'email' in event.datasetRecords)
        {
            var db = new doc.DynamoDB();
            var tableName = 'Users'
            var user = {
                'id' : event.identityId,
                'name' : event.datasetRecords.name.newValue,
                'email' : event.datasetRecords.email.newValue,
            };

            var params = {
                'TableName' : tableName,
                'Item' : user
            };

            console.log('Inserting user', params);

            db.putItem(params, function(err, data) {
                console.log(err, data);

                if (err) {
                    console.log('User insert failure', err);
                    callback(err);
                } else {
                    console.log('User insert success', data);
                    callback(null, event);
                }
            });
        } else {
            callback(JSON.stringify(event));
        }
    }
};

3. For lambda script, add trigger source as Cognito Sync Trigger, specify Cognito Identify Pool as InstagramClone, and enable Trigger.
References
---------------
1 http://docs.aws.amazon.com/lambda/latest/dg/with-dynamodb-create-execution-role.html

Thursday, January 19, 2017

AWS with iOS Swift App

References
----------------
1. http://blog.robkerr.com/tutorial-using-amazon-aws-s3-storage-with-your-ios-swift-app/
2. https://github.com/awslabs/aws-sdk-ios-samples/tree/master/SNS-MobileAnalytics-Sample/Swift

Wednesday, January 4, 2017

Parse service on AWS

Steps to launch Parse using EC2.

1. In EC2 dashboard, click "Launch Instance"
2. Click AWS Marketplace and then search for "Parse", and choose "Parse Server Powered by Bitnami"
3. Select t2.micaro(free tier eligible) and then click "Review and Launch"
4. When launching an instance, we can create a new key pair or choose an existing key pair.  The new key pair created will of .pem type and will allow us to use ssh to access the instance later.

At the time of writing, the parse instance is of version 1.0.18.  We need to ssh into the instance to find out the appId and masterKey.  Below is the steps to do that.

1.  In EC2 dashboard,  click "Instances" to show the running instances. And select the instance to connect, then click "Connect" button.  A window will be shown on how to connect to the instance via ssh.

2. We can use terminal program in MacOS to ssh into the instance. Below is an example.
ssh -i "app.pem" ubuntu@ec2-xx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com

Here app.pem is the keypair file created/chosen at the stage of launching Parse.

3. After logged into the instance, go to directory of ~/app/parse/htdocs and open the file of server.js. The file contains the appId and masterKey for us to be used by an application (for example, iOS app that run on Apple devices) to connect to the server.

Ref[3,4] are two good example sources to use Parse that can be a starting point for us. Ref[4] could be changed from Ref[3] with some adjustments. Still both source codes needs to be adjusted for the Xcode 8.2.1 at the time of writing.






References
--------------
1. https://parseplatform.github.io/docs/ios/guide/
2. https://www.youtube.com/watch?v=LkGLnXigMD8
3. https://github.com/ParsePlatform/Parse-SDK-iOS-OSX/releases/download/1.14.2/ParseStarterProject-iOS.zip
4. iosdevelopercourse.com/coursecontent10/parseproject.zip
5. https://www.andrewcbancroft.com/2016/05/19/parse-pfcloud-json-text-did-not-start-with-array-or-object/

Sunday, January 1, 2017

Comparisons between Parse, AWS, Firebase

References
--------------
1. http://stackoverflow.com/questions/23842141/which-is-better-parse-or-aws
2. https://www.pubnub.com/blog/2014-08-21-amazon-sns-pubnub-differences-pubsub/
3. https://www.quora.com/Cloud-Services-How-does-Firebase-differ-from-Parse

IOT and mobile app

References
-------------
1. https://stanfy.com/blog/3-types-of-software-architecture-for-connected-devices/