Wednesday, 28 October 2015

UIImage Category( Resize+RoundCourner+Alpha) Swift version

For iOS developer, Resizing images, cropping them to a specific thumbnail size, applying rounded corners is extremely easy with Trevor Harmon’s Image categories UIImage Category. These category helped many developers to finish image implementation quickly and very easily in their projects.

After apple announcement, "Swift will become a successor of objective -C", mostly iOS app developer switching their development in swift language. As Trevor Harmon’s Image categories are written in Objective-C, so developer chose bridging concept to use these category features in the swift code.

Nowadays people bypass a bridging headache as they like to complete their whole project in Swift. It will be helpful for them if they found the category in swift so that they can easily use it and finish their projects within deadline with a smiling face. 

I have created a swift version of Trevor Harmon’s Image categories found in a Blog.  Also, I have added some new methods and extra features. Here is the link of Github of these category files.

You can download them, use them directly without bridging headache like Objective-C version.

Modification or suggestion will be helpful to make it best.  In case, of any question or suggestion feel free to ask. Also, If you want any other helpful objective -C code in swift, let me know.

Tuesday, 29 September 2015

App Transport Security (ATS) and iOS 9 HTTP Connection error "The operation couldn’t be completed"

In 2015, With the launch of iOS 9 and OSX  El Capitan apple disabled all unsecured HTTP traffic from iOS apps and comes with new privacy feature called App Transport Security (ATS) to enforce best practices in secure connections between an app and its back end.

All iOS 9 devices running apps built with Xcode 7 that don’t disable ATS will be affected by this change and if app try to make HTTP requests with ATS enabled (iOS 9 and above), we will see the errors like this:

Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo=0x12ed5bad0 {NSUnderlyingError=0x12ee495b0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1004.)"


NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)

Before finding the solution to resolve this error we should know about ATS and why apple recommend this .

What is ATS and Why Apple pushing it as default from iOS 9?

According to apple “ App Transport Security (ATS) lets an app add a declaration to its Info.plist file that specifies the domains with which it needs secure communication. ATS prevents accidental disclosure, provides secure default behavior, and is easy to adopt. You should adopt ATS as soon as possible, regardless of whether you’re creating a new app or updating an existing one.

If you’re developing a new app, you should use HTTPS exclusively. If you have an existing app, you should use HTTPS as much as you can right now, and create a plan for migrating the rest of your app as soon as possible.“

Simply, App Transport Security is a feature that improves the security of connections between an app and web services. The feature consists of default connection requirements that conform to best practices for secure connections.

The reason why Apple is pushing so aggressively to force secure connections is because it’s the right thing to do. Protecting personal data from being compromised over insecure wireless connections, among other things, is great for users.

Requirement to adopt ATS :

App Transport Security is enabled by default when using NSURLSession, NSURLConnection, or CFURL in iOS 9 or OS X El Capitan.

App Transport Security requirements:

 * The server must support at least Transport Layer Security (TLS) protocol version 1.2.

 * Connection ciphers are limited to those that provide forward secrecy (see the list of ciphers below.)

 * Certificates must be signed using a SHA256 or greater signature hash algorithm, with either a 2048-bit or greater RSA key or a 256-bit or greater Elliptic-Curve (ECC) key.Invalid certificates result in a hard failure and no connection.

If your application attempts to connect to any HTTP server that doesn’t support the latest SSL technology (TLSv1.2), your connections will fail with an error like this:

Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo=0x12ed5bad0 {NSUnderlyingError=0x12ee495b0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1004.)"


App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.

How to Bypass App Transport Security:

ATS is good for you and your users and you shouldn’t disable it, but there are the way for developers to override this default behavior and turn off transport security as per the app requirement. To disable ATS you need to Open Info.plist, and add the following lines:


Your info.plist must be look like this.

Per-Domain Exceptions:

You can opt-out of ATS for certain URLs in your Info.plist by using NSExceptionDomains. Within the NSExceptionDomains dictionary you can explicitly define URLs that you need exceptions for with ATS.

