Thursday, 24 May 2018

Background

There are various network calls that we may have to make from your device to server. Internet network connection may not always be available for review. So it is better if we can check if the network is reachable before making the API calls. In this post, we will see the same in Objective C.

How to check an active internet connection on iPhone/iOS

First, add SystemConfiguration framework to your project.

To do this double click on your project. This should open your project settings. Select your app target.

Now go to "Build Phases" Tab on the top

Now Expand "Link Binary With Libraries"

Now click on '+' icon and search for "SystemConfiguration"

You should see "SystemConfiguration.framework"

Select it and click Add

You should see it would be added under "Link Binary With Libraries"

You should also start seeing "Framework" group under project navigator -

Background

If not, then please refer the previous post and set that up. Before starting with using this you should have -

Identity pool ID

AWS region where pool and S3 bucket reside

S3 bucket name

We will use above in configuration and implementation that follows.

How to upload files to S3 from iOS app written in Objective C using AWS Cognito identity pool

First, go to your pod file and update following dependencies -

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'TestApp' do
# Uncomment the next line if you're using Swift or would like to use dynamic frameworks
# use_frameworks!
# Pods for TestApp
pod 'AWSMobileClient', '~> 2.6.18' # For AWSMobileClient
pod 'AWSS3', '~> 2.6.18' # For file transfers
pod 'AWSCognito', '~> 2.6.18' # For data sync
end

Next run "Run pod install --repo-update" from the command line

Once you have the dependencies installed we can now write Objective C code to upload a file to S3.

Background

Many time when you are creating application be it mobile or web you have to provide an authentication mechanism for users to sign in before they can use your apps or websites features. AWS Cognito service does the same thing.

Amazon Cognito makes it easy for you to have users sign up and sign in to your apps, federate identities from social identity providers, secure access to AWS resources and synchronize data across multiple devices, platforms, and applications.

You can do 3 things using this service -

With Cognito Your User Pools, you can easily and securely add sign-up and sign-in functionality to your mobile and web apps with a fully-managed service that scales to support hundreds of millions of users.

With Cognito Federated Identities, your users can sign-in through social identity providers such as Facebook and Twitter, or through your own identity solution, and you can control access to AWS resources from your app.

With Cognito Sync, your app can save user data, such as preferences and game state, and sync that data to make your users' experiences consistent across their devices and when they are disconnected.

NOTE: AWS Cognito is a region-specific service so you have to select a region and make sure this service is available in that region. For this post, I am going to use us-east-1(N.Virginia).

Also in this post, I am going to show how we can create a federated identity pool which can be used to authenticate and access AWS resources. Though I am going to limit this post to setting-up the identity pool and corresponding AWS changes we will see how we can use this to upload sample files to Amazon S3 bucket.

How to setup Cognito Identity Pool for unauthenticated or authenticated access to AWS resources

Sign into AWS console and go to Cognito service.

Now click on "Manage Federated Identities"

Provide a name for your identity pool. Eg. athakur_test

If you want unauthenticated users to use this pool for authenticating and accessing AWS resources select "Enable access to unauthenticated identities". I am going to select this since finally, I want to use this pool to upload files to S3.

NOTE: Amazon Cognito can support unauthenticated identities by providing a unique identifier and AWS credentials for users who do not authenticate with an identity provider. If your application allows customers to use the application without logging in, you can enable access to unauthenticated identities.

If you want to provide access to authenticated users only then use one of the Authentication providers like Facebook, Amazon, Google etc

Click on "create pool".

Next screen you should see the roles that AWS will create for you. You can select "View details" to see the role details.

Finally, click "Allow"

NOTE1: Assigning a role to your application end users helps you restrict access to your AWS resources. Amazon Cognito integrates with Identity and Access Management (IAM) and lets you select specific roles for both your authenticated and unauthenticated identities.

NOTE2: By default, Amazon Cognito creates a new role with limited permissions - end users only have access to Cognito Sync and Mobile Analytics. You can modify the roles if your application needs access to other AWS resources, such as S3 or DynamoDB.

You can see click on video details to see this default policy document -

Note down the Identity pool id and region . In my case identity pool id is "us-east-1:f847843f-0162-43c2-b73f-efdc7c69cce2" and region is us-east-1. We will need it in subsequent setup.

The last part that is remaining now is to add additional permissions to our new role so that Cognito pool users can access other required AWS services - in this case since we need to upload files to S3 we just need S3 put access.

NOTE: Make sure Cognito identity pool should be created in same region as S3 bucket.

So go to IAM and go to roles and select it. Now click add inline policy and add following JSON policy -

You have to replace <BUCKET_NAME> with your actual S3 bucket name. Always make IAM policies as stricter as possible. In this case, we are just giving put object permission on the desired S3 bucket only.

Saturday, 19 May 2018

Background

In many scenarios, we need the current time in milliseconds. In this post, we will see various methods to get the time in milliseconds since the UNIX epoch (January 1, 1970 00:00:00 UTC) in various programming languages.

How to get the current time in milliseconds

Following are ways you can get current time in milliseconds for various programming languages -

Friday, 11 May 2018

Background

If you are a Mac user then you must be familiar with the peculiar trackpad gestures and usage Mac offers. Personally, I feel touch to click is a much better option then physically pressing the trackpad. In this post, I will show you how to do the same. I am using MacBook Pro running Sierra (10.13.4).

How to enable touch to click on your Mac

Go to "Preferences" and select "Trackpad"

Under "Point and Click" select "Tap to Click"

Notice how "Click with two fingers" is also changed to "Click or tab with two finders" for secondary click.

You can use this checkbox to enable or disable "Tap to click"

How to enable touch to click on your Mac with command line

You can do the same above setting with the command line. Execute following commands in your Terminal.