上一个demo:http://pan.baidu.com/s/1c0ANO6O
UIActivityViewController是IOS6.0以后苹果带的分享功能的视图控制器,属于uiviewController。现在一般都用umeng或者shareSDK,这个是原生版的。
创建UIActivityViewController的时候,苹果会自带几个分享功能,例如打印,AirDrop,save,Email等等。通过点击屏幕按钮,创建一个UIActivityViewController:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *bt = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
bt.backgroundColor = [UIColor orangeColor];
bt.layer.cornerRadius = 50;
[self.view addSubview:bt];
[bt setTitle:@"分享" forState:UIControlStateNormal];
bt.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2);
[bt addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)share:(id)sender
{
//要分享的内容,加要一个数组里边,初始化UIActivityViewController
NSString *textToShare = @"要分享内容title";
NSString *description = @"这是我的内容---------";
UIImage *imageToShare = [UIImage imageNamed:@"logocc.png"];
NSURL *urlToShare = [NSURL URLWithString:@"http://www.iashes.com/"];
NSArray *activityItems = @[textToShare, description,imageToShare, urlToShare];
//创建自定义的Activity,加到一个数组里边
MyActivity *act1 = [[MyActivity alloc]initWithImage:[UIImage imageNamed:@"logocc.png"] atURL:@"http://www.iashes.com/" atTitle:@"share Sina" atShareContentArray:activityItems];
//myActivity是自定义的类,继承于UIActivity
MyActivity *act2 = [[MyActivity alloc]initWithImage:[UIImage imageNamed:@"logo_64.png"] atURL:@"http://www.iashes.com/admin.html" atTitle:@"share Renren" atShareContentArray:activityItems];
NSArray *apps = @[act1,act2];
//创建
UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:apps];
//关闭系统的一些分享
activityVC.excludedActivityTypes = @[UIActivityTypePostToTwitter,
UIActivityTypeMessage,
UIActivityTypePrint,
UIActivityTypeCopyToPasteboard,
UIActivityTypeAssignToContact,
UIActivityTypeSaveToCameraRoll,
UIActivityTypeAddToReadingList,
UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo,
UIActivityTypePostToTencentWeibo,
UIActivityTypeAirDrop];
//模态
[self presentViewController:activityVC animated:YES completion:nil];
MyActivity.h中:
#import@interface MyActivity : UIActivity
//title是当前类型,shareImage是分享图标,URL是要分享的地址,getShareArray保存用户要分享的内容
@property (nonatomic,strong) UIImage *shareImage;
@property (nonatomic,copy)NSString *URL;
@property (nonatomic,copy)NSString *title;
@property (nonatomic,strong)NSArray *getShareArray;
-(instancetype)initWithImage:(UIImage *)shareImage atURL:(NSString *)URL atTitle:(NSString *)title atShareContentArray:(NSArray *)shareContentArray;
@end
MyActivity.m中:
#import "MyActivity.h"
@implementation MyActivity
//初始化方法
-(instancetype)initWithImage:(UIImage *)shareImage atURL:(NSString *)URL atTitle:(NSString *)title atShareContentArray:(NSArray *)shareContentArray
{
if(self = [super init]){
_shareImage = shareImage;
_URL = URL;
_title = title;
_getShareArray = [[NSArray alloc]initWithArray:shareContentArray];
}
return self;
}
//以下方法都是自定义UiActivity需要重写的方法
+(UIActivityCategory)activityCategory
{
return UIActivityCategoryShare;
}
//设置类型
-(NSString *)activityType
{
return _title;
}
//设置现实的标题
-(NSString *)activityTitle
{
return _title;
}
//图片
-(UIImage *)activityImage
{
return _shareImage;
}
//返回yes就行,这个表示该分享是不是在controller中显示
-(BOOL)canPerformWithActivityItems:(NSArray *)activityItems
{
return YES;
}
//是不是自定义的controller,如果为空,则调用performActivity方法
- (UIViewController *)activityViewController
{
return nil;
}
//点击分享图标之后触发的方法
-(void)performActivity
{
if(nil == _title) {
return;
}
NSLog(@"要分享的内容-----%@",_getShareArray);
NSLog(@"分享的类型------ %@",_title);
if([_title isEqualToString:@"share Renren"]){
NSLog(@"---^^^ renren");
//调用人人的sdk
}else if([_title isEqualToString:@"share Sina"]){
//调用新浪sdk
}
}
@end
版权属于: Ashes of time
原文地址: http://www.iashes.com/2015-02-468.html
转载时必须以链接形式注明原始出处及本声明。