使用子类 UIActivityItemProvider 时没有共享操作可用
No share actions available when using subclassed UIActivityItemProvider
当我只传入一个 NSString 时,一切正常,但是当我尝试传入我的自定义 ItemProvider 时,我没有可用的共享操作。
MyCustomProvider
class MyCustomProvider: UIActivityItemProvider {
var placeholder: String!
var alternate: String!
init(placeholder: String, alternate: String) {
self.placeholder = placeholder
self.alternate = alternate
super.init()
}
override func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
if activityType == UIActivityTypeMessage {
return NSString(string: alternate)
} else {
return NSString(string: placeholder)
}
}
}
我在哪里执行:
var stringToShare = MyCustomProvider(placeholder: "Placeholder", alternate: "Alternate")
var sharingItems = [AnyObject]()
sharingItems.append(stringToShare)
let activityViewController = UIActivityViewController(activityItems: sharingItems, applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityTypePrint, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr, UIActivityTypePostToVimeo, UIActivityTypeAirDrop]
self.presentViewController(activityViewController, animated: true, completion: nil)
当上面的 stringToShare 改为 NSString(string: "Placeholder")
时,一切正常。我在实施过程中搞砸了什么?
我通过以下方式解决了这个问题:
class MyCustomProvider: NSObject, UIActivityItemSource {
var placeholder: String!
var alternate: String!
init(placeholder: String, alternate: String) {
self.placeholder = placeholder
self.alternate = alternate
super.init()
}
func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
return NSString(string: placeholder)
}
func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
if activityType == UIActivityTypeMessage {
return NSString(string: alternate)
} else {
return NSString(string: placeholder)
}
}
}
我不知道为什么,但它有效。
我认为您必须将 placeholderItem 设置为您要在 item() 中 return 的类型。
An object that can stand in for the actual object you plan to create. The contents of the object may be empty but the class of the object must match the class of the object you plan to provide later.
当我只传入一个 NSString 时,一切正常,但是当我尝试传入我的自定义 ItemProvider 时,我没有可用的共享操作。
MyCustomProvider
class MyCustomProvider: UIActivityItemProvider {
var placeholder: String!
var alternate: String!
init(placeholder: String, alternate: String) {
self.placeholder = placeholder
self.alternate = alternate
super.init()
}
override func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
if activityType == UIActivityTypeMessage {
return NSString(string: alternate)
} else {
return NSString(string: placeholder)
}
}
}
我在哪里执行:
var stringToShare = MyCustomProvider(placeholder: "Placeholder", alternate: "Alternate")
var sharingItems = [AnyObject]()
sharingItems.append(stringToShare)
let activityViewController = UIActivityViewController(activityItems: sharingItems, applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityTypePrint, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr, UIActivityTypePostToVimeo, UIActivityTypeAirDrop]
self.presentViewController(activityViewController, animated: true, completion: nil)
当上面的 stringToShare 改为 NSString(string: "Placeholder")
时,一切正常。我在实施过程中搞砸了什么?
我通过以下方式解决了这个问题:
class MyCustomProvider: NSObject, UIActivityItemSource {
var placeholder: String!
var alternate: String!
init(placeholder: String, alternate: String) {
self.placeholder = placeholder
self.alternate = alternate
super.init()
}
func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
return NSString(string: placeholder)
}
func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
if activityType == UIActivityTypeMessage {
return NSString(string: alternate)
} else {
return NSString(string: placeholder)
}
}
}
我不知道为什么,但它有效。
我认为您必须将 placeholderItem 设置为您要在 item() 中 return 的类型。
An object that can stand in for the actual object you plan to create. The contents of the object may be empty but the class of the object must match the class of the object you plan to provide later.