Set different activity items for UIActivityViewController Swift
Asked Answered
J

3

37

I have UIActivityViewController with 4 options: Message / Mail / TW / FB. I want to send different activities for each option!

For eg: In iMessage sheet, I need to put : String, NSURL, and UIImage. In Mail I need to place string in Subject field, then String in body, UIImage and NSURL also. In TW/FB I want to place image like socials post did it, also some String and NSURL.

Do you have any idea if this is possible in iOS8, with Swift ?

I searched a lot for some pieces of code, did not found the best for me.

Jannjanna answered 5/5, 2015 at 21:39 Comment(0)
C
55

You should take advantage of the UIActivityItemSource protocol. The activityItems parameter of the initializer of UIActivityViewController accepts either an array of data objects or an array of objects that implement the UIActivityItemSource protocol.

As an example consider an item source like the following.

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")
    }
}

The above item source returns different string data objects, subjects and thumbnail images based on the activity type. To use, you just need to pass it into the UIActivityViewController initializer.

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

Similarly, you could define a custom MyUrlItemSource class that returns different URLs based on the selected activity and pass it along in the initializer.

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

All of this is outlined in the official documentation for UIActivityViewController and UIActivityItemSource

Calutron answered 12/8, 2015 at 19:15 Comment(4)
@BhaveshNai: There#s not much Swift magic involved here. Translating the above to Objective-C should be straightforward.Calutron
Writing delegate = self not required at all.Malocclusion
How would you do the last thing, "define a custom MyUrlItemSource class that returns different URLs based on the selected activity"Dyne
@user6631314: It's the same code except that you return a URL instead of a String.Calutron
H
5

Same solution like from hennes translated to 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")
  }

}
Higherup answered 28/6, 2017 at 7:56 Comment(0)
C
4

Objective-C equivalent of hennes answer:

@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
Chokedamp answered 19/10, 2017 at 9:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.