Move data/images between two iOS apps using custom URL handler

只愿长相守 提交于 2019-11-29 14:38:06

问题


After googling around and searching SO for a while, I stil couldn't find an answer -

I've wondered, How could I transfer data between two of my apps using custom URL handlers? Specifically images or an NSData object for that matter.

I know about being able to open specific parts of my app using custom handlers such as myapp1://start , myapp2://start , but I'm not sure how to go on transferring large amounts of data (~80k) through these handlers.

Would love to hear any creative solutions :)

p.s. The solution should be iOS >= 4.3 Compatible


回答1:


Use the custom URL handlers in combination with UIPasteboard. Save something from your first app (say, an image) to the general pasteboard, like so:

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

Then use the custom URL schemes to switch apps.

Then retrieve your image from within the new app when you need it:

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

Battle-tested. ; )




回答2:


One Solution could be:

  • Implement a Webserver
  • Open up your second app via the custom url scheme with the IP-adress and the port of your custom web server included in the url
  • Add the route or parameters to your image also to your URL

Download and enjoy your photo :-)

Another Solution:

  • Start a Bonjour service
  • in a network the second app can find this service
  • do some magic to pass the data in between the apps

EDIT: BETTER OTHER SOLUTION:

Just found another, but much more efficient way to do exchanges of larger data sets:
It is called UIPasteboard

Best reference for that:
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIPasteboard_Class/Reference.html

And another resource:
http://kmithi.blogspot.in/2012/03/sharing-data-among-ios-applications.html

That should do it. For a webserver: There are tons of implementations found using Google




回答3:


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



回答4:


Well, as far as I can tell ~80k is too much for a custom URL handler. But you could try to Base64-encode your data and append it to the custom URL. But I doubt that it will work with lots of data.

You could also have a look on the UIDocumentInteractionController, which is intended to open files with other applications that support them. This is what the Mail application does when you open an attachment with another app.

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




回答5:


using custom URL handler

it is specified, so it will not meet your specification.

But if you don't care about that, I would write to internal memory (file system)the 80k image, even if 80Mb and pass the "url" for the other app.

Big ego, and 0 effort in research:

copy to images folder

IPC communication via url

and my solution doesn't work... I am not giving fish, just teaching hot to get a fish.



来源:https://stackoverflow.com/questions/12276827/move-data-images-between-two-ios-apps-using-custom-url-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!