如何取消UIActivityItemProvider并且不显示活动?

时间:2014-02-26 09:59:03

标签: nsoperation uiactivityviewcontroller uiactivity

我正在使用UIActivityItemProvider子类来提供自定义数据。但有时获取数据失败,我不想提供活动(例如消息编写器)。在[self cancel]方法中尝试了return nil;item,但邮件编辑仍然显示(带有空消息)。

2 个答案:

答案 0 :(得分:5)

如果在从 - (id)项目返回之前关闭UIActivityViewController,它将不会显示用户选择的活动。

要做到这一点,首先需要获取activityViewControllerPlaceholderItem中的activityViewController。在 - (id)项目中,在dispatch_async中运行代码以更新进度并解除我使用promise lib进行的完成/错误。

在UIActivityItemProvider的子类中,执行类似于以下示例的操作。

-(id) activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{ self.avc = activityViewController;
  return NSURL;
}

-(id)item
{ __block BOOL fileProcDone = NO;
  dispatch_async(dispatch_get_main_queue(), ^
  { self.pvc = [[ProgressAlertVC alloc] init];
    [self.vc presentViewController:self.pvc animated:YES completion:nil];
    [[[[self promiseMakeFile]
    progressed:^(float progress)
    { self.pvc.progress = progress;
    }]
    fulfilled:^(id result)
    { [self.pvc dismissViewControllerAnimated:YES completion:^
      { fileProcDone = YES;
      }];
    }]
    failed:^(NSError *error)
    { [self.pvc dismissViewControllerAnimated:YES completion:^
      { [self.vc dismissViewControllerAnimated:YES completion:^
        { fileProcDone = YES;
        }];
      }];
    }];
  });
  while (!fileProcDone)
  { [NSThread sleepForTimeInterval:0.1];
  }; 
  return NSURL;
}
  

这将导致来自活动扩展的控制台日志消息,但只要它们正确处理错误,事情应该没问题。如果你从 - (id)activityViewController:itemForActivityType返回nil:你不会得到控制台错误,但即使你在此时解雇UIActivityViewController也会获得用户选择的活动。

答案 1 :(得分:1)

您只需要调用UIActivityItemProvider的cancel方法。由于UIActivityItemProvider是NSOperation,因此调用cancel将标记操作已取消。

此时,您有几个选项可以实际停止长时间运行的任务,具体取决于您的任务结构。您可以覆盖cancel方法并在那里取消,只需确保拨打[super cancel]即可。第二个选项是检查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)