iOS中双启动页以及铃声播放(仿酷我音乐盒)

上周闲来没事,听酷我音乐盒的时候发现每次启动,都会有两次启动页的情况(它的第二次启动页多半是名人打的广告),并且会伴随铃声“听音乐,用酷我”,花了点时间研究了下,并集成到了项目里,发现也就这么简单的实现了,代码不难,提供思路,代码如下:

#import "LaunchViewController.h"
#import "MainViewController.h"
#import "AppDelegate.h"
#import 
@interface LaunchViewController ()

@end

@implementation LaunchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"launch"]) {
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"launch"];
        UIImageView * backImage = [[UIImageView alloc]initWithFrame:self.view.bounds];
        backImage.image = [UIImage imageNamed:@"first_banner1"];
        [self.view addSubview:backImage];
        [self performSelector:@selector(handlePer) withObject:nil afterDelay:2.0];
        [self playNotifySound];
    } else {
      [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"launch"];
        [self handlePer];
    }
    
}

- (void)handlePer{
    AppDelegate * dele = (AppDelegate *)[UIApplication sharedApplication].delegate;
    dele.window.rootViewController = [MainViewController new];
    
}

//必须得是自定义的声音,经过测试系统的声音好像都带振动
- (void)playNotifySound {
    //获取路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"candoNotifySound" ofType:@"mp3"];
    //定义一个带振动的SystemSoundID
    SystemSoundID soundID = 1000;
    //判断路径是否存在
    if (path) {
        //创建一个音频文件的播放系统声音服务器
        OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)([NSURL fileURLWithPath:path]), &soundID);
        //判断是否有错误
        if (error != kAudioServicesNoError) {
            NSLog(@"%d",(int)error);
        }
    }
    //只播放声音,没振动
    AudioServicesPlaySystemSound(soundID);
}

简单解释一下,在appdelegate中设置视图的根视图为LaunchViewController,然后再LaunchViewController根据不同情况(在这里根据我本地存储的YES或者NO来区别)进行交替执行单启动页或者双启动页,最后的方法是添加铃声播放。是不是很简单实用呢~
知识在于分享,感谢阅读,愿你我共同进步。

你可能感兴趣的:(iOS中双启动页以及铃声播放(仿酷我音乐盒))