iOS 9 UIPasteboard won't work in the background

前端 未结 1 595
滥情空心
滥情空心 2021-01-17 08:53

iOS 9\'s [UIPasteboard generalPasteboard].string will become null when the app is in the background running a background task or Today widget.

Can\'t we

相关标签:
1条回答
  • 2021-01-17 09:10

    Can you explain where do you launch generalPasteboard?.

    This is what I would do:

    In your app delegate's applicationdidBecomeActive method put in this code:

    [[NSNotificationCenter defaultCenter] postNotificationName:@"appDidBecomeActive" object:nil];
    

    Next , in your current active view controller's init method subscribe to the notification.

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(returnFromBg)        
                                                 name:@"appDidBecomeActive" 
                                                 object:nil];
    
    - (void)returnFromBg {
           UIPasteboard *appPasteBoard = [UIPasteboard generalPasteboard];
           yourTextField.text = [appPasteBoard string;
    }
    

    PS Don't forgot to remove the observer when the view controller is removed:

    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    0 讨论(0)
提交回复
热议问题