2010-11-21 6 views
10

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

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

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

と:

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

を私のハンドラを登録します。

+0

あなたは今まで、これが解決したのですか?私もこれを達成しようとしています。 http://cl.ly/69a4あなたが答えを見つけたら、それを私と共有してもよろしいですか? – Frankrockz

答えて

11

私は同じ問題がありました。 changeCountプロパティのUIPasteboard Class Referenceドキュメント(強調は私です)によれば:

たび台紙の特異的変化の内容は、ペーストボードアイテムが追加、変更、またはされ除去さ-UIPasteboardは、このプロパティの値をインクリメントします。変更カウントをインクリメントした後、UIPasteboardは、UIPasteboardChangedNotification(追加および変更の場合)およびUIPasteboardRemovedNotification(削除の場合)という通知をポストします。 ...クラスはまた、アプリケーションが再アクティブ化し、別のアプリケーションがペーストボードの内容を変更したときに、変更カウントを更新します。ユーザーがデバイスを再起動すると、変更回数がゼロにリセットされます。

これは私のアプリが再アクティブ化されたときに私のアプリケーションがUIPasteboardChangedNotificationの通知を受け取ることを意味するために読んだものです。しかし、慎重に読んで、changeCountのみが更新されていることが明らかになりました。

アプリをバックグラウンドに変更したときに、changeCountが変更されたことがわかったら、私のアプリデリゲートでペーストボードのchangeCountを追跡し、予期した通知を送信してこれを処理しました。

​​

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

- (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]]; 
    } 
} 
+0

これは機能しますが、このソリューションはアプリがバックグラウンドのときに通知を監視することはできません。 –

関連する問題