Ios UIActivityViewController UIActivityViewControllerCompletionWithItemsHandler 列表项

Ios UIActivityViewController UIActivityViewControllerCompletionWithItemsHandler 列表项,ios,swift,uiactivityviewcontroller,Ios,Swift,Uiactivityviewcontroller,对于在iOS 8中运行的应用程序,我需要为UIActivityViewController编写一个完成处理程序,以捕获用户选择的“共享”方法的结果 这是我到目前为止的一段代码。我的问题是如何设置avc.completionWithItemsHandler?我相信这很简单,但我看不出来 var activityItems = NSMutableArray() activityItems.addObject("Email or text for 'share' goes here") var av

对于在iOS 8中运行的应用程序,我需要为
UIActivityViewController
编写一个完成处理程序,以捕获用户选择的“共享”方法的结果

这是我到目前为止的一段代码。我的问题是如何设置
avc.completionWithItemsHandler
?我相信这很简单,但我看不出来

var activityItems = NSMutableArray()
activityItems.addObject("Email or text for 'share' goes here")

var avc = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
avc.setValue("Subject for Email", forKey: "Subject")

avc.completionWithItemsHandler = //Here is where I dont know what to do.

self.navigationController?.presentViewController(avc, animated: true, completion: nil)

completionWithItemsHandler类型别名:

typealias UIActivityViewControllerCompletionWithItemsHandler = (String?, Bool, [AnyObject]?, NSError?) -> Void
注意:前面的代码块不会在项目中使用,它只显示所需的闭包类型()

这些是传递到完成处理程序中的参数,供您随意处理,因此完成处理程序如下所示:

avc.completionWithItemsHandler = { activity, success, items, error in 

}
activityVC.completionWithItemsHandler = {(activityType: UIActivityType?, completed: Bool, returnedItems: [Any]?, error: Error?) in

}

present(activityVC, animated: true, completion: nil)

注意:因为我没有阅读问题的“快速”部分,所以我用Obj-C回答了问题。我的错,对OP:我道歉

这里有一个更完整的答案,实际上可以编译。我使用:
dispatch\u async
来发出警报,以便您可以看到“activityType”最终是什么

avc.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertViewQuick(@"Activity Status", activityType, @"OK");
    });
    if (completed)
    {
        NSLog(@"The Activity: %@ was completed", activityType);
    }
    else
    {
        NSLog(@"The Activity: %@ was NOT completed", activityType);
    }
};

这是很久以前的回答,但有一个丢失和非swift信息的混合,所以这里是我的版本,希望它能帮助需要更完整的完成处理程序示例的人:

    avc.completionWithItemsHandler = {[weak self](activityTypeChosen, completed:Bool, returnedItems:[AnyObject]?, error:NSError?) -> Void in

        // ReturnedItems is an array of modified NSExtensionItem, or nil of nothing modified
        // if (activityType == nil) User dismissed the view controller without making a selection.

        // Dismiss the view controller we presented
        // (assume a reference to it was stored in self.activityVC)
        self?.activityVC?.dismissViewControllerAnimated(true, completion: {
            if activityTypeChosen == nil {
                NSLog("User canceled without choosing anything")
            }
            else if completed {
                NSLog(")User chose an activity and iOS sent it to that other app/service/whatever OK")
            }
            else {
                NSLog("There was an error: \(error)")
            }
        })
    }
请注意,该行将解除视图控制器。UIActivityViewController的文档非常明确地说,你的应用程序负责呈现VC和拒绝VC

如前所述,对于Swift 3和4以及iOS 10和11,请如下使用:

avc.completionWithItemsHandler = { activity, success, items, error in 

}
activityVC.completionWithItemsHandler = {(activityType: UIActivityType?, completed: Bool, returnedItems: [Any]?, error: Error?) in

}

present(activityVC, animated: true, completion: nil)

它与您的代码完全相同,但使用swift。第一个块是完成句柄的类型别名,有些人知道闭包的参数,而不是你实际投入到项目中的东西。好吧,好了,足够了:)考虑我被惩罚了。我的错。我没有看到“快速”部分。(我真的没有)。我将留下我的答案,但编辑它,说我误解了这个问题。此外,我不能编辑我的投票,因为答案是“16小时前,我不能编辑投票,除非答案被编辑”(出于某种原因)我编辑了我的帖子,以防止人们认为你将类型别名代码放入了他们的项目+1。当我收到编译器警告时,这是一个有用的答案。
“completionHandler”在iOS 8.0中被弃用:改用completionWithItemsHandler。
因为它显示了我需要添加的Swift代码模板。你是什么意思“实际上是可以编译的?”这个问题是关于SWIFT的,你在Objul-Co写了一个答案。好吧,够了:考虑我被惩罚了。我的坏。我没有看到“Swift”部分。(我真的没有)。我会留下我的答案,但是编辑它说我误解了问题。我也不能编辑我的投票COS答案是“16小时前,我无法编辑投票,除非答案被编辑”(出于某种原因)