iOS自定义分享 UIAlertController

所谓的自定义就是自己动手写不需要现成的。
这两天在做分享的时候用到了ShareSDK,但是里面在微博分享时他总是会跳到网页版分享,但是我需要的是客户端分享而这儿的客户端需要又有一定的要求!so要使用客户端的话只能自己写了。(新手不对之处请谅解!)

当然是一个View
.h的内容

@protocol AlertDelegate 

- (void)action_cancel;//取消
- (void)action_ShareWithNumber:(NSInteger )number;分享
@end

@interface AlertController : UIView
@property (nonatomic, weak) id deleate;
@end

.m的内容
我这是需要微博、微信、QQ的分享所以导入了

#import 
#import 
#import "WXApi.h"
#import 
#import "Header.h"

我在这儿定义了三个属性

@property (nonatomic, strong) UILabel *labelWithButtonView;//显示分享图标的
@property (nonatomic, strong) UIButton *buttonWithCancel;//取消
@property (nonatomic, assign) NSInteger numberWithInstalled;

这是警示框界面方法

self.numberWithInstalled = 0;//初始为0
    NSArray *arrayWithIcon = [NSArray array];
    NSArray *arrayWithName = [NSArray array];
    arrayWithIcon = @[@"1sns_icon_1",@"1sns_icon_6",@"1sns_icon_24",@"1sns_icon_22",@"1sns_icon_23",@"1sns_icon_37"];
    arrayWithName = @[@"微博",@"QQ空间",@"QQ好友",@"微信好友",@"微信朋友圈",@"微信收藏"];
    if ([QQApiInterface isQQInstalled]) {
        //判断是否有qq
    } else {
        arrayWithIcon = @[@"1sns_icon_1",@"1sns_icon_22",@"1sns_icon_23",@"1sns_icon_37"];
        arrayWithName = @[@"微博",@"微信好友",@"微信朋友圈",@"微信收藏"];
        self.numberWithInstalled = 2;
    }
    
    if ([WXApi isWXAppInstalled]) {
        //判断是否有微信
    } else{
        arrayWithIcon = @[@"1sns_icon_1",@"1sns_icon_6",@"1sns_icon_24"];
        arrayWithName = @[@"微博",@"QQ空间",@"QQ好友"];
        self.numberWithInstalled = 1;
    }
    if ([WXApi isWXAppInstalled] || [QQApiInterface isQQInstalled]) {
        
    } else {
        arrayWithIcon = @[@"1sns_icon_1"];
        arrayWithName = @[@"微博"];
    }
//上面这个方法有点麻烦但是很实用的
    self.backgroundColor = GetColor(1, 1, 1, 0.4);
    [self addSubview:self.buttonWithCancel];
    [self.buttonWithCancel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-10);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(ADAPTATION_HEIGHT(50));
    }];
    [self addSubview:self.labelWithButtonView];
    __weak typeof(self) weakSelf = self;
    [self.labelWithButtonView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-20 - ADAPTATION_HEIGHT(50));
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        if (weakSelf.numberWithInstalled == 0) {
            make.height.mas_equalTo(ADAPTATION_HEIGHT(200));
        } else {
            make.height.mas_equalTo(ADAPTATION_HEIGHT(100));
        }
        
    }];
    
    for (int i = 0; i < arrayWithIcon.count; i ++) {
        UIButton *bu = [[UIButton alloc]init];
        UILabel *label = [[UILabel alloc]init];
        if (i > 3) {
            bu.frame = CGRectMake(10 + MAINSCREEN_WIDTH / 4 * (i - 4), 10 + ADAPTATION_HEIGHT(100), ADAPTATION_WIDTH(60), ADAPTATION_WIDTH(60));
            label.frame = CGRectMake(10 + MAINSCREEN_WIDTH / 4 * (i - 4), ADAPTATION_WIDTH(70) + ADAPTATION_HEIGHT(100), ADAPTATION_WIDTH(60), 20);
        } else {
            bu.frame = CGRectMake(10 + MAINSCREEN_WIDTH / 4 * i, 10 , ADAPTATION_WIDTH(60), ADAPTATION_WIDTH(60));
            label.frame = CGRectMake(10 + MAINSCREEN_WIDTH / 4 * i, 70 , ADAPTATION_WIDTH(60), 20);
        }
        [bu setImage:IMGNAME(arrayWithIcon[i]) forState:UIControlStateNormal];
        bu.tag = 490 + i;
        [bu addTarget:self action:@selector(shareWithButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.labelWithButtonView addSubview:bu];
        label.text = arrayWithName[i];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = FONT(13);
        [self.labelWithButtonView addSubview:label];
    }

相对应的分享

- (void)shareWithButton:(UIButton *)sender{
    NSInteger number = sender.tag - 490;
    if (self.numberWithInstalled == 0) {
        [self.deleate action_ShareWithNumber:number];
        return;
    }
    if (number == 0) {
        [self.deleate action_ShareWithNumber:0];
    }
    //微信好友
    if (number == 1 && self.numberWithInstalled == 2) {
        [self.deleate action_ShareWithNumber:3];
    }
    //微信朋友圈
    if (number == 2 && self.numberWithInstalled == 2) {
        [self.deleate action_ShareWithNumber:4];
    }
    //微信收藏
    if (number == 3 && self.numberWithInstalled == 2) {
        [self.deleate action_ShareWithNumber:5];
    }
    //QQ空间
    if (number == 1 && self.numberWithInstalled == 1) {
        [self.deleate action_ShareWithNumber:1];
    }
    //QQ好友
    if (number == 2 && self.numberWithInstalled == 1) {
        [self.deleate action_ShareWithNumber:2];
    }
    
}

其他的都是Getter和Setter方法

在实现代理的类里面加入

#pragma mark - AlertDelegate
- (void)action_cancel{
    [self.aler removeFromSuperview];
}
- (void)action_ShareWithNumber:(NSInteger)number{
    NSLog(@"%ld",number);
    NSArray *imageArray = @[[UIImage imageNamed:@"[email protected]"]];
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    if (imageArray) {
        [shareParams SSDKSetupShareParamsByText:@""
                                         images:imageArray
                                            url:nil
                                          title:@""
                                           type:SSDKContentTypeAuto];
    }
    
    [shareParams SSDKEnableUseClientShare];
    
    //微博
    if (number == 0) {
            [ShareSDK share:SSDKPlatformTypeSinaWeibo parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
            }];
    }
    //QQ空间
    if (number == 1) {
        [ShareSDK share:SSDKPlatformSubTypeQZone parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
        }];
    }
    //QQ好友
    if (number == 2) {
        [ShareSDK share:SSDKPlatformSubTypeQQFriend parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
        }];
    }
    //微信好友
    if (number == 3) {
        [ShareSDK share:SSDKPlatformSubTypeWechatSession parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
                    }];
    }
    //微信朋友圈
    if (number == 4) {
        [ShareSDK share:SSDKPlatformSubTypeWechatTimeline parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
                    }];
    }
    //微信收藏
    if (number == 5) {
        [ShareSDK share:SSDKPlatformSubTypeWechatFav parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
                    }];
    }
    
}

需要其他的分享可以自行添加
这就能实现相应的分享 不足之处请请谅解!

你可能感兴趣的:(iOS自定义分享 UIAlertController)