Program Tip

둘 이상의 iPhone 응용 프로그램간에 데이터 공유

programtip 2020. 10. 15. 21:30
반응형

둘 이상의 iPhone 응용 프로그램간에 데이터 공유


동일한 장치의 두 응용 프로그램간에 데이터를 공유 할 수 있습니까?

아니면 다른 응용 프로그램이 내 응용 프로그램의 정보 / 데이터를 사용하거나 다른 방식으로 사용하도록 허용 할 수 있습니까?

예를 들어 첫 번째 응용 프로그램은 이벤트 관리 용이며 일부 이벤트를 저장하는 데 사용합니다. 두 번째 응용 프로그램은 미리 알림을위한 것으로, 이벤트에 대해 상기시키기 위해 다른 응용 프로그램에서 데이터를 가져옵니다.

이것은 실제 시나리오가 아닌 단순한 예입니다.


역사적으로 iPhone은 앱 간의 데이터 공유를 방지하려고 노력했습니다. 아이디어는 다른 앱의 데이터를 얻을 수 없다면 그 앱에 나쁜 일을 할 수 없다는 것입니다.

최근 출시 된 IOS에서는 약간 느슨해졌습니다. 예를 들어 iOS 프로그래밍 가이드에는 이제 한 앱이 특정 URL 접두사를 요청하고 다른 앱이 해당 URL을 참조하도록하여 앱간에 데이터를 전달하는 섹션이 있습니다. 따라서 웹 서버가 "http : //"URL에 응답하는 것과 동일한 방식으로 "event : //"URL에 응답하도록 이벤트 앱을 설정할 수 있습니다.

이러한 접근 방식에 대한 Apple의 문서는 여기에 있습니다 .

"사용자 정의 URL 체계 구현"을 살펴보십시오.


에서 샌드 박스 아이폰 OS 개발자가를 통해 직접 데이터를 공유 할 수 있기 때문에 응용 프로그램 사이 아이폰 OS 개발 데이터를 공유의 세계 어려울 수있는 파일 시스템 , 그들은 그들의 애플리케이션을위한 다른 해결책을 찾을 필요가있다. 몇 가지 일반적인 솔루션은 다음과 같습니다.

  • UIDocumentInteractionController

  • UIActivityViewController

  • 공유 키 체인 접근

  • 맞춤 URL 체계

  • 웹 서비스

  • iCloud API


UIDocumentInteractionController :

사용자가 특정 문서 UTI (Uniform Type Identifier) 를 처리 할 수있는 것으로 등록 된 다른 애플리케이션에서 문서를 열 수 있도록합니다 .

UIDocumentInteractionController는 과거에 기기의 다른 애플리케이션에서 문서를 여는 수단 (예 : 메일 앱에서 이메일 첨부 파일 열기)으로 사용되었습니다.

UIDocumentInteractionController

불행히도 UIDocumentInteractionController의 UI는 6 개의 애플리케이션 만 표시합니다 .

귀하의 애플리케이션이 목록에 표시된다는 보장은 없습니다. UIDocumentInteractionController는 더 이상 사용되지 않지만 UIActivityViewController 는 iOS 6.0부터 더 유연한 대체 기능을 제공합니다.

가용성 : iOS 3.2 이상

장점 :

  • 다양한 애플리케이션과 공통 데이터 유형을 공유 할 수 있습니다.

단점 :

  • UIDocumentInteractionController로 전송되는 데이터 유형을 제어 할 수 있지만 대상은 제어 할 수 없습니다.

  • 추가 사용자 상호 작용이 필요합니다.

  • 제한된 수의 데이터 대상으로 인해 응용 프로그램이 목록에 표시되지 않을 수 있습니다.


UIActivityViewController :

사용자가 데이터 배열로 여러 작업을 수행 할 수 있습니다.

예를 들어 인쇄, 이메일, 복사, 소셜 미디어에 게시하거나 다른 응용 프로그램에서 열 수 있습니다.

사용자에게 사용자 지정 서비스를 제공하기 위해 고유 한 UIActivity 하위 클래스를 만들 수 있습니다.

UIActivityController

가용성 : iOS 6.0 이상

장점 :

  • 다양한 애플리케이션 및 소셜 미디어와 공통 데이터 유형을 공유하는 데 적합합니다.

  • 활동에 적용 할 항목 배열을 제공 할 수 있습니다. 개체는 UIActivityItemSource 프로토콜을 준수해야합니다.

  • 제외 된 활동 유형을 설정할 수 있습니다.

  • 페이징 UI는 UIDocumentInteractionController보다 더 많은 데이터 대상을 허용합니다.

단점 :

  • 공통 데이터 유형의 "열기…"대상을 제한하려면 사용자 정의 활동 유형을 정의해야합니다.

  • 추가 사용자 상호 작용이 필요합니다.


공유 키 체인 액세스 :

응용 프로그램 제품군의 일부인 다른 응용 프로그램이 액세스 할 수있는 공유 키 체인에 데이터를 안전하게 저장할 수 있습니다.

