Hello everyone,
I used to think that it was impossible to passively share data between iOS apps on a given device. What I mean by that is I was under the impression that you could either share data from a server where a user is forced to login or directly by url which opens an app. I recently searched for a solution amongst the storm of developers searching for an alternate for the UDID call that Apple deprecated in iOS 5. In this post I’m going to show you the basics of how to share a unique ID that you create in 1 app and access it in another app. The way I found to accomplish this was using UIPasteboard which is essentially iOS’s way to allowing you to paste data which can be accessed by any app that knows what its looking for. You can “paste” a bunch of different types of data including dictionaries and data but in this example I’m just going to make a unique identifier, paste it to the paste board and access it in another app. Lets get started.
In the app delegate didFinishLaunchingWithOptions function of the first app:
//Create a unique id as a string
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
//create a new pasteboard with a unique identifier
UIPasteboard *pasteboard = [UIPasteboard pasteboardWithName:@"youruniquestring" create:YES];
[pasteboard setPersistent:YES];
//save the unique identifier string that we created earlier
[pasteboard setString:((__bridge NSString*)string)];
Here is the code to create a unique identifier using Apple’s unique ID creator which will be unique but not unique to our given device. What is important to note here is the “pasteboardWithName” function where I give it a unique identifier. This is important because it will uniquely identify this pasteboard so you know what to access in other apps and setPersistent which obviously means it will persist. We then save this code to the pasteboard with the last line(FYI I am using ARC which is why the __bridge is required).
In a separate app we can access this string with the following simple code (In the app delegate didFinishLaunchingWithOptions function of the second app):
UIPasteboard *pasteboard = [UIPasteboard pasteboardWithName:@"youruniquestring" create:NO];
NSLog([pasteboard string]);
Thats basically it. We created a unique identifier in one app and saved it to a pasteboard and then we accessed that string we saved in a separate app. There is a lot more that should be done here in order to use this in production but I always like to make my posts show the bare bones solution. Something to consider using this approach is that other apps have access to your data if they know what unique identifier to look for. It would probably be a very far fetched situation where someone would want to maliciously use your information or even know about it but you should keep that in mind. I hope this helps anyone looking for a tool to share information between apps and to potentially find a solution around the lack of the uniqueIdentifier function that was once supported by Apple.
-Ryan
If you found this post helpful please consider buying me a cup of coffee:
联系客服