我正在使用UIActivityViewController来显示共享表,以便用户可以共享我的应用。我正在测试推文,我得到了一些意想不到的结果。在第一次发推文时,一切顺利。在第二次,我得到一个重复的推文错误消息,这是预期的。问题是completionWithItemsHandler返回成功:Bool为true!
我希望能够向用户显示我自己的个性化消息,而不是当前返回的大量消息。
这是我的代码:
@IBAction func ShareButtonTapped(sender: AnyObject) {
let textToShare = "I'm using Buzz! The new way to send emoji's, with sound, it's annoying, funny and amazing"
var url = NSURL(string: "-Image url masked out-")
var data = NSData(contentsOfURL: url!)
let image = UIImage(data: data!)
if let myWebsite = NSURL(string: "-redirect masked out-")
{
let objectsToShare = [textToShare, myWebsite]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityVC.completionWithItemsHandler = {
(activity, success, items, error) in
println("Activity: \(activity) Success: \(success) Items: \(items) Error: \(error)")
}
self.presentViewController(activityVC, animated: true, completion: { () -> Void in
})
}
}
这是我的日志:
2015-01-27 11:10:58.021 Buzz [3239:813859] LaunchServices: invalidationHandler称为
2015-01-27 11:10:58.052 Buzz [3239:813860] LaunchServices:invalidationHandler名为Activity: com.apple.UIKit.activity.PostToTwitter成功:true项目:nil错误:无 2015-01-27 11:11:04.134 Buzz [3239:813859] LaunchServices: invalidationHandler称为
2015-01-27 11:11:09.182 Buzz [3239:813859]插件com.apple.share.Twitter.post无效
答案 0 :(得分:23)
使用像这样的完成处理程序对于 SWIFT 3 AND 4,iOS 10和11 :
activityVC.completionWithItemsHandler = {(activityType: UIActivityType?, completed: Bool, returnedItems: [Any]?, error: Error?) in
if !completed {
// User canceled
return
}
// User completed activity
}
self.present(activityVC, animated: true, completion: nil)
答案 1 :(得分:13)
SWIFT 2.0 iOS 8.0> ,您应该使用这样的完成处理程序:
self.presentViewController(activityVC, animated: true, completion: nil)
activityVC.completionWithItemsHandler = {(activityType, completed:Bool, returnedItems:[AnyObject]?, error: NSError?) in
// Return if cancelled
if (!completed) {
return
}
//activity complete
//some code here
}
答案 2 :(得分:6)
我认为您不会影响UIActivityViewController的反馈流,因为它是高级的,易于使用的组件,不适合细粒度的自定义。
但是,您可以做的是保存用户在第一条推文之后发送此确切消息的状态,然后使用excludedActivityTypes
和UIActivityTypePostToTwitter
从UIActivityController禁用Twitter。因此,不会显示重复推文的错误,而是阻止动作序列发生。
答案 3 :(得分:-2)
我建议更换
self.presentViewController(activityVC, animated: true, completion: { () -> Void in })
与
self.presentViewController(activityVC, animated: true, completion: nil)
这对我有用。希望它有所帮助!