Get copied data from UIPasteboard - ios

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.

Related

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.

What is the "iOS rich content paste pasteboard type" when copying from Safari?

When I copy text from a website in Safari, two pasteboard items are created. This is the output of [pasteboard items]
(
{
"Apple Web Archive pasteboard type" = < [...archive...] >;
"public.text" = "pages of a journal,";
},
{
"iOS rich content paste pasteboard type" = <694f5320 72696368 20636f6e 74656e74 20706173 74652070 61737465 626f6172 64207479 7065>;
}
)
What is the "iOS rich content paste pasteboard type" all about? What does the data actually represent? Is this representation ever used by other apps?
Thanks.
It's been long time since this question was asked, but anyways looks like I've found the answer in a patch for a WebKit bug.
Here's a code snippet from the patch demonstrating that this pasteboard format is just a flag for UIKit. Not sure what a "two-step paste" is all about, though.
// Flag for UIKit to know that this copy contains rich content. This will trigger a two-step paste.
static NSString* webIOSPastePboardType = #"iOS rich content paste pasteboard type";
[representations setValue:webIOSPastePboardType forKey:webIOSPastePboardType];

Recover a PDF on iOS from a web service

I have a strange problem with my iOS application that calls a .NET web service to recover a PDF.
The web service does some stuff, and returns a PDF document as a reference of the web service (via an out parameter)
The iOS application call the web service, receives an answer and deals with the XML received.
To begin, the web service just returned a "simple" pdf (created in Word). I test my application and all was running well.
I was happy :). Then, I used Microsoft Report Viewer to really generate the PDF...
So I really implement my web service for create the PDF with ReportViewer (http://msdn.microsoft.com/en-us/library/ms251671(v=vs.80).aspx). The pdf generated was correct, I can open it on my server when it was generated. But when I receive the answer in the iOS application, I can't open it because it was corrupt.
Here is the difference of the XML I receive from the web service :
When I return the pdf generated with Word : http://pastie.org/7982815
When I return the pdf generated with ReportViewer : http://pastie.org/7982811
So the main difference is the image parameter that is one-part with the simple PDF and split with the ReportViewer PDF.
Actually I have no idea why the byte array is split as it, and I don't know what I could do to receive a valid PDF.
Once again, the PDF generated with ReportViewer is good, I can open it on the server, and when I send it by e-mail I can open it on my iPad. The problem comes when the web service returns me the PDF as a byte array...
The "split" image is actually an artifact of the web service trying to parse the image for line separators (ie 0d0a == carriage return / new line). If you're lucky, you can reassemble the image by taking each one of the records, including the 0d0a, and just concatenating them. This may be easier than fixing the real problem, which is that your web service is not sending you a single blob, but trying to parse an image into "lines".
It's ok, I solved my problem by concat all the data contains in the image array.
So, considering the XML I receive from the web service (http://pastie.org/7982811), here is my objective-c code to build the correct NSData (the pdf) :
NSMutableData *concatData = [NSMutableData data];
NSArray *partsData = [result objectForKey:#"image"];
for(NSDictionary *dicPdfDataLine in partsData) { // Foreach parts of data
NSArray *arrayPdfDataLine = [dicPdfDataLine allValues];
NSData *dataLine = [arrayPdfDataLine objectAtIndex:0]; // Get the NSData of the current part part
[concatData appendData:dataLine]; // Concat the data
}

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

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.

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

Resources