两个 iPhone 应用程序之间的数据共享
我想在两个 iPhone 应用程序之间共享文本数据。我如何使用粘贴板来达到此目的。谁能帮我提供示例代码。提前致谢。建议我是否有比粘贴板更好的方法来完成我的任务。
i want to share text data between two iphone apps. How can i use paste board for this purpose. Can anyone help me with a sample code. Thanks in advance. Suggest me if there are better ways than pasteboard to accomplish my task.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只想使用 IPC,请让您的两个应用程序都能识别某些 URL 方案。然后调用
发送,使用
-application:handleOpenURL:
接收。要通过粘贴板共享字符串,请使用
save,并使用
board.string
作为 getter 来检索要共享的字符串。但是,如果用户在两者之间复制任何内容,您的共享数据将会丢失。或者,您可以通过以下方式共享内容:
/var/mobile/Library/AddressBook
和Keyboard
以及Preferences
。但这可能违反 SDK 规则。If you simply want to use IPC, make both of your apps recognize some URL scheme. Then call
to send, and use
-application:handleOpenURL:
to receive.To share strings via the pasteboard, use
to save, and use
board.string
as a getter to retrieve the string to share. But if the user copy anything in between your shared data will be lost.Alternatively, you can share stuff via:
/var/mobile/Library/AddressBook
andKeyboard
andPreferences
can be accessed even if your app is sandboxed. This may violate SDK rules though.您可以使用自定义 URL 方案 在您的应用程序之间进行通信。
我不会污染用户的剪贴板,除非他们特别想将某些内容复制到剪贴板 - 想象一下将文本片段存储在剪贴板中,然后让某些应用程序覆盖它只是为了将数据发送到另一个应用程序的挫败感......不是良好的用户体验。
更新:
正如@ohhorob评论的那样,可以创建自定义粘贴板。如果您要通过粘贴板共享数据,请确保创建您自己的(持久)应用程序粘贴板并使用它而不是主粘贴板。请参阅 此处了解更多信息。
You could use custom URL schemes to communicate between your apps.
I wouldn't pollute the user's clipboard unless they specifically wanted to copy something to the clipboard—imagine the frustration of storing a snippet of text in the clipboard and then having some app overwrite it just to send data to another app... Not a good user experience.
Update:
As @ohhorob commented, it is possible to create custom pasteboards. If you are going to share data via the pasteboard, make sure you create your own (persistent) application pasteboard and use this instead of the main pasteboard. See here for more information.