iOS检测手机是否为振动模式(非铃声模式)

  最近公司的项目中有一个需求就是检测手机为振动模式时,app不再播放背景音乐。搜索了一下iOS好像没有专门的方法去监听手机是否为振动模式,搜了好久终于找到一篇文章介绍一位大神,通过播放一段没有声音的音频的途径实现了判断。原文地址:https://blog.csdn.net/weasleyqi/article/details/11593313 ,github地址:https://github.com/Rich2k/RBDMuteSwitch 我接入代码调试了一下,发现大神的代码是可以用的,为了方便实用我把代码封装成了一个pod 可以使用 pod 'JKRBDMuteSwitch' 集成到项目中去,改造后的源码地址:https://github.com/xindizhiyin2014/JKRBDMuteSwitch
非常感谢这位大神的付出,不然还不知道要多花多少工夫呢。
使用方法和之前也是一模一样,没有变化,具体代码如下:


    #import <JKRBDMuteSwitch/RBDMuteSwitch.h>  //引入头文件


    @interface MainViewController : UIViewController <RBDMuteSwitchDelegate>
//实现这两句代码
    [[RBDMuteSwitch sharedInstance] setDelegate:self];
    [[RBDMuteSwitch sharedInstance] detectMuteSwitch];

//代理方法
    - (void)isMuted:(BOOL)muted {
        if (muted) {
            NSLog("Muted");
        }
        else {
            NSLog("Not Muted");
        }
    }

更多优质文章,可以微信扫码关注:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/HHL110120/article/details/88313733
今日推荐