Category Archives: Objective-C

Inter-Process Communication on Mac – Know How part4(Distributed Notifications)

In  Part 1 of this blog series we discussed about various techniques available for Inter-Process Communication (IPC) on Mac OS X.

As discussed in previous blog  Distributed notifications work as follows :
1. A notification center manages the sending and receiving of notifications. It notifies all observers of notifications meeting specific criteria.
2. The notification information is encapsulated in NSNotification objects. Client objects register themselves with the notification center as observers of specific notifications posted by other objects.
3. When an event occurs, an object posts an appropriate notification to the notification center.

Posting a distributed notification is an expensive operation. The notification gets sent to a system wide server that then distributes it to all the processes that have objects registered for distributed notifications. The latency between posting the notification and the notification’s arrival in another process is unbounded. In fact, if too many notifications are being posted and the server’s queue fills up, notifications can be dropped.

Let’s look at a simple code to send and receive distributed notification. Read the rest of this entry

Advertisements

Inter-Process messaging in cocoa

Objective-c supports an inter-process messaging system. An application running on different machines and on same computer can send message’s to each other.

Objective-c classes use for inter-process messaging are as follows:-

NSConnection
NSPort
NSMachPort
NSMessagePort
NSSocketPort
NSProxy etc..

NSConnection object manage the communication between objects and normally run in the background.

NSPort object represent the communication channel. Its an abstract class and sending and receiving messages handle through port.

NSProxy is an abstract superclass and typically a proxy forward the message to real object and load the real object.

Example: A program using NSSocketPort, NSConnection to send message from one computer to another computer. Read the rest of this entry

Inter-Process Communication on Mac – Know How part3 (Apple Events)

In  Part 1 of this blog series we discussed about various techniques available for Inter-Process Communication (IPC) on Mac OS X.

As discussed in the previous post Apple Events are the only IPC mechanism which is universally supported by GUI applications on Mac OS X for remote control. Operation like opening a application or telling a application to open a file or to quit etc. can be done using these.
AppleScript is a scripting language built on top of Apple Events which can be used using scripting bridge in a Mac application.

Here in this blog we will see the implementation  of IPC using apple events. Read the rest of this entry

How to detect and identify mounted and unmounted USB devices on Mac using Cocoa

There could be a situation in which we have to notify the application about mounting and un-mounting the USB and also to identify the USB i.e on which path it is mounted, what is the type of USB e.g I-Pod, I-Phone , Pen Drive etc.

First let’s see how can the application be notified of USB mounting and un-mounting.
To get the notification for mounting and un-mounting  of the USB device we can create 2 notifications:

Notification for Mountingthe USB device

[[[NSWorkspacesharedWorkspace] notificationCenter]addObserver:selfselector:@selector(deviceMounted:)  name: NSWorkspaceDidMountNotificationobject: nil];

Notification for Un-Mountingthe USB device

[[[NSWorkspacesharedWorkspace] notificationCenter]addObserver:selfselector:@selector(deviceUnmounted:)  name: NSWorkspaceDidUnmountNotificationobject: nil];

Once we get the notifications, now is the task to get the volume path on which the USB gets mounted.
This code will give an array of volume paths for each mounted USB

NSArray* devices = [[NSWorkspacesharedWorkspace] mountedRemovableMedia];

Now After getting the mounted paths, let’s identify the USBs
Every device has a VendorId, ProductId, ReleaseID and Name configured by the manufacturer. For ex. if the mounted USB is a I-Pod then it can be identified using the combination of it’s VendorId and ProductId. To get these attributes, Add IOKit framework to the project and import following headers in .m file. Read the rest of this entry

Inter-Process Communication on Mac – Know How part2 (Shared Memory)

In  Part 1 of this blog series we discussed about various techniques available for Inter-Process Communication (IPC) on Mac OS X.

As discussed in earlier blog Shared Memory is a implementation for IPC where a memory section is shared between different processes.In other words process A writes to this memory and B can read from this memory, or vice verse. This is fast and data doesn’t have to be copied around. The downside is that it’s really difficult to coordinate changes to the shared memory area.

Here in this blog we will see the implementation  of IPC using shared memory. We will create a server and client and will create a communication channel for data transfer between two processes.

First let us look at the basic implementation of Server using Shared memory Read the rest of this entry

Objective-c 2.0 features

Synthesize take place by default:-
In previous version , if we declare properties then we have to synthesize that property. But in modern
objective-c, we don’t require to synthesize the proeprty.
Eg:-
In modern objective-c,

@Interface MyClass : NSObject

@property (nonatomic, retain) NSString* myProperty;

@end

@Implementation MyClass

//Don’t require to synthesize the property in modern objective-c. Compiler automatically
synthesize it.
@end

Method ordering:-
In previous version, if method call before defining it and without declaring it, then it will give compile
time error. But in modern version if method invoke before definition and without any declaration ,then
complier will give no error.

NSNumber literals:-
In previous version, the initialization of NSNumber objects require class method.

Eg:- NSNumber* number =  [NSNumber numberWithInt:12];

