为UIActivityViewController Swift设置不同的活动项目

时间:2015-05-05 21:39:39

标签: ios swift uiactivityviewcontroller

我有UIActivityViewController有4个选项:消息/邮件/ TW / FB 。 我想为每个选项发送不同的活动!

例如: 在iMessage表格中,我需要添加:StringNSURLUIImage。 在邮件中,我需要在主题字段中放置字符串,然后在正文中放置字符串,UIImageNSURL。 在TW/FB我希望将图片放置为社交帖子,也是一些字符串和NSURL

你知道在iOS8中是否可以使用Swift吗?

我搜索了很多代码,但没有找到最适合我的。

3 个答案:

答案 0 :(得分:48)

您应该利用UIActivityItemSource协议。 activityItems初始值设定项的UIActivityViewController参数接受数据对象数组或实现UIActivityItemSource协议的对象数组。

作为示例,请考虑以下项目来源。

class MyStringItemSource: NSObject, UIActivityItemSource {
    @objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {
        return ""
    }

    @objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
        if activityType == UIActivityTypeMessage {
            return "String for message"
        } else if activityType == UIActivityTypeMail {
            return "String for mail"
        } else if activityType == UIActivityTypePostToTwitter {
            return "String for twitter"
        } else if activityType == UIActivityTypePostToFacebook {
            return "String for facebook"
        }
        return nil
    }

    func activityViewController(activityViewController: UIActivityViewController, subjectForActivityType activityType: String?) -> String {
        if activityType == UIActivityTypeMessage {
            return "Subject for message"
        } else if activityType == UIActivityTypeMail {
            return "Subject for mail"
        } else if activityType == UIActivityTypePostToTwitter {
            return "Subject for twitter"
        } else if activityType == UIActivityTypePostToFacebook {
            return "Subject for facebook"
        }
        return ""
    }

    func activityViewController(activityViewController: UIActivityViewController, thumbnailImageForActivityType activityType: String!, suggestedSize size: CGSize) -> UIImage! {
        if activityType == UIActivityTypeMessage {
            return UIImage(named: "thumbnail-for-message")
        } else if activityType == UIActivityTypeMail {
            return UIImage(named: "thumbnail-for-mail")
        } else if activityType == UIActivityTypePostToTwitter {
            return UIImage(named: "thumbnail-for-twitter")
        } else if activityType == UIActivityTypePostToFacebook {
            return UIImage(named: "thumbnail-for-facebook")
        }
        return UIImage(named: "some-default-thumbnail")
    }
}

上述项目源根据活动类型返回不同的字符串数据对象,主题和缩略图。要使用,您只需将其传递到UIActivityViewController初始化程序。

UIActivityViewController(activityItems: [MyStringItemSource()], applicationActivities: nil)

同样,您可以定义一个自定义MyUrlItemSource类,该类根据所选活动返回不同的URL,并在初始化程序中传递它。

UIActivityViewController(activityItems: [MyStringItemSource(), MyUrlItemSource()], applicationActivities: nil)

所有这些都在UIActivityViewControllerUIActivityItemSource

的官方文档中列出

答案 1 :(得分:3)

hennes转换为 swift 3

的相同解决方案
class MyStringItemSource: NSObject, UIActivityItemSource {

  public func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
    return ""
  }

  public func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType) -> Any? {
    if activityType == UIActivityType.message {
      return "String for message"
    } else if activityType == UIActivityType.mail {
      return "String for mail"
    } else if activityType == UIActivityType.postToTwitter {
      return "String for twitter"
    } else if activityType == UIActivityType.postToFacebook {
      return "String for facebook"
    }
    return nil
  }

  public func activityViewController(_ activityViewController: UIActivityViewController, subjectForActivityType activityType: UIActivityType?) -> String {
    if activityType == UIActivityType.message {
      return "Subject for message"
    } else if activityType == UIActivityType.mail {
      return "Subject for mail"
    } else if activityType == UIActivityType.postToTwitter {
      return "Subject for twitter"
    } else if activityType == UIActivityType.postToFacebook {
      return "Subject for facebook"
    }
    return ""
  }

  public func activityViewController(_ activityViewController: UIActivityViewController, thumbnailImageForActivityType activityType: UIActivityType?, suggestedSize size: CGSize) -> UIImage? {
    if activityType == UIActivityType.message {
      return UIImage(named: "thumbnail-for-message")
    } else if activityType == UIActivityType.mail {
      return UIImage(named: "thumbnail-for-mail")
    } else if activityType == UIActivityType.postToTwitter {
      return UIImage(named: "thumbnail-for-twitter")
    } else if activityType == UIActivityType.postToFacebook {
      return UIImage(named: "thumbnail-for-facebook")
    }
    return UIImage(named: "some-default-thumbnail")
  }

}

答案 2 :(得分:1)

Objective-C相当于hennes回答:

@interface MyStringItemSource : NSObject <UIActivityItemSource>
{

}

@end


@implementation MyStringItemSource

-(id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
    return @"";
}

-(id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(UIActivityType)activityType
{
    if (activityType == UIActivityTypeMessage) {
        return @"String for message";
    } else if (activityType == UIActivityTypeMail) {
        return @"String for mail";
    }
    return nil;
}

-(NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(UIActivityType)activityType
{
    if (activityType == UIActivityTypeMessage) {
        return @"Subject for message";
    } else if (activityType == UIActivityTypeMail) {
        return @"Subject for mail";
    }
    return @"";
}

-(UIImage *)activityViewController:(UIActivityViewController *)activityViewController thumbnailImageForActivityType:(UIActivityType)activityType suggestedSize:(CGSize)size
{
    if (activityType == UIActivityTypeMessage) {
        return [UIImage imageNamed: @"thumbnail-for-message"];
    } else if (activityType == UIActivityTypeMail) {
        return [UIImage imageNamed: @"thumbnail-for-mail"];
    }
    return [UIImage imageNamed: @"thumbnail-for-default"];
}

@end