8

After googling around and searching SO for a while, I stil couldn't find an answer -

I've wondered, How could I transfer data between two of my apps using custom URL handlers? Specifically images or an NSData object for that matter.

I know about being able to open specific parts of my app using custom handlers such as myapp1://start , myapp2://start , but I'm not sure how to go on transferring large amounts of data (~80k) through these handlers.

Would love to hear any creative solutions :)

p.s. The solution should be iOS >= 4.3 Compatible

Shai Mishali
  • 9,224
  • 4
  • 56
  • 83

5 Answers5

11

Use the custom URL handlers in combination with UIPasteboard. Save something from your first app (say, an image) to the general pasteboard, like so:

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
[[UIPasteboard generalPasteboard] setImage:myImage];

Then use the custom URL schemes to switch apps.

Then retrieve your image from within the new app when you need it:

   UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
   UIImage *tempImg = pasteboard.image;

Battle-tested. ; )

crgt
  • 1,482
  • 1
  • 14
  • 17
3

One Solution could be:

  • Implement a Webserver
  • Open up your second app via the custom url scheme with the IP-adress and the port of your custom web server included in the url
  • Add the route or parameters to your image also to your URL

Download and enjoy your photo :-)

Another Solution:

  • Start a Bonjour service
  • in a network the second app can find this service
  • do some magic to pass the data in between the apps

EDIT: BETTER OTHER SOLUTION:

Just found another, but much more efficient way to do exchanges of larger data sets:
It is called UIPasteboard

Best reference for that:
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIPasteboard_Class/Reference.html

And another resource:
http://kmithi.blogspot.in/2012/03/sharing-data-among-ios-applications.html

That should do it. For a webserver: There are tons of implementations found using Google

Fab1n
  • 2,103
  • 18
  • 32
  • Could provide any documentation or samples to point me in the right direction? – Shai Mishali Sep 08 '12 at 14:05
  • implementing Webserver will consume resources useless. Others solutions needs networking. –  Sep 08 '12 at 14:09
  • Hey @matheszabi. You shouldn't down vote, just because the solution consumes resources. And if you down voted me, just because I downvoted you: YOUR SOLUTION IS NOT ALLOWED AND NOT WORKING – Fab1n Sep 08 '12 at 14:10
  • Implementing a short webserver or a bonjour service might work but please provide more info on HOW and not just WHAT. Thank you. – Shai Mishali Sep 08 '12 at 14:12
  • @ShaiMishali Made my EDIT, please check it out and use UIPasteboard – Fab1n Sep 08 '12 at 14:18
  • Your answer was correct but not very informative when compared to crgt. So im marking his as the most correct one and will give you the bounty. – Shai Mishali Sep 09 '12 at 08:48
  • Yes, that's very ok for me. Thank you. Enjoy the solution! – Fab1n Sep 09 '12 at 09:09
1
     [http://www.codeproject.com/Articles/43447/How-to-Use-UIPasteBoard-to-Implement-Custom-Copy-a][1]

        As you know many of the controls in UIKit now come pre-loaded with the ability to copy and paste text. You can also use this new ability in your own apps to copy and paste other things including: images, SQLite databases, text or any file. This is a great way to share data between your apps if you want to provide users with a suite of apps with integrated functionality.



     CopyFrom Source Code
        -(IBAction)copyImageToPasteBoard{
            UIPasteboard *appPasteBoard = [UIPasteboard pasteboardWithName:@"CopyFrom" 
                     create:YES];
            appPasteBoard.persistent = YES;

            NSData *data = UIImagePNGRepresentation([UIImage imageNamed:@"Old-Time-Photo.jpg"]);
            [appPasteBoard setData:data forPasteboardType:@"com.appshop.copyfrom.imagedata"];
        }

        -(IBAction)copyStringToPasteBoard{
            UIPasteboard *appPasteBoard = [UIPasteboard pasteboardWithName:@"CopyFrom" 
                     create:YES];
            appPasteBoard.persistent = YES;
            [appPasteBoard setString:textView.text];
        }
        PasteTo Source Code
        -(IBAction)pasteImageToPasteBoard{
            UIPasteboard *appPasteBoard = [UIPasteboard pasteboardWithName:@"CopyFrom"
                     create:YES];
            NSData *data = [appPasteBoard dataForPasteboardType:@"com.appshop.copyfrom.imagedata"];
            imageView.image = [UIImage imageWithData:data];
        }

        -(IBAction)pasteStringToPasteBoard{
            UIPasteboard *appPasteBoard = [UIPasteboard pasteboardWithName:@"CopyFrom" 
                     create:YES];
            textView.text = [appPasteBoard string];
        }


    Using UIPasteBoard in iPhone programming is amazingly simple and opens up some possibilities that we did not have a year ago. To use UIPasteBoard you simply create a instance using pasteboardWithName, put stuff into the paste board and then set the persistant property equal to YES. Then any app can get a reference to your paste board and use the data contained within. You can use it for simple strings and even data that you can put into NSData like SQLite databases.
jbchitaliya
  • 211
  • 2
  • 13
0

using custom URL handler

it is specified, so it will not meet your specification.

But if you don't care about that, I would write to internal memory (file system)the 80k image, even if 80Mb and pass the "url" for the other app.

Big ego, and 0 effort in research:

copy to images folder

IPC communication via url

and my solution doesn't work... I am not giving fish, just teaching hot to get a fish.

Community
  • 1
  • 1
  • How would I write the data as a resource accessible by both apps ? Isn't each app sandboxed ? – Shai Mishali Sep 08 '12 at 14:02
  • how to do it that is another question, but here is a post about that: http://stackoverflow.com/questions/1500935/does-iphone-api-allow-application-read-write-to-iphones-filesystem , pls check the second answer –  Sep 08 '12 at 14:05
  • Apps are indeed sandboxed. Not possible to access the files of another app (speaking of no jailbreak) – Fab1n Sep 08 '12 at 14:07
  • there are common part where each of they can acces, like I have provided the link too, pls check it –  Sep 08 '12 at 14:08
  • @matheszabi second answer's link isn't working. And you cannot share files between apps, as you write them to disk. They are contained in a secure container. NO JAILBREAK – Fab1n Sep 08 '12 at 14:09
  • @matheszabi - The link you provided makes it seem like its not possible. You can't access it without a jailbreak. Of course the solution has to be App-Store approvable. – Shai Mishali Sep 08 '12 at 14:10
0

Well, as far as I can tell ~80k is too much for a custom URL handler. But you could try to Base64-encode your data and append it to the custom URL. But I doubt that it will work with lots of data.

You could also have a look on the UIDocumentInteractionController, which is intended to open files with other applications that support them. This is what the Mail application does when you open an attachment with another app.

https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIDocumentInteractionController_class/Reference/Reference.html#//apple_ref/doc/uid/TP40009304

swehrli
  • 141
  • 4