ios 在两个或多个 iPhone 应用程序之间共享数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9425706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Share data between two or more iPhone applications
提问by Kamleshwar
Is this possible to share data between two applications on the same device?
这是否可以在同一设备上的两个应用程序之间共享数据?
Or can I allow some other application to use my application's information / data or in any other way?
或者我是否可以允许其他应用程序使用我的应用程序的信息/数据或以任何其他方式?
For example, the first application is for event management, and I use it to save some event. The second application is for reminders, which will get data from the other application in order to remind me about the event.
例如,第一个应用程序是用于事件管理,我用它来保存一些事件。第二个应用程序用于提醒,它将从另一个应用程序获取数据以提醒我有关该事件的信息。
This is just a simple example, not a real scenario.
这只是一个简单的例子,不是真实的场景。
采纳答案by baudot
Historically, the iPhone has tried to prevent data sharing between apps. The idea was that if you couldn't get at another app's data, you couldn't do anything bad to that app.
从历史上看,iPhone 曾试图阻止应用程序之间的数据共享。这个想法是,如果你无法获得另一个应用程序的数据,你就不能对该应用程序做任何坏事。
In recent releases of IOS, they've loosened that up a bit. For example, the iOS programming guide now has a section on passing data between apps by having one app claim a certain URL prefix, and then having other apps reference that URL. So, perhaps you set your event app to answer "event://" URLs the same way that a webserver answers for "http://" URLs.
在最近的 IOS 版本中,他们放宽了一点。例如,iOS 编程指南现在有一节介绍通过让一个应用声明某个 URL 前缀,然后让其他应用引用该 URL 来在应用之间传递数据。所以,也许你设置你的活动应用答案“事件://”网址以同样的方式,对“HTTP://” Web服务器的URL的答案。
Apple's documentation of that approach is here.
苹果关于这种方法的文档在这里。
Have a peek under "Implementing Custom URL Schemes".
看一看“实施自定义 URL 方案”。
回答by Durai Amuthan.H
In the sandboxedworld of iOS development sharing data between applications can prove difficult Since iOS developers can't share data directly through the file system, they need to find alternate solutions for their applications. Some common solutions include:
在iOS 开发的沙盒世界中,在应用程序之间共享数据可能很困难 由于 iOS 开发人员无法直接通过文件系统共享数据,他们需要为他们的应用程序寻找替代解决方案。一些常见的解决方案包括:
UIDocumentInteractionController
UIActivityViewController
Shared Keychain Access
Custom URL Scheme
Web Service
iCloud API
UIDocumentInteractionController
UIActivityViewController
共享钥匙串访问
自定义 URL 方案
网络服务
云API
UIDocumentInteractionController:
UIDocumentInteractionController:
Allows the user to open a document in any other application that registers as being able to handle a particular document Uniform Type Identifier (UTI).
The UIDocumentInteractionController has been used in the past as a means of opening a document in other applications on the device, for example, opening email attachments from the Mail app.
允许用户在注册为能够处理特定文档统一类型标识符 (UTI) 的任何其他应用程序中打开文档。
UIDocumentInteractionController 过去曾被用作在设备上的其他应用程序中打开文档的一种方式,例如,从邮件应用程序打开电子邮件附件。
Unfortunately, the UIDocumentInteractionController‘s UI displays only six applications.
You cannot guarantee that your application will appear in the list. While the UIDocumentInteractionController has not been deprecated, the UIActivityViewControllerprovides a more flexible replacement as of iOS 6.0.
不幸的是,UIDocumentInteractionController 的 UI 只显示六个应用程序。
您不能保证您的应用程序会出现在列表中。虽然 UIDocumentInteractionController 尚未被弃用,但 UIActivityViewController提供了从 iOS 6.0 开始更灵活的替代品。
Availability:iOS 3.2+
可用性:iOS 3.2+
Pros:
优点:
- Allows sharing of common data types with a wide array of applications.
- 允许与各种应用程序共享通用数据类型。
Cons:
缺点:
Allows control of the type of data sent to the UIDocumentInteractionController, but not the destinations.
Requires additional user interaction.
Limited number of data destinations may cause your application not to display in the list.
允许控制发送到 UIDocumentInteractionController 的数据类型,但不能控制目标。
需要额外的用户交互。
有限数量的数据目的地可能会导致您的应用程序不显示在列表中。
UIActivityViewController:
UIActivityViewController:
Allows the user to perform a number of actions with an array of data.
For example they may print, email, copy, post to social media, or open in another application.
You may create your own UIActivity subclasses to provide custom services to the user.
允许用户使用一组数据执行许多操作。
例如,他们可以打印、发送电子邮件、复制、发布到社交媒体或在其他应用程序中打开。
您可以创建自己的 UIActivity 子类来为用户提供自定义服务。
Availability:iOS 6.0+
可用性:iOS 6.0+
Pros:
优点:
Great for sharing common data types with a wide array of applications and social media.
Can supply an array of items for application to an activity. Objects should conform to UIActivityItemSource protocol.
Has the ability to set excluded activity types.
Paging UI allows for more data destinations than UIDocumentInteractionController.
非常适合与各种应用程序和社交媒体共享常见数据类型。
可以提供一系列项目以应用于活动。对象应该符合 UIActivityItemSource 协议。
能够设置排除的活动类型。
分页 UI 允许比 UIDocumentInteractionController 更多的数据目的地。
Cons:
缺点:
You must define a custom activity type to restrict “Open In…” destinations of common data types.
Requires additional user interaction.
您必须定义自定义活动类型以限制常见数据类型的“打开方式...”目标。
需要额外的用户交互。
Shared Keychain Access:
共享钥匙串访问:
Allows you to securely store data to a shared keychain that other applications that are part of a suite of applications can access.
All applications that share keychain access must use the same app ID prefix.
For an example of shared keychain access in action. See Apple's GenericKeychain sample code.
允许您将数据安全地存储到共享钥匙串中,作为应用程序套件一部分的其他应用程序可以访问该钥匙串。
共享钥匙串访问的所有应用程序必须使用相同的应用程序 ID 前缀。
有关共享钥匙串访问的示例。请参阅Apple 的 GenericKeychain 示例代码。
Availability:iOS 3.0+
可用性:iOS 3.0+
Pros:
优点:
- Secure access to data.
- 安全访问数据。
Cons:
缺点:
You can only share data between applications that share a common app ID prefix.
The Keychain API on the iOS Simulator comes from OS X, which has different API than that of the iOS device.
您只能在共享通用应用 ID 前缀的应用之间共享数据。
iOS Simulator 上的 Keychain API 来自 OS X,它具有与 iOS 设备不同的 API。
Custom URL Scheme:
自定义 URL 方案:
Allows data to pass between applications using simple URLs.
允许数据使用简单的 URL 在应用程序之间传递。
Availability:iOS 3.0+
可用性:iOS 3.0+
Pros:
优点:
- No network connection required.
- Great for small amounts of data that you can easily encode into an escaped, legal URL.
- 无需网络连接。
- 非常适合您可以轻松编码为转义的合法 URL 的少量数据。
Cons:
缺点:
You must encode data into an escaped legal URL.
Note:base64 encoding has seen common use turning serializable data into a string value. However, base64 strings may include characters that are invalid for use in URLs. You might consider using base64url. See Base 64 Encoding with URL and Filename Safe Alphabetfor more information.
您必须将数据编码为转义的合法 URL。
注意:base64 编码已被广泛使用,将可序列化数据转换为字符串值。但是,base64 字符串可能包含在 URL 中使用无效的字符。您可以考虑使用 base64url。有关详细信息,请参阅带有 URL 和文件名安全字母表的 Base 64 编码。
iCloud API:
云API:
Everybody knows about what is iCloud,Pros and Cons so no more explanation for that.
But One might ask how it is possible to share data between applications inside a single device there are some workarounds to achieve that.
每个人都知道什么是 iCloud,优点和缺点,因此不再对此进行解释。
但是有人可能会问如何在单个设备内的应用程序之间共享数据,有一些解决方法可以实现这一点。
It's possible because the identifier which is used for iCloud is different from bundle identifier so it's possible to share images,videos and other documents.
To know more see the discussion on this topic
这是可能的,因为用于 iCloud 的标识符与捆绑标识符不同,因此可以共享图像、视频和其他文档。
要了解更多信息,请参阅有关此主题的讨论
Web Service:
网络服务:
Sync data through third party (e.g. Dropbox) or custom built web service.
通过第三方(例如 Dropbox)或定制的网络服务同步数据。
Availability:iOS 2.0+
可用性:iOS 2.0+
Pros:
优点:
- Useful for sharing and otherwise distributing large amounts of data.
- 用于共享和分发大量数据。
Cons:
缺点:
- Requires a network connection.
- Web service implementation overhead.
- 需要网络连接。
- Web 服务实现开销。
回答by ChintaN -Maddy- Ramani
From iOS 8I've successfully Access Same folder in using "App Group Functionality." I'm extending answer of @siejkowski.
从iOS 8我已经成功地使用“应用程序组功能”访问相同的文件夹。我正在扩展 @siejkowski 的答案。
Note:It will work from same developer account only.
注意:它仅适用于相同的开发者帐户。
For that you have to follow below steps.
为此,您必须遵循以下步骤。
- first Enable "App Groups" from your developer account.
- Generate Provisioning profile. and use it.
- 首先从您的开发者帐户启用“应用程序组”。
- 生成配置文件。并使用它。
Now You have to create Two Apps. Sample Name
现在您必须创建两个应用程序。样品名称
- Demo_Share_One
- Demo_Share_Two
- Demo_Share_One
- Demo_Share_Two
Now We are copying images from Demo_Share_Oneto Sharing folder which is created by default when you enable App Groups and run app. and will access all those images from Demo_Share_Two.
现在我们正在将Demo_Share_One 中的图像复制到共享文件夹中,该文件夹在您启用应用程序组并运行应用程序时默认创建。并将从Demo_Share_Two访问所有这些图像。
You have to Take Group Name which was set to your developer account.lets say group.filesharingdemo
.
您必须使用设置为您的开发人员帐户的组名称group.filesharingdemo
。让我们说。
Add Below method in Both apps to get Relative path of sharing folder url.
在两个应用程序中添加以下方法以获取共享文件夹 url 的相对路径。
- (NSString *) getSharedLocationPath:(NSString *)appGroupName {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *groupContainerURL = [fileManager containerURLForSecurityApplicationGroupIdentifier:appGroupName];
return [groupContainerURL relativePath];
}
Now we are Copying Images from Bundle from Demo_Share_One
现在我们从Demo_Share_One 的Bundle 复制图像
-(IBAction)writeImage:(id)sender
{
for (int i = 0; i<15; i++)
{
NSString *strSourcePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"hd%d",i+1] ofType:@"jpg"];
NSString *strDestinationPath = [[self getSharedLocationPath:@"group.filesharingdemo"] stringByAppendingPathComponent:[NSString stringWithFormat:@"hd%d",i+1]] ;
BOOL filewrite = [[NSFileManager defaultManager]copyItemAtPath:strSourcePath toPath:strDestinationPath error:nil];
if (filewrite)
NSLog(@"File write");
else
NSLog(@"can not write file");
}
}
Now in Demo_Share_Twoto access those images
现在在Demo_Share_Two中访问这些图像
NSString *pathShared = [[self getSharedLocationPath:@"group.filesharingdemo"] stringByAppendingPathComponent:[NSString stringWithFormat:@"hd%d.jpg",number]];
NSLog(@"%@",pathShared);
//BOOL fileExist = [[NSFileManager defaultManager] fileExistsAtPath:pathShared];
imgView.image = [UIImage imageWithContentsOfFile:pathShared];
And Now You will get all images which your write from Demo_Share_One.
现在您将获得您从Demo_Share_One写入的所有图像。
So From now onwards if you want to share this folder two your third app. just add that app in your group. So it is too easy to access same elements in Your Multiple apps.
所以从现在开始,如果你想共享这个文件夹,你的第三个应用程序。只需将该应用程序添加到您的组中即可。所以在你的多个应用程序中访问相同的元素太容易了。
if you will not enable App Groups in your AppID then you will get [self getSharedLocationPath:@"group.filesharingdemo"] is null.
如果您不会在您的 AppID 中启用应用程序组,那么您将得到 [self getSharedLocationPath:@"group.filesharingdemo"] 为空。
Thanks to Apple for Share Elements from your own apps functionality. Happy Coding. :)
感谢 Apple从您自己的应用程序功能中共享元素。快乐编码。:)
回答by siejkowski
Since iOS 8 you can easily share data between apps as long as they are in the common App Group.
从 iOS 8 开始,只要应用程序在公共应用程序组中,您就可以轻松地在应用程序之间共享数据。
Apple documentation best explains it in the Extensions context: https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html
Apple 文档在扩展上下文中对其进行了最好的解释:https: //developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html
Basically, you need to:
基本上,您需要:
- Define App Group ID (in Certificates, Identifiers & Profilessection of Member Centerfor your Apple Developer Program.
- Enable App Groups capability specifying the above App Group ID for each app that needs to communicate (cen be done either in Xcode: Target -> Capabilitiesor at Member Center).
- Use one of two APIs for shared container access.
- 定义 App Group ID(在Apple Developer Program的Member Center 的Certificates, Identifiers & Profiles部分。
- 启用应用程序组功能,为每个需要通信的应用程序指定上述应用程序组 ID(在 Xcode: Target -> Capabilities或Member Center 中完成)。
- 使用两个 API 之一进行共享容器访问。
First API is based on NSUserDefaults
:
第一个 API 基于NSUserDefaults
:
NSString *appGroupId = @"group.my.group.id";
NSUserDefaults *myDefaults = [[NSUserDefaults alloc]
initWithSuiteName:appGroupId];
[myDefaults setObject:@"foo" forKey:@"bar"];
Second API is based on NSFileManager
. It's simply a shared folder that you can access after obtaining it's url:
第二个 API 基于NSFileManager
. 它只是一个共享文件夹,您可以在获取它的 url 后访问它:
NSString *appGroupId = @"group.my.group.id";
NSURL *sharedFolderURL = [[NSFileManager defaultManager]
containerURLForSecurityApplicationGroupIdentifier:appGroupId];
Anything you put inside myDefaults
or the folder pointed by sharedFolderURL
will be visible and accessible for all your apps.
您放入的任何内容myDefaults
或指向的文件夹sharedFolderURL
都将对您的所有应用程序可见且可访问。
In case of folder, please write/read atomically just to ensure no deadlocks are possible.
在文件夹的情况下,请以原子方式写入/读取,以确保不会出现死锁。
回答by JugsteR
Share data between apps possible? Yes it is!
可以在应用程序之间共享数据吗?是的!
Use UIPasteBoard available from iOS 3.0, documentation is available here. Apple docs say:
使用可从 iOS 3.0 获得的 UIPasteBoard,文档可在此处获得。苹果文件说:
The UIPasteboard class enables an application to share data within the application or with another application using system-wide or application-specific pasteboards.
UIPasteboard 类使应用程序能够使用系统范围或应用程序特定的粘贴板在应用程序内或与另一个应用程序共享数据。
It is also possible to share data between apps in the keychain, although the data is primarily meant to be passwords and such, anything serializable could be stored. Hereis a Stack Overflow question about that.
也可以在钥匙串中的应用程序之间共享数据,尽管数据主要是密码等,任何可序列化的东西都可以存储。 这是一个关于这个的 Stack Overflow 问题。
回答by PawanShakya
You can use Custom URL scheme to access data from one app to another. Follow below mentioned link for more info -
您可以使用自定义 URL 方案来访问从一个应用程序到另一个应用程序的数据。按照下面提到的链接了解更多信息 -
http://iosdevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html
http://iosdevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html
回答by MasterBeta
Mention that sharing data between apps via UIPasteBoard only works for apps in the same application group in iOS7. As apples says:
提到通过 UIPasteBoard 在应用程序之间共享数据仅适用于 iOS7 中同一应用程序组中的应用程序。正如苹果所说:
+[UIPasteboard pasteboardWithName:create:] and +[UIPasteboard pasteboardWithUniqueName] now unique the given name to allow only those apps in the same application group to access the pasteboard. If the developer attempts to create a pasteboard with a name that already exists and they are not part of the same app suite, they will get their own unique and private pasteboard. Note that this does not affect the system provided pasteboards, general, and find.
+ [UIPasteboard pasteboardWithName:创建:]和+ [UIPasteboard pasteboardWithUniqueName现在唯一给定的名称,只允许同一个应用程序组中的应用程序访问的纸板。如果开发人员尝试使用已存在的名称创建粘贴板,并且它们不属于同一应用程序套件,则他们将获得自己独特的私有粘贴板。请注意,这不会影响系统提供的粘贴板、通用和查找。
回答by Kamil
You can use https://github.com/burczyk/Camouflageto read and write NSData to iOS Camera Roll as .bmp file and share it between apps :)
您可以使用https://github.com/burczyk/Camouflage将 NSData 作为 .bmp 文件读取和写入 iOS 相机胶卷并在应用程序之间共享:)
Brand new solution!
全新的解决方案!
回答by QED
If you don't mind hitting the network, you could implement a custom web service to do it, or use some cloud service. iCloud itself will not be of much use here; it only allows you to share data between the same app on different devices. You can read about iCloud here.
如果您不介意访问网络,您可以实现自定义 Web 服务来执行此操作,或者使用一些云服务。iCloud 本身在这里用处不大;它只允许您在不同设备上的同一个应用程序之间共享数据。您可以在此处阅读有关 iCloud 的信息。
Without using the network, you can exploit "fast app switching" to transfer a limited amount of data between apps via URL encoding. The actual amount of data transferable I don't know, but it would be very limited I'm sure.
在不使用网络的情况下,您可以利用“快速应用程序切换”通过 URL 编码在应用程序之间传输有限数量的数据。我不知道可传输的实际数据量,但我敢肯定它会非常有限。
JugsteR and baudot's answers are best in this case.
在这种情况下,JugsteR 和 baudot 的答案是最好的。
回答by dbrajkovic
No. You would have to use some cloud solution.
不,您必须使用一些云解决方案。