Transfer information between apps in iOS development 2--uipasteboard

Source: Internet
Author: User

take a look at the following Uipasteboard usage-(void) Writevalueinappone {uipasteboard*myuipasteboard =[Uipasteboard Generalpasteboard]; printf ("myuipasteboard.string%s\n", [Myuipasteboard.stringutf8string]); Myuipasteboard.string=MyLabel.Text;} -(void) getvalueinapptwo {uipasteboard*myuipasteboard =[Uipasteboard Generalpasteboard]; printf ("myuipasteboard.string%s\n", [Myuipasteboard.stringutf8string]); Mytextfield.text= Myuipasteboard.string; }


2. Detailed Usage: http://blog.csdn.net/zhangao0086/article/details/7580654 (ext.)

Sometimes we may need to copy the text on the Uilabel, or the Uiimageview picture, and Uilabel and Uiimageview do not respond to the touch event or copy, then we need to implement a replicable uilabel ourselves. Add a new class to inherit from Uilabel:

    1. @interface Uicopylabel:uilabel
    2. @end
    1. #import "UICopyLabel.h"
    2. @implementation Uicopylabel
    3. @end

In order to receive the event (which can be the first responder), we need to override a method:

    1. -(BOOL) canbecomefirstresponder{
    2. return YES;
    3. }

There are also two methods that need to be covered for replication operations:

    1. Features of "Feedback" care
    2. -(BOOL) Canperformaction: (SEL) Action Withsender: (ID) sender{
    3. return (Action = = @selector (copy:));
    4. }
    5. Implementation for copy
    6. -(void) copy: (ID) sender{
    7. Uipasteboard *pboard = [Uipasteboard Generalpasteboard];
    8. pboard.string = Self.text;
    9. }
With the above three methods, we can handle copy, of course, in case of receiving the event:
  1. Uilabel The default is not to receive events, we need to add the touch event ourselves
  2. -(void) attachtaphandler{
  3. self.userinteractionenabled = YES; //master switch for user interaction
  4. UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Handletap:)];
  5. touch.numberoftapsrequired = 2;
  6. [Self addgesturerecognizer:touch];
  7. [Touch release];
  8. }
  9. Binding events
  10. -(ID) initWithFrame: (CGRect) frame
  11. {
  12. self = [super Initwithframe:frame];
  13. if (self) {
  14. [Self attachtaphandler];
  15. }
  16. return self;
  17. }
  18. Ditto
  19. -(void) awakefromnib{
  20. [Super awakefromnib];
  21. [Self attachtaphandler];
  22. }

We are ready to receive the event! Since I set the tap number to 2 above, so I need to double-click to capture, next, we need to deal with this tap, so that the menu bar pop out:

    1. -(void) Handletap: (uigesturerecognizer*) recognizer{
    2. [Self becomefirstresponder];
    3. Uimenucontroller *menu = [Uimenucontroller Sharedmenucontroller];
    4. [Menu SetTargetRect:self.frame InView:self.superview];
    5. [Menu Setmenuvisible:yes Animated:yes];
    6. }

In this way, a replicable Uilabel is born! It can handle receive clicks, pop-up menu bars, handle copy, which is a very common replicable control.

-----------------------------------------the wretched dividing line-----------------------------------------

Next we make a replicable uiimageview, create a new Viewcontroller, put two imageview, and show different graphs by default:

Then copy the above code directly over to three places:

  1. -(BOOL) Canperformaction: (SEL) Action Withsender: (ID) sender{
  2. return (Action = = @selector (copy:) | | action = = @selector (paste:));
  3. }
  4. -(void) copy: (ID) sender{
  5. Uipasteboard *pboard = [Uipasteboard Generalpasteboard];
  6. Pboard.image = Self.image;
  7. }
  8. -(void) paste: (ID) sender{
  9. Uipasteboard *pboard = [Uipasteboard Generalpasteboard];
  10. Self.image = Pboard.image;
  11. }
-----------------------------------------the wretched dividing line-----------------------------------------
Uipasteboard can not only communicate within the application, but also communicate between applications, you should have seen, such as I copy a URL, and then open Safari, paste into the address bar, and we can "quietly" between the application of communication, sharing data.

Note: the "whisper" here simply says that other applications do not know, and the system is allowed.

We use the template single View application to create two simple works, one called Pasteboardwrite, the other called Pasteboardread, the interface is as follows:

In the Pasteboardwrite, click "Write" after the text in the TextField to the pasteboard, and then switch to Pasteboardread when the display. If we only want to give "one of our own", we can not use the system of the universal pasteboard, we need to create a:

    1. Need to provide a unique name, generally use the inverted domain name: com.mycompany.myapp.pboard
    2. The following parameter indicates whether, if not present, creates a
    3. Uipasteboard *PB = [Uipasteboard pasteboardwithname:@"Testboard" create:yes];
Using this clipboard, we can put the text in, and then read it in another app, some of the common types have been set to properties:

In addition, if the data types that are capable of converting to plist (NSString, Nsarray, Nsdictionary, NSDate, NSNumber, and Nsurl), we can call Setvalue:forpasteboardtype: method to store data, other types can only call Setdata:forpasteboardtype: Method (plist data type can also be used), similar to this:

    1. Nsdictionary *dict = [nsdictionary dictionaryWithObject:textField.text forkey:@"content"];
    2. NSData *dictdata = [Nskeyedarchiver archiveddatawithrootobject:dict];
    3. [PB setdata:dictdata forpasteboardtype:@"MyType"];
Get is similar to this:

    1. Uipasteboard *PB = [Uipasteboard pasteboardwithname:@"Testboard" create:yes];
    2. Nsdictionary *dict = [nskeyedunarchiver unarchiveobjectwithdata:[pb dataforpasteboardtype:@"MyType"]];
    3. Caption.text = [dict objectforkey:@"content"];

A pasteboardtype is mentioned above, which is a uniform type identifier (Uniform type Identifier UTI) that helps the app to get the data it can handle. For example, you can only handle the text paste, that gives you a uiimage is obviously useless. You can use a common UTI, or you can use any character, and Apple recommends using the inverted domain name plus the type name: Com.myCompany.myApp.myType.

With your own clipboard, you can only communicate between your local apps, and sometimes it can make your user experience even better, like that.

UPDATED:
IOS7 modified part of the design of the Uipasteboard.
Before this, you can access the content only if you know the name of any Uipasteboard, and now only the app under the same Cfbundleidentifier identity can share the content. such as Com.mycompany.a and com.mycompany.b, their com.mycompany parts are the same, they can be shared.


Transfer information between apps in iOS development 2--uipasteboard

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.