打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
iOS and Stuff | A way to share data between apps on an iOS device

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:

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
复制粘贴板
对IOS设备中UDID的一些思考
How to get IMEI on iPhone 5
iOS and WP7 Programming Tidbits: Ruby, AppleScript, accessing the clipboard from JavaScript, Stanfor
在UIWebView中添加自定义编辑菜单
赶紧下载iOS10开发教程第二弹
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服