iOS - UIPasteboard not working outside app

跟風遠走 提交于 2019-12-12 08:48:43

问题


I think this is more of an SDK flaw than my apps' but recently I've been trying to use UIPasteboard to copy strings from my app and it works fine to pasting somewhere when I'm inside the app.

When I jump to another app by either pressing the home button or anything like that, I simply don't have the option to paste the copied content.

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setString: @"blah" ];
NSLog(@"%@", pasteboard.string);

It will print "blah" in this case, and whenever I quick touch a textfield, it will show the paste option. But if I go to Safari, Notes or Mail It doesn't show me that option.

Also, If I copy something from mail and go to my app, I won't see the paste option aswell...


回答1:


I have similar problem. That might be some conflicts with some third party library. I found that when I remove Flurry Analytics , everything is fine. I guess the lib does something on "EnterBackground" event.

You could try to "clean up" your application. remove function call on AppDelgate's enterbackground delegate.

I mean your code or third part code might do sth during "DidEnterBackground" that mass up your clipboard. try do not code anything on this:

  • (void)applicationDidEnterBackground:(UIApplication *)application{}

Also try to remove third party code which need you to invoke in : -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions –




回答2:


To do a persistent pasteboard between applications you must use

UIPasteboard *pasteboard = [UIPasteboard pasteboardWithName:pasteboardIdentifier create:YES];
[pasteboard setPersistent:YES];
[pasteboard setString:string];



回答3:


I was able to restore pasteboard functionality by going back to Flurry 2.8.4. Flurry 3.0.2 and 3.0.3 somehow disabled copy/paste support with external apps like Notepad.




回答4:


It seems that Flurry solved this problem by releasing the 3.0.4

Too bad, my users' complaints flooded my mailbox...




回答5:


// 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);


来源:https://stackoverflow.com/questions/8402374/ios-uipasteboard-not-working-outside-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!