키 체인 액세스를 공유하는 모든 애플리케이션은 동일한 앱 ID 접두사를 사용해야합니다.

실행중인 공유 키 체인 액세스의 예입니다. Apple의 GenericKeychain 샘플 코드를 참조하십시오 .

공유 키 체인

가용성 : iOS 3.0 이상

장점 :

  • 데이터에 대한 보안 액세스.

단점 :

  • 공통 앱 ID 접두사를 공유하는 애플리케이션간에 만 데이터를 공유 할 수 있습니다.

  • iOS 시뮬레이터의 키 체인 API는 iOS 기기와 다른 API가있는 OS X에서 제공됩니다.


맞춤 URL 체계 :

단순 URL을 사용하여 애플리케이션간에 데이터를 전달할 수 있습니다.

맞춤 URL 체계

가용성 : iOS 3.0 이상

장점 :

  • 네트워크 연결이 필요하지 않습니다.
  • 이스케이프 된 합법적 인 URL로 쉽게 인코딩 할 수있는 소량의 데이터에 적합합니다.

단점 :

  • 데이터를 이스케이프 된 법적 URL로 인코딩해야합니다.

    참고 : base64 인코딩은 직렬화 가능한 데이터를 문자열 값으로 바꾸는 데 일반적으로 사용되었습니다. 그러나 base64 문자열에는 URL에서 사용할 수없는 문자가 포함될 수 있습니다. base64url 사용을 고려할 수 있습니다. 자세한 내용은 URL 및 파일 이름 안전 알파벳사용한 Base 64 인코딩 을 참조하십시오.


iCloud API :

모두가 iCloud, 장단점에 대해 알고 있으므로 더 이상 설명하지 않습니다.

그러나 하나의 장치 내에서 응용 프로그램간에 데이터를 공유 할 수있는 방법에 대해 몇 가지 해결 방법이 있는지 묻습니다.

iCloud

iCloud에 사용되는 식별자가 번들 식별자와 다르기 때문에 가능하므로 이미지, 동영상, 기타 문서 공유가 가능합니다.

자세한 내용은이 주제에 대한 토론을 참조하십시오.


웹 서비스:

타사 (예 : Dropbox) 또는 맞춤형 웹 서비스를 통해 데이터를 동기화합니다.

웹 서비스

가용성 : iOS 2.0 이상

장점 :

  • 많은 양의 데이터를 공유하거나 배포하는 데 유용합니다.

단점 :

  • 네트워크 연결이 필요합니다.
  • 웹 서비스 구현 오버 헤드.

참고


에서 아이폰 OS 8 내가 사용에 성공적으로 액세스 같은 폴더했습니다 " 앱 그룹 기능을. "나는 @siejkowski의 대답을 확장하고있다.

참고 : 동일한 개발자 계정에서만 작동합니다.

이를 위해 아래 단계를 따라야합니다.

  1. 먼저 개발자 계정에서 "앱 그룹"을 활성화합니다.
  2. 프로비저닝 프로필을 생성합니다. 그리고 그것을 사용하십시오.

이제 두 개의 앱을 만들어야합니다. 샘플 이름

  1. Demo_Share_One
  2. Demo_Share_Two

Now We are copying images from Demo_Share_One to 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.

You have to Take Group Name which was set to your developer account.lets say group.filesharingdemo.

Add Below method in Both apps to get Relative path of sharing folder 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

-(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_Two to access those images

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.

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.

Thanks to Apple for Share Elements from your own apps functionality. Happy Coding. :)


Since iOS 8 you can easily share data between apps as long as they are in the common App Group.

Apple documentation best explains it in the Extensions context: https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html

Basically, you need to:

  1. Define App Group ID (in Certificates, Identifiers & Profiles section of Member Center for your Apple Developer Program.
  2. Enable App Groups capability specifying the above App Group ID for each app that needs to communicate (cen be done either in Xcode: Target -> Capabilities or at Member Center).
  3. Use one of two APIs for shared container access.

First API is based on 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:

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.

In case of folder, please write/read atomically just to ensure no deadlocks are possible.


Share data between apps possible? Yes it is!

Use UIPasteBoard available from iOS 3.0, documentation is available here. Apple docs say:

The UIPasteboard class enables an application to share data within the application or with another application using system-wide or application-specific pasteboards.

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. Here is a Stack Overflow question about that.


You can use Custom URL scheme to access data from one app to another. Follow below mentioned link for more info -

http://iosdevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html


Mention that sharing data between apps via UIPasteBoard only works for apps in the same application group in iOS7. As apples says:

+[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.


You can use https://github.com/burczyk/Camouflage to read and write NSData to iOS Camera Roll as .bmp file and share it between apps :)

Brand new solution!


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.

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.

JugsteR and baudot's answers are best in this case.


No. You would have to use some cloud solution.

참고URL : https://stackoverflow.com/questions/9425706/share-data-between-two-or-more-iphone-applications

반응형