0

I have two sets of data I am trying to copy to clipboard. An audio file and a string. I can't get to copy them both, but each individually works. Here is the code I have to set the data and copy it:

.h

@property NSData *previewData;
@property NSString *linkData;

.m

NSArray *preview = [JSON valueForKeyPath:@"results.previewUrl"];
NSArray *linkData = [JSON valueForKeyPath:@"results.url"];

_previewData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[preview objectAtIndex:0]]];
_linkData = [linkData objectAtIndex:0];

// Need to combine these two lines
[pasteboard setData:_previewData forPasteboardType:@"public.mpeg-4-audio"];
pasteboard.string = _linkData;
abc123abc
  • 5
  • 4

1 Answers1

2

When you set the string property of a UIPasteBoard it replaces all current items. You need to set the items property, which takes an array of dictionaries, where each dictionary represents an item to be added to the pasteboard -

NSDictionary *imageItem=@{@"public.mpeg-4-audio":self.previewData};
NSDictionary *textItem=@{@"public.plain-text":self.linkData};

pasteboard.items=@[imageItem,textItem];
Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • Sorry, there was a typo - I used ',' when it should be ':'. Also I was trying to use constants from core services, but this give a problem with Objective C, so it is easier to just use the literals – Paulw11 Nov 16 '14 at 22:49
  • It works! Sorta. I need to delete what is already copied in pasteboard before though – abc123abc Nov 16 '14 at 23:01
  • Sorry, yes. The difference between your original code and addItems is that `addItems` adds to the existing content. – Paulw11 Nov 16 '14 at 23:02
  • So I should just use this before those lines since there is no way to clear it: `[pasteboard setValue:@"" forPasteboardType:UIPasteboardNameGeneral];` – abc123abc Nov 16 '14 at 23:04
  • I have updated my answer - you can just assign directly to the `items` property to replace what is already there – Paulw11 Nov 16 '14 at 23:05
  • Did you get my last update - I forgot to change the : to = at first – Paulw11 Nov 16 '14 at 23:08
  • hey could you please look my issue http://stackoverflow.com/questions/28272426/how-do-i-copy-audio-file-to-clipboard-and-send-via-imessage-ios-8 – jamil Feb 02 '15 at 12:23