Add this code to your info.plist source file.

                <!--Include to allow subdomains-->
                <!--Include to allow HTTP requests-->
                <!--Include to specify minimum TLS version-->
                <!--Include domain support forward secrecy using ciphers-->
                <!--Include to allow HTTP requests-->

Your structured info.plist source file looks like this.

And your info.plist looks like this :

Now, You can see ATS is good for app developers and app users as it improves the security of connections between an app and web services, but you can override it.

Tuesday, 4 August 2015

AppleScript: Variables, Operators and useful command

In our last blogs "Introduction to AppleScript" and "Getting Started with Apple Script", we discussed what is apple script, how it's useful for Mac users & how to write some basic script. Now it's time to go in deep and learn how to use variable, operators and more useful command.

Variables, Operators and useful command

Declaring Variables:

Variables are basically a way to store information within your script. You can, for example, do a calculation and save the result in a variable.It allows you to access that information later without doing the calculation again.

In AppleScript variables are declared using the "set" and "to" commands. set is the keyword that you use to define a variable. The syntax goes like this:

Format:     set variableName to "variableValueOrData"

Example:   set userFullName to "Rupesh Kumar"

By doing this you are setting your variable name. Many programming languages require that you state the type of variable you want in the declaration (integer, floating point, text, etc.). AppleScript however, is intelligent enough to work with your variables without any instruction about the format.

The most common data types used in AppleScript are numbers, string or text, lists,boolean,records.

string: Stores a string of characters of unlimited length. You define a string by bracketing a quantity between double-quotes:

set myString to "hello world"

You must escape the double-quote and the backslash with backslash.
set myString to "escape \" and \\ with \\"
AppleScript defines several constants of class string: return, space and tab.

boolean: true or false

integer: Stores integers in the range -536870912 .. 536870911

real: Stores (in double-precision) a real number in the range ±10^308.

date: Stores a date.

list: Stores a list of any quantities. You define a list by bracketing its items between braces.the syntax for defining a list variable goes like this: {data,data}. Text has quotes around it as usual. You can retrieve a particular list item by using the item number of listVariable syntax. The first item of the list is item 1. You can add things to the end of a list by using the concatenation operation (with the &).

set names to {"Ritesh", "Rupesh", "Amit"}
log item 2 of names
set names to names & "Rohit"
log names

record - stores a list of key-value pairs. you can access the record using it key.

set the_record to {firstname:"Rupesh", lastname:"Kumar"}
log lastname of the_record
-- kumar

How to add comments in apple Script:

In AppleScript, comment is a text inside the editor that it is ignored by the script and only there for humans to read. Comments are used to annotate the code and remind us what each operation, variable ,func, logic does.

You can add comment in applescript by adding the “—-”. For example:

script code —-Your comment

set name to "Rupesh" ---here we set the text Rupesh to variable name.

The gray text you see in the end of line after —- is the comment.

Some Useful Commands:

activate command:
Brings an application to the front, and opens it if it is on the local computer and not already running.

activate application "TextEdit"
tell application "TextEdit" to activate

launch command:
Launches an application, if it is not already running, but does not send it a run command.
If an application is already running, sending it a launch command has no effect.

launch application "TextEdit"
tell application "TextEdit" to launch

display notification command:

Posts a notification using the Notification Center, containing a title, subtitle, and explanation, and optionally playing a sound.

text:The body text of the notification. At least one of this and the title must be specified.
with title: The title of the notification. At least one of this and the body text must be specified.
subtitle text: The subtitle of the notification.
sound name text: The name of a sound to play when the notification appears. This may be the base name of any sound installed in Library/Sounds.text


display notification "It's time to go for lunch" with title "Rupesh Script classes" subtitle "Lunch Time"

copy command:
Copies one or more values, storing the result in one or more variables. This command only copies AppleScript values, not application-defined objects.

The copy command may be used to assign new values to existing variables, or to define new variables.
The copy is a “deep” copy, so sub-objects, such as lists within lists, are also copied.

set Fname to "Rupesh "
set Lname to "Kumar"
copy Fname to Lname
display dialog Fname & Lname