But in modern objective-c  , NSNumber  objects can initialize like this:-
NSNumber* number  = @3;
For character,
NSNumber * number  = @’A’

Array Literals:-
In modern objective- c initialization of array literals   have less typing.

Eg:- As per previous version:-
NSArray* array = [[NSArray alloc] initWithObjects:@”A”, @”B” , @”C”, nil];
As per  modern objective-c:-
NSArray* array = @[@”A”, @”B”, @”C”, @”D”, @”E”];

Accessing array elements:-

As per previous version:-
NSString*  element = [array objectAtIndex:0];

As per modern objective-c:-
NSString* element = array[0];

The new syntax using index subscripting.

Creating mutable array as per new syntax:-
NSMutableArray* mutableArray = [@[@”A”, @”B”] mutableCopy];

Dictionary Literals:-
Initialize   dictionary as per previous version:-

NSDictionary* dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:object1, key1, object2,     key2, nil];

Initialize dictionary as per modern objective-c:-

NSDictionary* dictionary =  @{Key1: object1, key2:object2};

As with array, subscripting can also use in referencing dictionary items in modern objective-c.

object1  = dictionary[Key1];

instancetype:-
Its a return type keyword thats return an instance of a class they are called on.
In modern objective-c use instancetype instead of id where appropriate. It provide more type safety
instead of id.
Eg:-
Suppose there is a class

@Interface MyClass: NSObject

+(instancetype) methodA;
+(id) methodB;

@end

@Implementation MyClass

+(instancetype) methodA
{
return [[[self class] alloc] init];
}

+(id)methodB
{
return [[[self class] alloc] init];
}

@end

The methodA return an instance of kind MyClass and methodB return an instacne of id.
So suppose if we write any method and wrote some code like:-

[[MyClass methodA] count];

And

[[MyClass methodB] count];

then first line of code will give compile time error becuase no count method is declare and define in MyClass, but second line return id type object and compile will not give any warning in this case.


Optional garbage collector and fast enumeration:-

Objective-c 2.0 have optional garbage collector option and also have fast enumeration  feature.

Written By: Yogesh Arora, Software Engineer, Mindfire Solutions

Minimum Cost String Transformation Problem with varying cost of operations

Overview

In computer science, edit distance is a way of quantifying how dissimilar two strings (e.g., words) are to one another by counting the minimum number of operations required to transform one string into the other.

Edit distances find applications in natural language processing, where automatic spelling correction can determine candidate corrections for a misspelled word by selecting words from a dictionary that have a low distance to the word in question.

In bioinformatics, it can be used to quantify the similarity of macromolecules such as DNA, which can be viewed as strings of the letters A, C, G and T.

Several definitions of edit distance exist, using different sets of string operations. One of the most common variants is called Levenshtein distance, named after the Soviet Russian computer scientist Vladimir Levenshtein. In this version, the allowed operations are the removal or insertion of a single character, or the substitution of one character for another. Levenshtein distance may also simply be called “edit distance”, although several variants exist.

Levenshtein distance algorithm gives shortest path to reach to the destination string but that is not unique. So, The algorithm will give reliable minimum cost path only if all the operations i.e Insertion, Deletion and Substitution have same cost.

In this text I will analyze and provide an algorithm using decision tree and list for getting minimum cost path in a string transformation where all operations have varying cost depending on the character on which the operation is performed.

In the text I will also present source code in Objective-C language for the implementation of the algorithm.

Read the rest of this entry

Access/control scriptable application using scripting bridge and Cocoa

ScriptingBridge framework in Cocoa can be used to access features from any scriptable application. In this article I will discuss how to access the features of iTunes such as current playing track, duration of the track, playlists etc by using ScriptingBridge.framework in a Cocoa Application. However you can follow the same steps to allow your application to interact with any other scriptable application.

Read the rest of this entry

How to get selected text and its coordinates from any system wide application using Accessibility API?

In my previous post, I had discussed about accessing the text value from any system wide application by using Accessibility API. Now going further,  you may want to access only the selected text or the position of selected text, we will discuss about it in this article.

Read the rest of this entry

Working with JNI on MAC OSX platform

 In this post, I will discuss about the Java Native Interface(JNI) and Java Virtual Machine(JVM) on Mac OSX.

JNI stands for ‘Java Native Interface’. It will act as an interface between java and other languages. JNI allow a code written written in  languages like C, C++,  Objective-C etc to use java libraries and from java code to use other language code.It means you can call java methods from other languages using JNI and vice versa. Java native methods are flagged by the keyword ‘native’.

Advantages:

  • Use the existing library,
  • Speed of execution,
  • Invoke API functions from product that is developed in C or C++ from a java client and vice versa.

Disadvantages:

  • Run time errors debugging is difficult in native code.An applet can’t call a native methods.
 You can’t say ‘write once run anywhere’.

Introduction to JVM
JVM stands for ‘Java Virtual Machine’. Java Virtual Machine or JVM is a platform independent execution environment that converts Java byte-code into machine code(binary). JVM function is to load the appropriate class files for executing a java program, and then to execute it. Read the rest of this entry

%d bloggers like this: