开发者

data share between two iphone apps

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开发者_StackOverflow社区 are better ways than pasteboard to accomplish my task.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