delay command
Waits for a specified number of seconds.

tell application “Finder” to activate
delay 2.0
end tell

Operator in AppleScript:

Arithmetic operators: Arithmetic operation can be applied on numbers only.

  + operator  example: log numberA + numberB —-Addition
  - operator  example: log numberA - numberB —-subtraction
  * operator  example: log numberA * numberB —-multiplication
  / operator  example: log numberA / numberB —-division (result will be a real)
  ^ operator  example: log numberA + numberB —- raise a number to a power
div operator  example: log numberA div numberB —- Division (Result will be an integer)

Relational operators: Relational operators can be applied for both number and string. In string operators work on the bases of string length. Only is equal to(=) operator can be applied on any data type & compare the inside data.

 > operator     example: log numberA > numberB       -- is greater than
 < operator     example: log "Rupesh" < "Rupe"          --is less than (False: based on string length)
  >= operator  example: log numberA >= numberB    --is greater than or equal to
  <= operator  example: log "Rupesh" <= "rupesh"     --is less than or equal to (based on string length)

equal, is not equal to:
We mostly use equal, is not equal to operators for numbers, list, record, and text comparison.lets see how it’s work for them in apple script.

Two number are equal if their real value is equal.

Example: log 55 = 55.1 -- result will be false
                log 55 = 55.0  -- result will be true
Two lists are equal if they both contain the same number of items and if the value of an item in one list is identical to the value of the item at the corresponding position in the other list:

{ 1, 2, “Hi” } = {1, 2, “bye”} --result: false

Two records are equal if they both contain the same collection of properties and if the values of properties with the same label are equal. The order in which properties are listed does not affect equality.

{ name:”Rupesh”, age:”24” } = { age:”24”, name:”Rupesh”}  —-result : true

Two text objects are equal if they are both the same series of characters. They are not equal if they are different series of characters.

"RUpesh" is equal to "rupesh" --result: true

considering case
"RUpesh" is equal to "rupesh"

end considering

concatenation( &):
The process of combining two pieces of text is called a concatenation operation. In AppleScript, you do this with the ampersand symbol. In appleScript & (concatenation) operation be done with string,list,record.


String concatenation:

set textA to "Hello"
set textB to " Apple"
log textA & textB

list concatenation

set names to {"Ritesh", "Rupesh", "Amit"}
log item 2 of names
set names to names & "Rohit"
log names

Same you can do with record also.

Where, I can found more command:
AppleScript itself has a wide range of commands that can be applied to any program or item in OS X. For each application there are manuals for how to communicate with those applications through AppleScript. These manuals are called "Dictionaries".

To view a dictionary, go to File>Open Dictionary in Script Editor. Scroll down the list of applications, click on any application and hit "OK".  For example, I select iTunes app and click on “OK”. You should see the following window:

The column on the left contains the available "Suites" of commands and items. When you click on a suite, you'll see everything contained in the suite displayed below.

Suites contain commands (C with a circle) and classes (C with a square), classes contain properties (P) and elements (E).

Now, use your smartness and create your own script.

Wednesday, 22 October 2014

Getting Started with AppleScript

In our last blog "Introduction to AppleScript", we discussed what is apple script and how it's useful for Mac users. Now it's time to learn how to write the AppleScript.

Getting Started with AppleScript:

To create an AppleScript, open the application "AppleScript Editor" from your application folder (Application --> utilities --> AppleScript Editor ). A simple window containing a large text field with a strip of buttons along the top.

You can see, AppleScript Editor window divided into three part. Lets understand what each each part indicate.

Top Part :
Top part contain four buttons. As their name show they are use to compile, build, record and run the script.

Middle Part:
Middle part contain the text field, where we write our script. When you start writing, the colour of text is normal. After you are done entering the code, click on the "Compile" or "Run" button from top. If your syntax is correct, your code will automatically format and colorize. If you have made an error, Script Editor will highlight the problematic area and give you a message about what it thinks might have gone wrong.

You can get the information of colours displayed in your compiled code by going to Script Editor -->Preferences --> Formatting. You can change the default colours and use your own colours.

