-1

I have two apps and I'm trying to pass the PDF file from one to another.

We will consider the two apps as App1 and App2. I'm opening App2 from App1.

So I have configured URLTypes in the info plist of App2.

On click of button 'Open App2' in the App1, I'm creating a pasteboard and assigning it with one the pdf files which is in the directory of project as below:

In App1 'openApp2' action method:

//pdf copying to pasteboard
NSString *pdfPath = [[NSBundle mainBundle] pathForResource:@"thenativescriptbook" ofType:@"pdf"];

NSURL *pdfURL = [NSURL fileURLWithPath:pdfPath];

NSData *data = [NSData dataWithContentsOfURL:pdfURL];

[[UIPasteboard generalPasteboard] setData:data forPasteboardType:(NSString *)kUTTypePDF];

//code to launch app2
NSString *customAppURL = @"Example://";
NSMutableDictionary *options;

[[UIApplication sharedApplication] openURL:[NSURL 
URLWithString:customAppURL] options:options completionHandler:^(BOOL 
success) {
    NSLog(@"**************Launch Success*************");
}];

It is launching the App2.

Now I'm trying to get the PDF file from the pasteboard in App2 like below:

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSData *data = [pasteboard dataForPasteboardType:(NSString *)kUTTypePDF];
NSURL *pdfURL = [NSURL URLWithDataRepresentation:data relativeToURL:nil];
self.pdfPath = [pdfURL path];
NSLog(@"PATH: %@",self.pdfPath);

But it is giving something wrong which I'm unable to open in any of the PDF readers.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Aug 02 '18 at 13:22
  • Sure will not add urgent....but i'm in such a situation where i need to provide the demo on the above immediately & i was stuck! – Vishwavijet N B Aug 02 '18 at 15:10
  • Indeed, but it really does not matter if the Queen is coming around for tea. If you are suffering under the assumption that your urgencies are automatically other people's urgencies, or that you can place other people under a sense of moral obligation to you by telling them you have some important work, then my response is that this site does not work like that. My personal view is that society does not work like that either, and I am perplexed as how anyone might think that it does. – halfer Aug 02 '18 at 15:12
  • I have frequently asked "urgent beggars" whether they have a national or local culture that might normalise this peculiar phenomenon, and I have never once received a satisfying answer. I'd be fascinated to understand how that mode of exchange works, given that it is (obviously) an audience comprised of volunteers. – halfer Aug 02 '18 at 15:15

1 Answers1

1

The issue seems to be in your App2, when you try to load the PDF file back. When you first set the data in App1 you retrieve the DATA of the PDF file, and put this data on the pasteboard.

Then in App2, you retrieve the data of the PDF, but then instead of saving this to a new file, you try to create a URL from the PDF data and... that fails.

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSData *data = [pasteboard dataForPasteboardType:(NSString *)kUTTypePDF];

That part so far works, but then...

NSURL *pdfURL = [NSURL URLWithDataRepresentation:data relativeToURL:nil];
self.pdfPath = [pdfURL path];
NSLog(@"PATH: %@",self.pdfPath);

You try to create a URL out of the PDF data you retrieved. Here's a rough sample on how to create a file:

NSString *cachesFolder = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *file = [cachesFolder stringByAppendingPathComponent:@"testfile.pdf"];    
[data writeToFile:file options:NSDataWritingAtomic error:nil];
halfer
  • 19,824
  • 17
  • 99
  • 186
ekscrypto
  • 3,718
  • 1
  • 24
  • 38