Sunday 1 April 2012

Inter-App Communication - Document Support in iOS Application


Hi All,

Now, I would like to discuss about how an iOS application can be communicate to other iOS application available in the device (simply says inter-app communication). May be we all know how we are able to open an url in safari from our native app. It like,

 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://knowledgetransfer-naveenshan.blogspot.com/"]];  

so here safari is an application in the device we invoke it by openURL. The iOS detect the application need to open is by using the given url scheme. i.e. here the given url scheme is "http". usually if the openURL scheme is 'http' or 'https' then iOS launch safari and safari load the given url in it.

Safari detect the url need to load is through the UIApplication delegate which will invoke when an application is launch by some other application the delegate is as follows,

 -(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {  
   //here we can get the url.  
   return YES;  
 }  

In this delegate method safari will get the url need to load and it load that url inside it.

Now you have in doubt that how can we define a url scheme for our own application,

Its very simple just add a key named "URL types" to your application info plist and define a "URL identifier" and "URL Schemes" inside it.





To load a file in iBook we can use write a scheme like this,

 //to load iBook  
 NSString *stringURL = [NSString stringWithFormat:@"itms-books://?%@",filePath];  
 NSURL *fileURL = [NSURL URLWithString:stringURL];  
 [[UIApplication sharedApplication] openURL:url];  

here we need to know the scheme defined in iBook application. Similarly to open our application from other application that application need to know scheme defined in our application. :) :) :)

So only by defining URL types in plist the iOS only allows other application to open your application only by knowing your application scheme (Great! Security) and not listed in any UIDocumentInteractionController OpenIn Menu . It is because how did iOS know whether that document is supported by your application unless and until you tell them explicitly.

hence we need to define the supported document inside the application info plist then whenever any UIDocumentInteractionController of any application checks for a document OpenIn that specified in your application, then your application is also listed in OpenIn Menu .













Sample Plist Attached here.

cheers!!!!

if you need more details feel free to contact me 

thanks,
Naveen Shan