iOS 7 之Airdrop 分享Plain text and attributed string data

接上一篇 先介紹分享文字的 地址點擊打開鏈接


OpenStringViewController.mand modify it as shown below. Each line notedbelow should be added to the top of the appropriate method: 


  • -  (void)viewDidLoad{

    // Update object to be shared.

    self.objectsToShare=@[self.textView.text];

    ... the rest of the code ...

    }

  • -  (void)textViewDidBeginEditing:(UITextView*)textView{

    self.objectsToShare=@[textView.text];

    ... the rest of the code ...

    }

  • -  (BOOL)textView:(UITextView*)textViewshouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
    {

    self.objectsToShare=@[textView.text];

    ... the rest of the code ...

    }

- (void)textViewDidEndEditing:(UITextView*)textView{

self.objectsToShare=@[textView.text];

... the rest of the code ...


All you are doing here is setting and updatingobjectsToShareon the superclass incritical places. The object you’ll share is simply theNSStringtext content of thetext view.

Build and run your app; enter some text in theStringtab and try to send it viaAirDrop with another device. When the transfer completes, the receiving device willopen the Notes.app, and save the transferred text as a new note, as shown below: 


OpenAttributedStringController.mand modify the following methods asindicated below: 


  • -  (void)viewDidLoad{

    ... add this before [super viewDidLoad] ...

    self.objectsToShare=@[self.textView.attributedText];[superviewDidLoad];

    }

  • -  (void)textViewDidBeginEditing:(UITextView*)textView{

    ... the rest of the code ...

    self.objectsToShare=@[textView.attributedText];}

  • -  (BOOL)textView:(UITextView*)textViewshouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
    {

    ... before return YES ...

    self.objectsToShare=@[attrStr];

    return YES;}

- (void)textViewDidEndEditing:(UITextView*)textView{

self.objectsToShare=@[textView.attributedText];

... the rest of the code ...

}


The changes here are similar to the code for plain text, except you’re using theattributedTextproperty as the object to send via AirDrop.

Build and run your app again; type some text into the Attributed tab and be sure toinclude the name of a color or two to exercise the text highlighting. The apprecognizes red, blue, green, purple, brownandyellowin its current state. Feelfree to explore the code and add support for more colors




注:

 AirDrop does not fully supportNSAttributedString. That means on the receiving device you will get a plaintext, which by default opens inNotes.app. If you need to transferattributions, you need to wrap your NSAttributedString in a custom object,save it with a custom file type, and pass the file URL to AirDrop. On thereceiving device, you need to register for your custom file type and take itfrom there. 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章