我很清楚如何与the iOS sharing activity分享链接...但我想在网址中为不同类型的共享定制跟踪,始终使用相同的标准......示例如下。 ..
Twitter的:
http://www.example.com?utm_source=TWITTER&utm_medium=social&utm_campaign=socialbuttons&utm_content=app_android
Facebook的:
http://www.example.com?utm_source=FACEBOOK&utm_medium=social&utm_campaign=socialbuttons&utm_content=app_android
等等...
有可能吗?我该怎么办?
答案 0 :(得分:4)
我通过添加自定义UIActivityItemProvider
:
#import "CustomUiActivityItemProvider.h"
@implementation CustomUiActivityItemProvider
- (id)initWithText:(NSString *)text{
if ((self = [super initWithPlaceholderItem:text])) {
self.text = text ?: @"";
self.url = @"";
}
return self;
}
- (id)item {
NSString *activityType = self.activityType;
if ([self.placeholderItem isKindOfClass:[NSString class]]) {
if ([self.activityType isEqualToString:UIActivityTypePostToFacebook]) {
self.url = [[NSString alloc] initWithFormat:@"%@&utm_source=facebook", self.text];
} else if ([activityType isEqualToString:UIActivityTypePostToTwitter]) {
self.url = [[NSString alloc] initWithFormat:@"%@&utm_source=twitter", self.text];
} else if ([activityType isEqualToString:UIActivityTypeMessage]) {
self.url = [[NSString alloc] initWithFormat:@"%@&utm_source=message", self.text];
} else if([activityType isEqualToString:UIActivityTypeMail]){
self.url = [[NSString alloc] initWithFormat:@"%@&utm_source=email", self.text];
} else if ([activityType isEqualToString:UIActivityTypePostToWeibo]){
self.url = [[NSString alloc] initWithFormat:@"%@&utm_source=weibo", self.text];
}else{
self.url = [[NSString alloc] initWithFormat:@"%@&utm_source=other", self.text];
}
return self.url;
}
return self.placeholderItem;
}
@end
并像这样使用它:
CustomUiActivityItemProvider *customProvider = [[CustomUiActivityItemProvider alloc] initWithText:urlString];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[customProvider]
applicationActivities:nil];
activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll];
activityVC.completionHandler = ^(NSString *activityType, BOOL completed) {
// CODE....
}
[self presentViewController:activityVC animated:YES completion:nil];
答案 1 :(得分:2)
UIActivityItemSource
作为UIActivityViewController
的数据提供者。文档说明如下。实现此协议时,您的对象将成为数据提供者,为视图控制器提供对项目的访问权。
按如下方式创建NSObject
课程,并在UIViewController
课程后添加
class shareDifferentUrl: NSObject, UIActivityItemSource {
@objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
return ""
}
@objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
if activityType == UIActivityTypePostToTwitter {
return NSURL(string:"https://twitter.com/")
} else if activityType == UIActivityTypePostToFacebook {
return NSURL(string:"https://www.facebook.com/")
}
return nil
}
}
现在只需将上述课程添加为activityItems
的{{1}}
UIActivityViewController
现在,我们可以为 twitter 和 fb
@IBAction func share(sender: AnyObject) {
let activityVC = UIActivityViewController(activityItems: [shareDifferentUrl()] as [AnyObject], applicationActivities: nil)
self.navigationController?.presentViewController(activityVC, animated: true, completion: nil)
}