UIActivityItemProvider
サブクラスを使用してカスタム データを提供しています。しかし、データの取得に失敗し、アクティビティを表示したくない場合があります (メッセージ コンポーザーなど)。メソッドで試し[self cancel]
てみreturn nil;
ましitem
たが、メッセージ コンポーザはまだ表示されます (空のメッセージが表示されます)。
2 に答える
1
cancel
UIActivityItemProviderのメソッドを呼び出すだけです。UIActivityItemProvider は NSOperation であるため、呼び出すcancel
と操作がキャンセルされたとマークされます。
その時点で、タスクの構造に応じて、実行時間の長いタスクを実際に停止するオプションがいくつかあります。メソッドをオーバーライドしてcancel
そこでキャンセルすることもできますが、必ず呼び出し[super cancel]
てください。2 番目のオプションは、メソッドisCancelled
内の値をチェックすることです。item
アイテム プロバイダーの例
import UIKit
import Dispatch
class ItemProvider: UIActivityItemProvider {
override public var item: Any {
let semaphore = DispatchSemaphore(value: 0)
let message = "This will stop the entire share flow until you press OK. It represents a long running task."
let alert = UIAlertController.init(title: "Hello", message: message, preferredStyle: .alert)
let action = UIAlertAction.init(title: "OK", style: .default, handler:
{ action in
semaphore.signal()
})
let cancel = UIAlertAction.init(title: "CANCEL", style: .destructive, handler:
{ [weak self] action in
self?.cancel()
semaphore.signal()
})
alert.addAction(action)
alert.addAction(cancel)
//Truly, some hacking to for the purpose of demonstrating the solution
DispatchQueue.main.async {
UIApplication.shared.delegate?.window??.rootViewController?.presentedViewController!.present(alert, animated: true, completion: nil)
}
// We can block here, because our long running task is in another queue
semaphore.wait()
// Once the item is properly cancelled, it doesn't really matter what you return
return NSURL.init(string: "blah") as Any
}
}
ビュー コントローラーで、次のような共有アクティビティを開始します。
let provider = ItemProvider.init(placeholderItem: "SomeString")
let vc = UIActivityViewController.init(activityItems: [provider], applicationActivities: nil)
self.present(vc, animated: true, completion: nil)
于 2016-11-02T01:20:05.730 に答える