使用自定义URL处理程序在两个iOS应用之间移动数据/图像

时间:2012-09-05 07:49:01

标签: ios uiimage nsdata data-transfer

在谷歌搜索并搜索了一段时间之后,我找不到答案 -

我想知道,我如何使用自定义网址处理程序在我的两个应用之间传输数据?特别是图像或NSData对象。

我知道能够使用自定义处理程序打开我的应用程序的特定部分,例如myapp1:// start,myapp2:// start,但我不确定如何继续传输大量数据(~80k) )通过这些处理程序。

很想听到任何有创意的解决方案:)

p.s。解决方案应该是iOS> = 4.3兼容

5 个答案:

答案 0 :(得分:10)

将自定义URL处理程序与UIPasteboard结合使用。将第一个应用程序(例如图像)中的内容保存到常规粘贴板,如下所示:

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
[[UIPasteboard generalPasteboard] setImage:myImage];

然后使用自定义网址方案切换应用。

然后在需要时从新应用中检索您的图片:

   UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
   UIImage *tempImg = pasteboard.image;

战测试。 ; )

答案 1 :(得分:3)

一种解决方案可能是:

  • 实施网络服务器
  • 使用IP地址和网址中包含的自定义网络服务器端口,通过自定义网址方案打开您的第二个应用
  • 将路线或参数也添加到您的图片

下载并欣赏您的照片: - )

另一种解决方案:

  • 启动Bonjour服务
  • 在网络中,第二个应用可以找到此服务
  • 做一些魔术来传递应用之间的数据

编辑: 更好的解决方案:

刚刚找到了另一种更有效的方式来交换更大的数据集:
它被称为UIPasteboard

最佳参考:
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIPasteboard_Class/Reference.html

另一种资源:
http://kmithi.blogspot.in/2012/03/sharing-data-among-ios-applications.html

应该这样做。 对于网络服务器:使用Google

发现了大量实现

答案 2 :(得分:1)

     [http://www.codeproject.com/Articles/43447/How-to-Use-UIPasteBoard-to-Implement-Custom-Copy-a][1]

        As you know many of the controls in UIKit now come pre-loaded with the ability to copy and paste text. You can also use this new ability in your own apps to copy and paste other things including: images, SQLite databases, text or any file. This is a great way to share data between your apps if you want to provide users with a suite of apps with integrated functionality.



     CopyFrom Source Code
        -(IBAction)copyImageToPasteBoard{
            UIPasteboard *appPasteBoard = [UIPasteboard pasteboardWithName:@"CopyFrom" 
                     create:YES];
            appPasteBoard.persistent = YES;

            NSData *data = UIImagePNGRepresentation([UIImage imageNamed:@"Old-Time-Photo.jpg"]);
            [appPasteBoard setData:data forPasteboardType:@"com.appshop.copyfrom.imagedata"];
        }

        -(IBAction)copyStringToPasteBoard{
            UIPasteboard *appPasteBoard = [UIPasteboard pasteboardWithName:@"CopyFrom" 
                     create:YES];
            appPasteBoard.persistent = YES;
            [appPasteBoard setString:textView.text];
        }
        PasteTo Source Code
        -(IBAction)pasteImageToPasteBoard{
            UIPasteboard *appPasteBoard = [UIPasteboard pasteboardWithName:@"CopyFrom"
                     create:YES];
            NSData *data = [appPasteBoard dataForPasteboardType:@"com.appshop.copyfrom.imagedata"];
            imageView.image = [UIImage imageWithData:data];
        }

        -(IBAction)pasteStringToPasteBoard{
            UIPasteboard *appPasteBoard = [UIPasteboard pasteboardWithName:@"CopyFrom" 
                     create:YES];
            textView.text = [appPasteBoard string];
        }


    Using UIPasteBoard in iPhone programming is amazingly simple and opens up some possibilities that we did not have a year ago. To use UIPasteBoard you simply create a instance using pasteboardWithName, put stuff into the paste board and then set the persistant property equal to YES. Then any app can get a reference to your paste board and use the data contained within. You can use it for simple strings and even data that you can put into NSData like SQLite databases.

答案 3 :(得分:0)

  

使用自定义网址处理程序

已指定,因此不符合您的规格。

但是如果你不关心这个,我会写80k图像的内存(文件系统),即使是80Mb并传递给另一个app的“url”。

大自我,努力研究:

copy to images folder

IPC communication via url

我的解决方案不起作用......我不是给鱼,只是教热鱼来钓鱼。

答案 4 :(得分:0)

好吧,据我所知〜对于自定义URL处理程序来说,80k太多了。但您可以尝试对数据进行Base64编码并将其附加到自定义URL。但我怀疑它可以处理大量数据。

您还可以查看UIDocumentInteractionController,它旨在打开包含支持它们的其他应用程序的文件。这是当您使用其他应用程序打开附件时Mail应用程序执行的操作。

https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIDocumentInteractionController_class/Reference/Reference.html#//apple_ref/doc/uid/TP40009304