Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

ios - Set different activity items for UIActivityViewController Swift

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...