Skip to content

Instantly share code, notes, and snippets.

@JeOam
Last active October 29, 2015 17:26
Show Gist options
  • Save JeOam/60d78be2ea91a98a9b80 to your computer and use it in GitHub Desktop.
Save JeOam/60d78be2ea91a98a9b80 to your computer and use it in GitHub Desktop.
UIActivityViewController tips
NSString *string = @"Text to share"
UIImage *image = [UIImage imageNamed:@"imageToShare"];
NSURL *URL = [NSURL URLWithString:@"relativeUrl"];

// Integrate with WeChat
NSArray *activity = @[[[WeixinSessionActivity alloc] init], [[WeixinTimelineActivity alloc] init]];

UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[string, image, URL] applicationActivities:activity];
[self.navigationController presentViewController:activityViewController animated:YES completion:nil];

注:微信需要在 application:didFinishLaunchingWithOptions: 中注册 app:

[WXApi registerApp:@"appId"];

这是从微信、微博等跳回来需要的设置,(注,微博的URL Schemes需在 appId 前面加 wb,微信的自带了 wx) : TARGETS -> Info -> URL Types -> 编辑 URL Schemes:添加唯一的字符串

@JeOam
Copy link
Author

JeOam commented Dec 19, 2014

Custom UIActivity:

// .h
#import <UIKit/UIKit.h>
@interface ActivityViewCustomActivity : UIActivity
@end

// .m
#import "ActivityViewCustomActivity.h"
@implementation ActivityViewCustomActivity
- (NSString *)activityType
{
    return @"yourappname.Review.App";
}
- (NSString *)activityTitle
{
    return @"Review App";
}
- (UIImage *)activityImage
{  
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        return [UIImage imageNamed:@"iPadShare.png"];
    }
    else
    {
        return [UIImage imageNamed:@"iPhoneShare.png"];
    }
}
- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems
{
    NSLog(@"%s", __FUNCTION__);
    return YES;
}
- (void)prepareWithActivityItems:(NSArray *)activityItems
{
    NSLog(@"%s",__FUNCTION__);
}
- (UIViewController *)activityViewController
{
    NSLog(@"%s",__FUNCTION__);
    return nil;
}
- (void)performActivity
{   
    // This is where you can do anything you want, and is the whole reason for creating a custom 
    // UIActivity
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=yourappid"]];
    [self activityDidFinish:YES];
}
@end

Note On activityImage:

The alpha channel of the image is used as a mask to generate the final image that is presented to the user. Any color data in the image itself is ignored. Opaque pixels have a gradient applied to them and this gradient is then laid on top of a standard background. Thus, a completely opaque image would yield a gradient filled rectangle.

For iPhone and iPod touch, images on iOS 7 should be 60 by 60 points; on earlier versions of iOS, you should use images no larger than 43 by 43 points. For iPad, images on iOS 7 should be 76 by 76 points; on earlier versions of iOS you should use images no larger than 60 by 60 points. On a device with Retina display, the number of pixels is doubled in each direction.

via click

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment