13

これを行う方法はありますか?起動時にオブジェクトを登録しますが、オブジェクトをUIPasteboardChangedNotificationバックグラウンドに送信して (たとえば) Safari を開いてテキストをコピーすると、ハンドラーが呼び出されません。(今のところシミュレーターのみを使用しています)。

私は両方を使用しました:

[[NSNotificationCenter defaultCenter] addObserver:self 
    selector:@selector(pasteboardNotificationReceived:) 
    name:UIPasteboardChangedNotification 
    object:[UIPasteboard generalPasteboard]];

と:

[[NSNotificationCenter defaultCenter] addObserver:self 
    selector:@selector(pasteboardNotificationReceived:) 
    name:UIPasteboardChangedNotification 
    object:nil ];

ハンドラーを登録します。

4

1 に答える 1

12

私も同じ問題を抱えていました。プロパティのUIPasteboardクラスリファレンスドキュメントによるとchangeCount(強調は私のものです):

ペーストボードの内容が変更されるたびに (具体的には、ペーストボード項目が追加、変更、または削除されると)、UIPasteboard はこのプロパティの値を増やします。変更カウントをインクリメントした後、UIPasteboard は UIPasteboardChangedNotification (追加と変更の場合) および UIPasteboardRemovedNotification (削除の場合) という名前の通知を投稿します。...アプリケーションが再アクティブ化され、別のアプリケーションがペーストボードの内容を変更した場合にも、このクラスは変更カウントを更新します。ユーザーがデバイスを再起動すると、変更カウントはゼロにリセットされます。

UIPasteboardChangedNotificationこれを読んで、アプリが再アクティブ化されると、アプリケーションが通知を受け取ることを意味していました。changeCountただし、注意深く読むと、アプリが再アクティブ化されたときに更新されるのは のみであることがわかります。

changeCountアプリのデリゲートでペーストボードを追跡し、アプリがバックグラウンドにある間に変更されたことがわかったときに予想される通知を投稿することで、これに対処しましchangeCountた。

アプリ デリゲートのインターフェイスで:

NSUInteger pasteboardChangeCount_;

アプリ デリゲートの実装では、次のようになります。

- (BOOL)application:(UIApplication*)application
    didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
  [[NSNotificationCenter defaultCenter]
   addObserver:self
   selector:@selector(pasteboardChangedNotification:)
   name:UIPasteboardChangedNotification
   object:[UIPasteboard generalPasteboard]];
  [[NSNotificationCenter defaultCenter]
   addObserver:self
   selector:@selector(pasteboardChangedNotification:)
   name:UIPasteboardRemovedNotification
   object:[UIPasteboard generalPasteboard]];

  ...
}

- (void)pasteboardChangedNotification:(NSNotification*)notification {
  pasteboardChangeCount_ = [UIPasteboard generalPasteboard].changeCount;
}

- (void)applicationDidBecomeActive:(UIApplication*)application {
  if (pasteboardChangeCount_ != [UIPasteboard generalPasteboard].changeCount) {
    [[NSNotificationCenter defaultCenter] 
     postNotificationName:UIPasteboardChangedNotification
     object:[UIPasteboard generalPasteboard]];
  }
}
于 2011-03-15T17:15:48.940 に答える