您的位置:首页 > 移动开发

UIPasteboard Example – Read, Write and Share data between apps

2015-08-10 14:56 501 查看
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.

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.

pasteboardType : is Uniform Type Identifier Ex: kUTTypePlainText, kUTTypeText, kUTTypeJPEG,kUTTypePNG.

To get the list of system declared Uniform Type Identifiers: Click
Here

1.3 APIs for reading data from Pasteboard.



2.WRITING DATA TO GENERATE PASTEBOARD

2.1 Copy Text to General Pasteboard

Text can be copied to Pasteboard using setString / setData.

2.2 Copy Image to General Pasteboard

Image can be copied to Pasteboard using setImage / setData.



3). READING DATA TO GENERAL PASTEBOARD

Text can be read using string attribute/dataForPasteboardType.



4). WRITING DATA TO APP PASTEBOARD

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

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.

NSKeyedUnarchiver is used to decode NSData as NSDictionary.



6).REMOVE PASTEBOARD

Pasteboard data can be removed using method removePasteboardWithName.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: