将UIPasteboard内容粘贴到其他应用程序中

时间:2015-07-20 02:31:47

标签: ios uipasteboard

我使用以下代码将一些文本复制到UIPasteboard

[[UIPasteboard generalPasteboard] setPersistent:YES];
[UIPasteboard generalPasteboard].string = @"Testing";

我能够通过以下方式阅读内容:

NSLog(@"Pasteboard String: %@", [UIPasteboard generalPasteboard].string); 
// which gives "Testing"

但是,我无法在任何其他应用中检索复制的文本。在我的应用程序中复制文本然后粘贴到其他应用程序的正确方法是什么?

使用iOS 8。

1 个答案:

答案 0 :(得分:-1)

  // Save text

      UIPasteboard* board = [UIPasteboard
    pasteboardWithName:@"com.company.wtv" create:YES]; 
    board.persistent=YES; [board setValue:@"123456ccc"
    forPasteboardType:@"com.company.wtv.sharedValue"];

    // Retrive text

        UIPasteboard* board = [UIPasteboard pasteboardWithName:@"com.company.wtv" create:YES];
        board.persistent=YES;
        NSData* result=nil;
        NSString*resultStr=nil;
        result =[board valueForPasteboardType:@"com.company.wtv.sharedValue"];
        resultStr=[[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];// I got resultStr containing
    123456ccc

        NSLog(@"key %@",resultStr);