2016-08-04 60 views
5

我很清楚如何與the iOS sharing activity共享鏈接...但我想自定義不同類型的股票的URL跟蹤,總是使用相同的標準...下面的示例...自定義iOS的分享網址

的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

您需要爲** Twitter共享不同'url' **和** facebook **使用'UIActivityController'。 – Gokul

+0

是的,確切地說。 :-) – Napolux

+1

Duplicate:[爲UIActivityViewController Swift設置不同的活動項目](http://stackoverflow.com/q/30063708/2415822) – JAL

回答

4

我在Objective-C解決了添加自定義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 .The文檔狀態數據提供者。

在實施該協議中,您的對象成爲數據提供者,提供視圖控制器能夠訪問的項目。

  • 爲了更多地瞭解請參閱本documentation
  • 創建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 
    } 
    } 
    
  • 這就是它現在只是添加上面的類作爲您UIActivityViewController

    @IBAction func share(sender: AnyObject) { 
        let activityVC = UIActivityViewController(activityItems: [shareDifferentUrl()] as [AnyObject], applicationActivities: nil) 
        self.navigationController?.presentViewController(activityVC, animated: true, completion: nil) 
    } 
    
  • activityItems現在我們可以能後添加它分享不同url推特fb