两个 iPhone 应用程序之间的数据共享

发布于 2024-08-18 12:09:46 字数 104 浏览 5 评论 0原文

我想在两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

预谋 2024-08-25 12:09:46

如果您只想使用 IPC,请让您的两个应用程序都能识别某些 URL 方案。然后调用

[[UIApplication sharedApplication] openURL:
  [NSURL URLWithString:@"theOtherApp://dataToShare"]];

发送,使用-application:handleOpenURL:接收。


要通过粘贴板共享字符串,请使用

 UIPasteboard* board = [UIPasteboard generalPasteboard];
 board.string = @"Some string to share";

save,并使用 board.string 作为 getter 来检索要共享的字符串。但是,如果用户在两者之间复制任何内容,您的共享数据将会丢失。


或者,您可以通过以下方式共享内容:

  • AddressBook。创建虚拟联系人并使用它进行共享。
  • 钥匙串。制作共享钥匙串项目。仅限 3.x。
  • 从共享文件夹中读取/写入。即使您的应用程序处于沙盒状态,也可以访问 /var/mobile/Library/AddressBookKeyboard 以及 Preferences。但这可能违反 SDK 规则。

If you simply want to use IPC, make both of your apps recognize some URL scheme. Then call

[[UIApplication sharedApplication] openURL:
  [NSURL URLWithString:@"theOtherApp://dataToShare"]];

to send, and use -application:handleOpenURL: to receive.


To share strings via the pasteboard, use

 UIPasteboard* board = [UIPasteboard generalPasteboard];
 board.string = @"Some string to share";

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:

  • AddressBook. Create a dummy contact and use it for sharing.
  • Keychain. Make a Shared Keychain Item. 3.x only.
  • Read/write from shared folders. /var/mobile/Library/AddressBook and Keyboard and Preferences can be accessed even if your app is sandboxed. This may violate SDK rules though.
无声静候 2024-08-25 12:09:46

您可以使用自定义 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文