iOS复制链接弹出广告

2018-05-08  本文已影响10人  倪大头

UIPasteboard类可以获取当前粘贴板上的信息,在AppDelegate类如下方法中弹出广告:

- (void)applicationDidBecomeActive:(UIApplication *)application {//app进入活跃状态
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    if ([pasteboard.string isEqualToString:@"来呀造作啊"]) {
        NSLog(@"%@",pasteboard.string);
        PasteboardAlertView *pasteboardAlert = [[PasteboardAlertView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT)];
        UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
        [keyWindow addSubview:pasteboardAlert];
    }
}

准备一个View类作为广告弹窗:

#import "PasteboardAlertView.h"

@implementation PasteboardAlertView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
        [self pasteboardAlertShow];
    }
    return self;
}

- (void)pasteboardAlertShow {//弹出
    //商品视图
    UIView *infoView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScaleX*300, kScaleY*500)];
    infoView.center = CGPointMake(self.frame.size.width/2, kScaleY*70 + infoView.frame.size.height/2);
    infoView.backgroundColor = [UIColor whiteColor];
    infoView.layer.cornerRadius = 5;
    infoView.clipsToBounds = YES;
    [self addSubview:infoView];
    //商品图片
    UIImageView *infoImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, infoView.frame.size.width, kScaleY*320)];
    infoImg.image = [UIImage imageNamed:@"Dva2"];
    infoImg.contentMode = UIViewContentModeScaleAspectFill;
    infoImg.clipsToBounds = YES;
    [infoView addSubview:infoImg];
    //价格
    UILabel *priceLabel = [[UILabel alloc]init];
    priceLabel.text = @"1149";
    priceLabel.textColor = [UIColor orangeColor];
    priceLabel.font = [UIFont systemFontOfSize:18];
    [priceLabel sizeToFit];
    priceLabel.center = CGPointMake(infoView.frame.size.width/2, CGRectGetMaxY(infoImg.frame) + kScaleY*30 + priceLabel.frame.size.height/2);
    [infoView addSubview:priceLabel];
    //商品名称
    UILabel *nameLabel = [[UILabel alloc]init];
    nameLabel.text = @"守望先锋";
    nameLabel.textColor = [UIColor blackColor];
    nameLabel.font = [UIFont systemFontOfSize:16];
    [nameLabel sizeToFit];
    nameLabel.center = CGPointMake(infoView.frame.size.width/2, CGRectGetMaxY(priceLabel.frame) + kScaleY*20 + nameLabel.frame.size.height/2);
    [infoView addSubview:nameLabel];
    //查看详情
    UIButton *checkBtn = [[UIButton alloc]initWithFrame:CGRectMake(kScaleX*20, CGRectGetMaxY(nameLabel.frame) + kScaleY*20, kScaleX*260, kScaleY*40)];
    [checkBtn setTitle:@"查看详情" forState:UIControlStateNormal];
    [checkBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    checkBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    checkBtn.backgroundColor = [UIColor orangeColor];
    checkBtn.layer.cornerRadius = checkBtn.frame.size.height/2;
    [infoView addSubview:checkBtn];
    [[checkBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self pasteboardAlertDismiss];
    }];
}

- (void)pasteboardAlertDismiss {//消失
    [self removeFromSuperview];
}

@end

粘贴板上有指定字符串时进入App即会弹出广告

上一篇下一篇

猜你喜欢

热点阅读