UIActivity custom share, thumbnail icon not be shown when touch more button
Asked Answered
H

4

9

My app support iOS 7+, I use UIActivity image size: 60pt for iPhone, 76pt for iOS for iPad,

When touch more button to re-order the items, it can't show the icon in the list.

Show share activities

enter image description here

CODE as below:

class ZYShare {
class ActivityItem {
    init() {
        self.title = ""
        self.icon = ""
    }

    var title: String?
    var icon: String?
    var type: ShareType = ShareTypeAny
}

class CommonActivity: UIActivity {
    lazy var itemInfo = ActivityItem()

    override func prepareWithActivityItems(activityItems: [AnyObject]) {
        // do something

    }

    override func canPerformWithActivityItems(activityItems: [AnyObject]) -> Bool {
        return true
    }

    override class func activityCategory() -> UIActivityCategory {
        return UIActivityCategory.Action
    }

    override func activityType() -> String? {

        return NSLocalizedString(itemInfo.title ?? "", comment: "")
    }

    override func activityTitle() -> String? {
        return NSLocalizedString(itemInfo.title ?? "", comment: "")
    }

    override func activityImage() -> UIImage? {
        println(itemInfo.icon)

        if let icon = itemInfo.icon {
            return UIImage(named: icon)
        } else {
            return nil
        }
    }

    override func performActivity() {
        ShareSDK.showShareViewWithType(itemInfo.type
            , container: nil
            , content: publishContent
            , statusBarTips: false
            , authOptions: authOptions
            , shareOptions: options
            , result: handle)

        self.activityDidFinish(true)
    }
}

class QQActivity: CommonActivity {
    override init() {
        super.init()

        self.itemInfo.icon = "ShareToQQ"
        self.itemInfo.title = "QQ"
        self.itemInfo.type = ShareTypeQQ
    }
}

class WeChatSessionActivity: CommonActivity {
    override init() {
        super.init()

        self.itemInfo.icon = "ShareToWeChat"
        self.itemInfo.title = "微信"
        self.itemInfo.type = ShareTypeWeixiSession
    }
}

class WeChatTimelineActivity: CommonActivity {
    override init() {
        super.init()

        self.itemInfo.icon = "ShareToWeChatTimeLine"
        self.itemInfo.title = "朋友圈"
        self.itemInfo.type = ShareTypeWeixiTimeline
    }
}

class QQSpaceActivity: CommonActivity {
    override init() {
        super.init()

        self.itemInfo.icon = "ShareToQzone"
        self.itemInfo.title = "QQ空间"
        self.itemInfo.type = ShareTypeQQSpace
    }
}
}
Helga answered 13/12, 2014 at 4:13 Comment(7)
Its preferable to upload screens with english text. Because everyone can understand english but not this language (I'm guessing its Chinese).Ratfink
From here. The image is used to generate a button for your service in the UI displayed by the UIActivityViewController object. I think the image is for the icon of custom activity in your first picture. The more button is for rearrange the activities, be it a custom or built in one. Icon is not shown for custom activity.you can use the built in Weibo and TencentWeibo icon as long as you have them installed on your iPhone.Lesialesion
For others like wechat, you can use a custom share sheet just like what the ShareSDK library does, which is shown in your code.Lesialesion
@Lesialesion Thanks, ShareSDK is OK, but I want to use iOS system provided UI.Helga
Then, I am afraid there is no way to show that icon.Lesialesion
@Lesialesion That's too badHelga
It is ok, I don't think it would have any severe consequence.Lesialesion
F
6

Quite old question, but maybe the solution is helpful for others too!

In your UIActivity subclass implement/overwrite the method

- (UIImage *)activityImage {
    return [UIImage imageNamed:@"Activity Icon"];
}

to return the image to be shown in the UIActivityViewController itself (as you've done).

In addition to this implement/overwrite the method

- (UIImage *)activitySettingsImage {
    return [UIImage imageNamed:@"Activity Settings Icon"];
}

to return an(other or the same) image to be shown in the more/settings view.

I haven't found the second method in the docs, but it is none of the '_xx' method of UIAction, so I would guess it isn't private...

Freitas answered 28/2, 2016 at 8:28 Comment(4)
Nice finding! Thanks.Helga
hi ZYiOS did your app get approved after using this method ??Kainite
Have never had problems to get an approval (using this method).Freitas
Hello @Helga I tried same implementation as per above description, But Not working in my application....Canvass
B
4

I found the solution in Swift.

@objc var _activitySettingsImage: UIImage? {
    return UIImage(named: "icon")!
}

The image is 29x29 + @2x + @3x. Rather activityImage, no mask is apply to the image. If you want rounded corners, you must add them.

Works with Swift 5 on iOS 12

Banderillero answered 1/4, 2019 at 13:27 Comment(1)
that did the trick on iOS17 as well!Glabrous
R
3

UIActivity has an undocumented activitySettingsImage property. The following code shows how to implement it with Swift 3 in order to display a thumbnail of your custom activity in the "Activities" list:

import UIKit

class MyActivity: UIActivity {

    override var activityType: UIActivityType? {
        return UIActivityType(rawValue: String(describing: classForCoder))
    }

    override var activityTitle: String? {
        return "My Activity Title"
    }

    override var activityImage: UIImage? {
        return UIImage(named: "icon")
    }

    var activitySettingsImage: UIImage? {
        return UIImage(named: "icon")
    }

    override class var activityCategory: UIActivityCategory {
        return .share
    }

    /* ... */
}

Note that you don't have not override activitySettingsImage to implement it in your code.

Rudolphrudwik answered 26/1, 2017 at 11:59 Comment(0)
C
0

I have encountered the same problem in my project in Swift. I tried to use @imanou-petit answer, but had no success fixing icon bug. But, then I tried to write my UIActivity subclass in objective-c(despite my project is in Swift), with activitySettingsImage method, like @laboretars wrote, and it worked! That's strange, but it worked.

Cuomo answered 11/3, 2019 at 11:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.