当用户在 FB 或 Twitter 上发帖时,我想知道分享是否完成,或者视图已被 x 关闭。
有谁知道 ShareKit 中是否内置了委托方法,或者我是否必须将自己的方法写入其中?
现在我直接使用共享器,但我可能会切换到使用共享工具包弹出窗口。我只是使用两行代码:
SHKItem *item = [SHKItem text:someText];
[SHKFacebook shareItem:item];
我遇到了您的问题并提出了一个解决方案,也许不是最漂亮的解决方案,但它确实解决了问题。有一个名为 SHKSharerDelegate 的 delagate,它可以与共享器一起用于此目的,因此如果您直接从代码中调用共享器(没有操作表),那么您应该执行以下操作:
NSString* mySharerClassName = @"SHKFacebook";
SHKSharer* classItem = (SHKSharer*)[[NSClassFromString(mySharerClassName) alloc] init];
Class sharerClass = [classItem class];
if ( [sharerClass canShare] ){
[classItem performSelector: @selector(setItem:) withObject: item];
//Assuming that the class where this code is conforms to the SHKSharerDelegate protocol
[classItem performSelector: @selector(setShareDelegate:) withObject: self];
[classItem performSelector: @selector(send)];
}
如果您必须使用 ActionSheet,它会变得有点棘手,主要是因为不支持它,只需转到 ActionSheet 头文件 (ShareKit/UI/SHKActionSheet.h) 并添加一个委托属性:
@property (nonatomic, retain) id sharerDelegate;
注意不是id<SHKSharerDelegate>
,尝试那样做,你会经历很多痛苦。这就是为什么我说那不是最漂亮的。添加并合成属性后,查找此方法:
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
它在哪里说
id sharer = [sharers objectAtIndex:buttonIndex];
[NSClassFromString(sharer) performSelector:@selector(shareItem:) withObject:item];
改变它
id sharer = [sharers objectAtIndex:buttonIndex];
if ( sharerDelegate == nil ){
[NSClassFromString(sharer) performSelector:@selector(shareItem:) withObject:item];
}else{
SHKSharer* classItem = [[NSClassFromString(sharer) alloc] init];
[classItem performSelector: @selector(setItem:) withObject: item];
[classItem performSelector: @selector(setShareDelegate:) withObject: sharerDelegate];
[classItem performSelector: @selector(send)];
}
如果您对此更感兴趣,我将尝试尽快发表博客文章并编辑答案以引用它。希望我仍然可以帮助别人!