Move data/images between two iOS apps using custom URL handler - ios

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

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. ; )

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

[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.

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

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.

Related

Can I create an NSURL that refers to in-memory NSData?

The docs for NSURL state that:
An NSURL object represents a URL that can potentially contain the
location of a resource on a remote server, the path of a local file on
disk, or even an arbitrary piece of encoded data.
I have a blob of in-memory data that I'd like to hand to a library that wants to load a resource via an NSURL. Sure, I can first write this NSData to a temp file and then create a file:// NSURL from that, but I'd prefer to have the URL point directly to the buffer that I already have present in memory.
The docs quoted above seem to suggest this is possible, but I can't find any hint of how to accomplish it. Am I missing something?
NSURL supports the data:// URL-Scheme (RFC 2397).
This scheme allows you to build URLs in the form of
data://data:MIME-Type;base64,<data>
A working Cocoa example would be:
NSImage* img = [NSImage imageNamed:#"img"];
NSData* imgData = [img TIFFRepresentation];
NSString* dataFormatString = #"data:image/png;base64,%#";
NSString* dataString = [NSString stringWithFormat:dataFormatString, [imgData base64EncodedStringWithOptions:0]];
NSURL* dataURL = [NSURL URLWithString:dataString];
Passing around large binary blobs with data URLs might be a bit inefficient due to the nature of base64 encoding.
You could also implement a custom NSURLProtocol that specifically deals with your data.
Apple has some sample code that uses a custom protocol to pass around image objects: https://developer.apple.com/library/mac/samplecode/SpecialPictureProtocol/Introduction/Intro.html#//apple_ref/doc/uid/DTS10003816
What you are missing is the NSURLProtocol class. Takes about three dozen lines of code, and any code that handles URLs properly can access your in-memory data. Read the documentation, it's not difficult and there is sample code available.
Unfortunately there are some APIs that take an NSURL as a parameter, but can only handle file URLs.

How can I get raw data of image from UIPasteboard if it's copied by another app (such as photos, safari, etc..)

Is there a way to get raw data of image from UIPasteboard instead of UIImage if it's copied by another app such as photos or mobile safari?
I'm currently facing wired differences between IOS 6.0 and IOS 6.1(7.0 also)
In IOS 6.0, UIPasteboard's item of the copied image by photos or mobile safari contains raw data of the image.
But In IOS 6.1 and above, it contains UIImage instead of raw data.
In IOS 6.0, copied item of UIPasteboard is below
Printing description of array:
<__NSArrayM 0x8a804c0>(
{
"com.compuserve.gif" = <47494638 39614002 ...... 3b3a2000 3b>;
"public.url" = "url of the image....";
}
)
In IOS 6.1 and above, it contains UIImage instead of raw data.
Printing description of array:
<__NSArrayM 0xa25b7b0>(
{
"com.compuserve.gif" = "<UIImage: 0x9429570>";
"public.url" = "url of the image...";
}
)
If that image format is PNG or JPEG, it's not that bad.
(I still have to compress again if it's JEPG though.)
But when I try to paste animated gif image, it becomes more complicated.
I don't know even it's passible to create animated gif image from normal UIImage.
I can download again from original url, but downloading data that I already have seems not good solution I think.
And also, if it's copied from photos app, there's no such url. (there's some mysterious uri named "com.apple.mobileslideshow.asset-object-id-uri" that is undocumented instead of url)
There seems a workaround, because when I try to do exactly same action between photos and email app, It works properly
Any suggestions?
Well now, I figured it out myself.
You can simply get raw (binary)data of the image form general pasteboard by sending
dataForPasteboardType:(NSSting*)PasteboardType message to general pasteboard, if it's copied from Apple's built-in Mobile Safari or Photos App. (#"com.compuserve.gif" for the pasteboard type in my case)
I myself feel a bit foolish for not having checked all the passible methods sooner. :(
My confusion comes from items property of the UIPasteboard.
I thought that items are containing all of data of current pasteboard. So I try to save that array from pasteboard and want to use it later, but I were totally wrong.
As documented in UIPasteboard Class Reference, the items property contains dictionary with key being the representation type and the "value" the object associated with that type.
At this point of time, The "value" refers really "value" of the representation, not the data of that type. This meaning of the "value" is the same as the value of thesetValue: forPasteboardType: method.
On the other words, you cannot retrieve raw(binary) data of the image from items property, even if you set the image to the pasteboard by sending setData: forPasteboardType: message.(I tested it on IOS 7)
In addition, raw data of the image from items property in IOS 6.0 seems a bug of that OS version. (This may not true, it's just my opinion)
You can get NSData from UIPasteboard if you specify right PasteboardType:
NSData* pasteData = [[UIPasteboard generalPasteboard] dataForPasteboardType:(NSString*)kUTTypeJPEG];
Do not forget to import
<MobileCoreServices/MobileCoreServices.h>
The UIPasteBoard will contain whatever is placed in it. It's up to the app that is copying to the paste board to put the contents in the proper format. The app can place items as raw binary data or as objects such as UIImage in the paste board.
If you're getting something different between iOS versions, you're probably using different versions of the app or it's simply copying things differently.
You're right that you can't represent an animated GIF in a UIImage because a UIImage only contains a single image. Perhaps the app is just copying the first frame's bitmap data in that case?
You can convert a UIImage to raw JPEG data using UIImageJPEGRepresentation.

How to get data in iOS pasteboard to stay multiple access

What I'm trying to do currently is basically I have 2 apps and based on if the other one is installed or not, behave in a certain way. I came upon Pasteboard for inter-app communication so I thought I should use those. Well here is the problem, both apps do something like this
UIPasteboard *pasteboard = [UIPasteboard pasteboardWithName:#"com.company.app.key" create:YES];
[pasteboard setPersistent:YES];
NSData *data = [pasteboard dataForPasteboardType:#"com.company.otherapp"];
if(data != nil)
{
// Do stuff
}
NSData *selfKey = [pasteboard dataForPasteboardType:#"com.company.thisapp"];
if(data == nil)
{
[pasteboard setValue:#"string" forPasteboardType:#"com.company.thisapp"];
}
So I installed one app and ran the other, and the first time it worked, data wasn't nil, and if I converted the NSData to NSString using the NSString initializer with NSData, I read "string".
The problem is that all I do is close the second app, run it again and suddenly data is nil. I thought at first that after I access it, the pasteboard just deletes it, so I added the following line in // Do stuff
[pasteboard setValue#"string" forPasteboardType:#"com.company.otherapp"];
Nope data is still nil if I run the app the second time. I need the string to stay in the Pasteboard forever since its all I have to tell me if my other app is installed or not, so does anyone have any ideas why data is returning nil only after 1 run?
I must note that all I am doing is pressing the home button and then running the app again,
so the situation is
-Run app1
-Run app2 (gets the data from Pasteboard)
-press home button
-Run app2 (data now nil)
When you load your pasteboard, you should set it to being persistent. After the first line in your code above, add:
pasteboard.persistent = YES;
So I figured out why app2 could not see it again. What setValue:forPasteboardType does is that it overwrites the first item in the Pasteboard. So every time I ran my apps, they would overwrite each other's key in the pasteboard.
The solution is using the class's multiple pasteboard item to search for the index of the pasteboardtype and getting data from that.

Using UIPasteboard to share data with iOS Numbers App

I'm wanting to share data in my app with iOS Numbers via the Pasteboard. I've written the following code: -
NSData *data1 = [#"Col1,1\tCol1,2\n\nCol2,1\tCol2,2\n" dataUsingEncoding:NSASCIIStringEncoding];
[[UIPasteboard generalPasteboard] setData:data1 forPasteboardType:#"com.apple.iWork.TSPNativeData"];
However, when you use NSLog to check what's stored in the pasteboard - it's empty?
Initially I've tried using the basic code: -
[UIPasteboard generalPasteboard].string = #"Col1,1\tCol1,2\n\nCol2,1\tCol2,2\n";
However when that pastes into Numbers all the text is stored within one cell, rather than being outputted into the separate cells.
Has any one had any experiencing in trying to share data from their apps and Numbers?
Thanks,
Martyn

Get copied data from UIPasteboard

i have copied image from UIwebView using clipboard and i want to mail it.For this,I use general pasteboard to get data,but there is a problem in retrieving data.When i check the pasteboard current data,it says the it has Apple Web Archive pasteboard type data,how to read this.here is my code of retriving text.
UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
NSArray* array = [pasteboard pasteboardTypes];
for (NSString* type in array) {
NSLog(#"%#",type);
}
NSString* item = #"Apple Web Archive pasteboard type";
NSData* val = [pasteboard dataForPasteboardType:item];
I tried to create a UIImage using this data but that didn't work.
I don't understand what you mean by mail it? You can paste the webpage image copy right into the mail app and it will appear as an image.
You can rebuild the data from the Apple Web Archive pasteboard type if you need to manual. It is essentially a XML document with html and the actual image data all within. The html and accompanying images are base64 encoded. If you want to look at an archive example save this, or perhaps a simple webpage in safari as an archive. Open the archive file in something like Text wrangler. Text edit will probably try to render it.
I've written a post on how to make an Apple Web Archive pasteboard type that might help you understand the process.
http://mcmurrym.wordpress.com/2010/08/13/pasting-simplehtml-into-the-mail-app-ios/
I take it you are trying to mail it from within your app and not using the mail app?
If this is the case you will probably have to get the xml from the pasteboard, find the tag that holds the encoded image data, decode it and create an image from the decoded data.

Resources