iOS 类似复制链接打开淘宝APP后弹出页面功能

写在前面

我们应该都有用过这个功能,你的朋友微信给你分享了一个淘宝里面的商品链接,然后当你复制这个链接打开淘宝APP的时候,就会弹出一个弹窗,像这样:


example.PNG

这个功能想必大家都挺熟悉,受这个启发我们产品也想在我们APP上添加这样一个功能,与这个不一样的是,当我们复制一段网址的时候打开我们的APP会弹出框填一些信息后上传到我们的“资源库”。大体功能就这样,所以记录一下实现的过程。

一、弹窗视图功能

.h中:两个信号一个是确定信号一个是取消信号
两个方法,一个显示一个隐藏方法

@property (nonatomic, strong) RACSubject *uploadSureSignal;//确定上传信号
@property (nonatomic, strong) RACSubject *hideSucSignal;//隐藏

- (void)show;
- (void)hide;

.m中:主要是两个textview,还有涉及到在keywindow上,IQKeyboard的一些操作

@property (nonatomic, assign) CGFloat keyboardHeight;//键盘高度
@property (nonatomic, strong) CustomUITextView *nameTV;
@property (nonatomic, strong) CustomUITextView *desTV;

因为发现IQKeyboard在这个弹出界面有问题,所以在显示这个界面的时候,将IQKeyboard禁用取之使用系统的keyboard监听方法

在(void)show方法中:
-(void)show {
//键盘通知
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    
    [defaultCenter addObserver:self selector:@selector(keyboardWillShowOrHide:) name:UIKeyboardWillShowNotification object:nil];
    
    [defaultCenter addObserver:self selector:@selector(keyboardWillShowOrHide:) name:UIKeyboardWillHideNotification object:nil];
}
//监听方法
- (void)keyboardWillShowOrHide:(NSNotification *)notification {
    //获取通知名
    NSString *notificationName = notification.name;
    //获取通知内容
    NSDictionary *keyboardInfo = notification.userInfo;
    //键盘弹出时,让画面整体稍稍上移,并伴随动画
    //键盘回收时反之
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat height = keyboardFrame.size.height;
    self.keyboardHeight = height;
    //动画结束后self.view的frame值
    CGRect selfViewFrame = self.bgView.frame;
    //通过通知名字判断弹出还是回收
    if ([notificationName isEqualToString:UIKeyboardWillShowNotification]) {
        selfViewFrame.origin.y = SCREEN_HEIGHT - PANELHEIGHT - height;
    } else {
        selfViewFrame.origin.y = SCREEN_HEIGHT - PANELHEIGHT;
    }
    
    //取出动画时长
    NSTimeInterval duration = [keyboardInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    //使用动画更改self.view.frame
    [UIView animateWithDuration:duration animations:^{
        //这里填入一些view的最终状态属性设置,即会自动产生过渡动画
        self.bgView.frame = selfViewFrame;
    }];
    
}

同时在show方法中显示keyWindow,进而改变界面的frame进行显示

- (void)show {
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self];
    CGRect frame = self.bgView.frame;
    if (frame.origin.y == SCREEN_HEIGHT) {
        frame.origin.y = SCREEN_HEIGHT - PANELHEIGHT;
        [UIView animateWithDuration:0.4 animations:^{
            self.bgView.frame = frame;
        }];
    }

hide方法这里要考虑到键盘弹出后将self.bgView向上提高后frame的变化。

CGRect selfFrame = self.bgView.frame;
    if (selfFrame.origin.y == SCREEN_HEIGHT - PANELHEIGHT || selfFrame.origin.y == SCREEN_HEIGHT - PANELHEIGHT - self.keyboardHeight) {
        [self resignFirstResponder];
        selfFrame.origin.y = SCREEN_HEIGHT;
        [UIView animateWithDuration:0.4 animations:^{
            self.bgView.frame = selfFrame;
        } completion:^(BOOL finished) {
            [IQKeyboardManager sharedManager].enable = YES;
            [[NSNotificationCenter defaultCenter] removeObserver:self];
//            [self.hideSucSignal sendNext:nil];
            [self removeFromSuperview];
        }];
    }
delegate中的操作

这里首先要弄懂APPdelegate中的这几个代理方法的意思:

//App已经启动
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    return YES;
}

//App挂起状态
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

//APP进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

//APP将重新回到前台
- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

//APP进入活跃状态
- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

//系统时间发生改变时执行
- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

在上面的这些代理方法中,我们需要用到的是 applicationDidBecomeActive方法。在这个方法中我们去检查系统的粘贴板UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

if (pasteboard.string) {
            NSLog(@"string:%@", pasteboard.string);
            NSString *urlStr = pasteboard.string;
            if ([urlStr hasPrefix:@"https://"] || [urlStr hasPrefix:@"http://"]) {
//如果粘贴板中的字符串包含https或http字段,我们去检查当前的控制器 如果当前的控制器是我们弹出做操作的控制器的话 isPopVC = NO;
                BOOL isPopVC = NO;
                UIViewController * Rootvc = self.window.rootViewController;
                if ([Rootvc isKindOfClass:[UINavigationController class]]) {
                    UINavigationController * nav = (UINavigationController *)Rootvc;
                    UIViewController * v = [nav.viewControllers lastObject];
                    if ([v isKindOfClass:[UploadResCofingVC class]]) {
                        isPopVC = YES;
                    }
                }
                //如果popView == nil 并且isPopVC == NO 弹出popView弹窗视图 进行操作
                if (!self.popView && !isPopVC) {
                    UploadResourcesPopupView *popView = [UploadResourcesPopupView new];
                    [popView show];
                    self.popView = popView;
                    [self.popView.hideSucSignal subscribeNext:^(id x) {
                        @strongify(self);
                        self.popView = nil;
                    }];
                }
            }
        }
    }
总结

以上大体就是实现这个功能的基本思路,细节方面因项目而异了,比如我们需要判断当前用户的角色,当前用户是否登录,对弹窗视图后续的一些操作。当然并不完美,欢迎批评指正。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,657评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,889评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,057评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,509评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,562评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,443评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,251评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,129评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,561评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,779评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,902评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,621评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,220评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,838评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,971评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,025评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,843评论 2 354

推荐阅读更多精彩内容

  • 一本书就是一张藏宝图,虽然,不是每张都能找到宝藏…… 书本被遗忘在角落里,偶然被拾起,但人们大多不愿意费心读懂它们...
    杏仁BOOK阅读 579评论 0 0
  • 我在台上神采奕奕,精神抖擞的讲啊讲啊,学生在下面静得水平如静,我尽量平定情绪鼓励大家积极发言,可还是静的让...
    陶新霞阅读 226评论 1 2
  • 不必硬撑 我自己有过很多次硬撑的经历,也曾经看到过别人硬撑。基本上,硬撑,是职场必备的素质。不过,是否要一直、长期...
    做自己的CEO阅读 341评论 1 1
  • “把我的扇子还给我!” “呦,大美人,说话挺冲啊,求我,我就给你。”靳祈说了一句这么不要脸的话后,又在万俟凡耳边蹭...
    不朽jun阅读 269评论 0 0