Bottom Part:

Bottom of your Script Editor window is like a debug log. Once you run the script, bottom event log tells you what the result was, what event is happening.

Now, Its time to learn some basic command

tell Block:

The tell tells an app to do something. Most commands in AppleScript are located inside a "tell block". It's called a "tell block" because you are "telling" a given application what you want it to do.

For example:

tell application "Safari" to close every window
tell application "Safari"
      close every window

end tell

The "tell application...." means that the computer tells an app to do something. The part after "ApplicationName" is what the app will do. Here it is "close every window". End tell means it will end the tell block and quit.

say command:
The say command is something that makes your Mac talk!

Syntax:     say "Rupesh"

"Rupesh" is what your Mac will say. If you run the script, you will hear a manly voice saying the word. If you type something like "Rueewssaart", Mac voice will say it letter by letter.

Creating "Hello  Rupesh" script:

As, I said it's very much easier and you have no need some extra programming skills for it. You have to think like you are telling a person A to pass the message of some task to another person B in normal english.

For example, How you pass a message to display a chart with text "Hello Rupesh" from person A to B.  Here Person A is your system & person B is application you want to communicate using script to display chart with some text.

You personally meet to person A so, no need to indicate person A in message. Now pass the message in daily communication english language.

You probably say this to Person A:

         tell the person "B" to display the chart "Hello rupesh"

Yes, you wrote a script. You just need to convert it in technical terms. Technically, Person is an application, chart is an dialog. So,  your message will looks like this:

        tell application "Finder" to display dialog " Hello Rupesh"

Copy this and paste in your script editor textField area. Now compile & then run the script. You will see a dialog will open with "Hello Rupesh".

Now, use your smartness and create your own script with more application like, mail, safari,textEdit etc. In our next blog we will learn about variables, operators & some more useful command in AppleScript.

Tuesday, 21 October 2014

Introduction to AppleScript

Now a days, Computer makes our life easier by reducing the time and complexity of work completion. For example, In older day's, records were saved on paper's and mostly task related to updating, searching etc, was very time consuming and complex. After the introduction of computer, it’s become much easier to update, searching,indexing of the record.

Sometimes while working on the computer work we perform the same series of tasks again & again. For example, if you want to change the name of 1000's of file in a folder or want to change the extension of 100 files, you'll soon realise how much time-consuming and stressful repetitive task.

Here, you start thinking that isn't there an easier way?

Yes, it's called "Script". For Macintosh, it's called “Applescript”.

Whats is AppleScript:

AppleScript is a powerful scripting language that comes built-in with the Macintosh Operating System. It’s introduced in 1992. AppleScript is an English-like language used to write script files for Macintosh Operating System. If you are not using a computer that is running on Mac OS, you will not be able to use AppleScript.

In one line you can say "AppleScript is the language of automation for Mac OS"

Principle use of AppleScript:

The principal use for AppleScript is the automation of tasks that are normally repetitious and time consuming. Apple script helpful to automate & control the actions of the computer and the applications that run on it.

AppleScript is much more than just a macro-language, which simply repeats your recorded actions, AppleScript can "think", can make decisions based on user-interaction or by parsing and analysing data, documents or situations.

For more you can say "Apple script is an language interface for MAC, MAC can be automated and controlled using  AppleScript, which is an integral part of the Macintosh operating system.

How much it’s useful for mac user:

AppleScript has proven to be the best tool for making life with a computer a productive experience. It’s provide a lot of things.

AppleScript can be helpful to user in many way. Some of them are:

  • To create shortcuts for complex tasks like naming files, resetting preferences and many more from their day-to-day Mac experience.
  • To perform repetitive tasks, retrieving data and files from remote servers.
  • To create easy-to-use automation tools, AppleScript can automate much of what we do.
  • Schedule time to time repetitive task.
  • To make user time spent of the computer more productive.
  • To save user time and money.

The best part about AppleScript is that you don't have to be a genius programmer to use it. In fact, you don't have to have any programming experience whatsoever.
Lets start with some basics of AppleScript. In our next blog "Getting started with AppleScript", we will learn some basic command and create our first Hello script. 

