UIPasteboard Example – Read, Write and Share data between apps

In UIPasteboard Example, I have explained how to read data from UIPasteboard and write data to UIPasteboard and share data between apps.
Pasteboard can be used to store Plain Text, Rich-Text, Image, Email,…etc.

Topics covered in this tutorial:
1).UIPasteboard APIs
2).Writing Data to General Pasteboard
3).Reading Data to General Pasteboard
4).Writing Data to App Pasteboard
5).Reading Data from App Pasteboard
6).Remove a Pasteboard

1.UIPASTEBOARD APIS

1.1 APIs for creating a Pasteboard.

1
2
3
+ ( UIPasteboard *)generalPasteboard;
+ ( UIPasteboard *)pasteboardWithName:( NSString *)pasteboardName create :( BOOL )create;
+ ( UIPasteboard *)pasteboardWithUniqueName;

generalPasteboard : method returns system board which supports general copy-paste operations.
pasteboardWithUniqueName : method is used to create a app pasteboard which is identified by a unique system generated name.
pasteboardWithName: method returns a a pasteboard identified by name. if create flag is TRUE, then pasteboard is created.

Note: Data can be shared between apps using app pasteboard.

 

1.2 APIs for writing data to Pasteboard.

1
2
3
4
5
//setValue method is used to store objects.
- ( void )setValue:( id )value forPasteboardType :( NSString *)pasteboardType;
 
//setData method is used to store raw data
- ( void )setData:( NSData *)data forPasteboardType :( NSString *)pasteboardType;

pasteboardType : is Uniform Type Identifier  Ex: kUTTypePlainTextkUTTypeTextkUTTypeJPEG,kUTTypePNG.
To get the list of system declared Uniform Type Identifiers: Click Here

 

1.3 APIs for reading data from Pasteboard.

1
2
3
4
5
//returns data
- ( NSData *)dataForPasteboardType:( NSString *)pasteboardType;
 
//returns value
- ( id )valueForPasteboardType:( NSString *)pasteboardType;

2.WRITING DATA TO GENERATE PASTEBOARD

2.1 Copy Text to General Pasteboard

Text can be copied to Pasteboard using setString / setData.

1
2
3
4
5
6
7
8
//Method 1
NSString * text= @"Ravi" ;
UIPasteboard * pasteboard=[ UIPasteboard generalPasteboard ];
[pasteboard setString :text];
 
//Method 2
NSData *data = [text dataUsingEncoding :NSUTF 8 StringEncoding];
[pasteboard setData :data forPasteboardType :( NSString *)kUTTypeText];

2.2 Copy Image to General Pasteboard

Image can be copied to Pasteboard using setImage setData.

1
2
3
4
5
6
7
8
 
//Method
NSData *imageData = UIImagePNGRepresentation(image);
[pasteboard setData:imageData forPasteboardType:(NSString *)kUTTypePNG];

3). READING DATA TO GENERAL PASTEBOARD

Text can be read using string attribute/dataForPasteboardType.

1
2
3
4
5
6
7
8
UIPasteboard * pasteboard=[UIPasteboard generalPasteboard];
//Method 1
NSLog(@ "Text =%@" ,[pasteboard string]);
 
//Method 2
NSData * data = [pasteboard dataForPasteboardType:(NSString*)kUTTypeText];
NSString * text =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@ "Text =%@" ,text);

4). WRITING DATA TO APP PASTEBOARD

You can use below method, to write a dictionary to App pasteboard.

1
2
3
4
5
6
7
8
9
10
#define PASTE_BOARD_NAME @"mypasteboard"
#define PASTE_BOARD_TYPE @"mydata"
 
-( void ) writeToPasteBoard :(NSDictionary*) dict
{
     UIPasteboard * pb = [ UIPasteboard pasteboardWithName : PASTE_BOARD_NAME create : YES ];
     [pb setPersistent : TRUE ];
 
     [pb setData :[ NSKeyedArchiver archivedDataWithRootObject :dict] forPasteboardType :PASTE_BOARD_TYPE];
}

By default, Pasteboard created with pasteboardWithName or pasteboardWithUniqueName are not persistent. To make them persistent, set the persistent to TRUE.

NSKeyedArchiver is used to encode NSDictionary as NSData.

5). READING DATA FROM APP PASTEBOARD

You can use below method, to read data from Pasteboard.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
-(NSDictionary*) readFromPasteboard
{
 
     UIPasteboard * pb=[ UIPasteboard pasteboardWithName : PASTE_BOARD_NAME create : NO ];
     NSData * data=[pb valueForPasteboardType :PASTE_BOARD_TYPE];
     NSDictionary * dict;
 
     if (!data) {
         return nil ;
     }
     @try {
         dict = [ NSKeyedUnarchiver unarchiveObjectWithData :data];
     }
     @catch (NSException* exception)
     {
         NSLog( @"Exception: %@" ,exception);
         return nil ;
     }
     return dict;
}

NSKeyedUnarchiver is used to decode NSData as NSDictionary.

6).REMOVE PASTEBOARD

Pasteboard data can be removed using method removePasteboardWithName.

1
[ UIPasteboard removePasteboardWithName :PASTE_BOARD_NAME];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值