Question

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)

Was it helpful?

Solution 2

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.

OTHER TIPS

When you load your pasteboard, you should set it to being persistent. After the first line in your code above, add:

pasteboard.persistent = YES;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top