Monday, 13 October 2014

What's new in iOS 8 for user (Part - 2)

In our last post  What's new in iOS 8 for user, we discussed about some new features, introduce in apple recent release of iOS (iOS 8). As, This is the biggest  release in apple history that have a lots of new features. Here we are describing some more feature's which were not included in our last post.

Share Last Location before battery dies:

Apple Find my iPhone is most popular app and useful to track your device, But if your battery goes to dies, you can not track it.
In iOS 8 apple added a new feature for this. Now in iOS 8, Find my iPhone can send the last known location of your device to iCloud before battery dies.

To enable this feature, perform these steps:
 1) Open Settings -- > Tap iCloud ( You must be logged in with iCloud ID)
 2) Tap iCloud  ---> Find my iPhone ---> Turn on the option for Send Last Location

Upgraded iMessage app:

iOS 8 iMessaging is enhanced up-to a high level. Apple added the group messaging feature like in the What’sApp. You can now name the group chat, add new Contacts to the chat, use Do Not Disturb on a per-conversation basis to keep yourself from having to deal with tons of notifications, even share your location with the group for an hour, all day or indefinitely.

Here are the some of new features of iMessage app:

1) Group messaging - You can create a group, add/ remove people into group, leave the group, do not disturb option also available when you have no time or not want to message from that group. You can turn on by switching the do not disturb button.
2) Send a voice message( Recorded/Audio messaging) - Touch and hold with your thumb to record your message, then swipe up to send it.
3) Send a video message -Tap the camera button and select video mode in camera & send it.
4) Share your location -Tap to choose how long you’d like to share your location.
5) Send multiple photo or video

To use these feature's , You need to enable iMessage with apple id or associated phone number in message setting.

Make the Photo's invisible:

Sometimes, you may not want to display all of your photos in the Photos app. Before iOS 8 you need to download some application to secure the private photo from other. For those photos, iOS 8 can now hide them from view, allowing you to still keep them around.

How to do that:
 1) Open the photo app.
 2) Navigate to the photo that you'd like to have hidden,
 3) Tap and hold on it until a menu appears.
 4) Tap the Hide button in the menu that appears above the photo.

You hidden photos still visible to other user in the Albums tab. You can unhide them by:
 1) Navigating to one of the Albums containing it,
 2) Tapping and holding on the photo
 3) Select unhide.

Improved Spotlight Search:

Apple has also improved its Spotlight tool, Spotlight no longer restricts results just to stuff on your device, such as contacts or apps. It searches for files and information on the device, the web. The tool now considers the answer, uses context to provide the most relevant choices. 

When searching for a person, item or term, a Wikipedia snippet will appear. The new Spotlight identifies hot topics and knows what’s trending.

Predictive Keyboard's:

In iOS 8, Apple has added support for more third-party keyboards and improved its own keyboard to add predictive text using quick type feature in keyboard. Now, keyboard try to predict what word you are trying to type based on your past conversations and writing style. It also adjusts based on the person you’re communicating with.

Now with quick type keyboard distinguish between informal languages uses in the Messages app, for example and more formal language when using the Mail app.

It can do this in lots of different languages. Supported languages include English optimised for the US, UK, Canada and Australia; French; German; Italian; Portuguese optimised for Brazil; Spanish; and Thai. And Simplified Chinese, Traditional Chinese and Japanese Kanji input continue to feature predictive input.


Apple wants you to be able to seamlessly switch between multiple Apple devices. With iOS 7 you can already pick up iMessages on multiple devices -- such as an iPad, iPhone and Mac With iOS 8 and OS X Yosemite, you’ll be able to do more wonderful things than ever before.

Now you can start writing an email, during free time like traveling and start the same mail when you reach in office or home on your mac, It's not only with mail, it with all most common app Mail, Safari, Pages, Numbers, Keynote, Maps, Messages, Reminders, Calendar and Contacts. This happens automatically when your devices are signed in to the same iCloud account.

Now you can do the same with SMS and phone calls on different devices as long as they running iOS 8 or OS X Yosemite on the same Wi‑Fi network. Incoming calls show the caller’s name, number and profile picture. Just click or swipe the notification to answer, ignore or respond with a quick message.

Family Sharing:

In iOS 8, Apple introduce a new concept family sharing to bring harmony in user family’s digital life.

With Family Sharing:
-Up to six people in a family can share purchases from iTunes, iBooks and the App Store without sharing accounts. 
-Up to six people can join a shared family stream to automatically share photos, calendars and reminders.

Family purchases can be paid with the same credit card and Children in the family will have to ask their parents’ permission if they want to buy apps or media with the shared credit card.

Health App:

The new Health app on iOS 8 features a dashboard showing all the user’s health and fitness data. Health app is designed to let you take control of your personal health and fitness data in a central hub. 
It can pull in stats from third-party fitness apps so you can monitor your activity, calorie intake, sleep and vitals in one place and blood type and allergy information can be accessed from the Lock Screen, in case of an emergency.

Monday, 29 September 2014

What's new in iOS 8 for user

Recently, Apple launch it's new version of mobile operating system iOS 8. This launch contains a lot of new things for iPhone & iPad user. If you are planing to buy an apple device or simply want to upgrade your existing device, here are some things you need to know about the new operating system.

1. Interactive notiļ¬cations:

In previous iOS version you can only view the notification, just like a text information.You
need to unlock the device or open the app to interact with that notification.

Example: You are getting message notification, then you need to unlock the device to reply it.

In iOS 8 apple make it easier to interact with notification. You can reply the notification without unlocking the device It allow to take action on texts, email, calendar invitations, reminders.
It's not only for apple pre-loaded app, even also for third party app. (like: Facebook, Skype, twitter.)

Now it’s easy to quickly reply to important messages and keep doing what you were doing.

2. Desktop mode in safari:

Some time, when you open a webpage in your mobile device & observe that the web page is little different that  you see it in desktop browser. Some people not like that as it totally different from desktop web site & they prefer the desktop site.

With iOS 8 Apple added desktop mode in safari for such type user. Now user can view the webpage in both mode mobile or desktop according to their preference.

You can use this feature by:
 - Tapping the address field that loaded mobile page
 - Swipe down on the screen below , you will see "Request Desktop Site" button that appears.

3. Enhanced Photo app:

Apple standard pre-loaded Photos app has been updated with better control of light, color alignment,adjustment and cropping.You have no need to edit your photo on desktop or other third party  Photo app. Just click the photo, edit it and share it with your family, friends.

Here are the new features that is added in iOS 8 photo app.

1. Photo library searching
2: Smart suggestions during photo search
3: Smart composition tools.
4: Cropping.
5: Smart adjustment - adjusts exposure, brightness, contrast and more.
6: Filter and other editing tool
7: Adjusts exposure, brightness, contrast, and more.
8: New mode comes to Camera: Time-lapse videos(snapping photos at dynamically selected intervals)

4. Battery Usage:

In iOS 7 apple make the carrier usage by every app available to user. In iOS 8 apple make the user to more interactive about their device status.
In iOS 8 apple make the battery usage by each app avaible to user.

You can see that by going to seeting --> Genral --> Usage --> Battery usage

5. Safari Credit card scanner:

Now a days online shopping is a very common. Suppose you are on online shopping site credit card  entry page & you have to enter the credit card detail manually using the iPhone or iPad keyboard.

It very hectic to enter much information using the device small keyboard & most of us enter 1 or 2 digits wrong in our first attempt.

To reduce the hectic way, apple introduce the new safari credit card scanning feature, Here are the steps to use this feature:

1: Go to online shopping site using safari.
2: Tap on card entry field.
3: You will see a button over keyboard "Scan Credit Card". Tap on it.
4: On tap of it you will see a screen with card frame . Re-position your card in that frame, it will automatically scanned when it properly positioned.

Once scanning finished you card information automatically displayed in required field.

There are some more important features that are still not include in this post. I will describe about them in "What's new in iOS 8 for user (Part -2)".