欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS开发检测是否开启定位、是否允许消息推送等权限的实例

程序员文章站 2023-12-19 12:12:22
1.ios开发检测是否开启定位: 需要导入: #import 代码如下:...

1.ios开发检测是否开启定位:

需要导入:

#import <corelocation/corelocation.h> 

代码如下:

+ (void)openlocationservicewithblock:(returnblock)returnblock 
{ 
  bool isopen = no; 
  if ([cllocationmanager locationservicesenabled] && [cllocationmanager authorizationstatus] != kclauthorizationstatusdenied) { 
    isopen = yes; 
  } 
  if (returnblock) { 
    returnblock(isopen); 
  } 
} 

2.ios开发检测是否允许消息推送:

需要导入:

#import <usernotifications/usernotifications.h> 

代码如下:

+ (void)openmessagenotificationservicewithblock:(returnblock)returnblock 
{ 
 bool isopen = no; 
#if __iphone_os_version_max_allowed >= __iphone_8_0 
 uiusernotificationsettings *setting = [[uiapplication sharedapplication] currentusernotificationsettings]; 
 if (setting.types != uiusernotificationtypenone) { 
  isopen = yes; 
 } 
#else 
 uiremotenotificationtype type = [[uiapplication sharedapplication] enabledremotenotificationtypes]; 
 if (type != uiremotenotificationtypenone) { 
  isopen = yes; 
 } 
#endif 
 if (returnblock) { 
  returnblock(isopen); 
 } 
} 
+ (void)openmessagenotificationservicewithblock:(returnblock)returnblock 
{ 
#if __iphone_os_version_max_allowed >= __iphone_10_0 
 [[unusernotificationcenter currentnotificationcenter] getnotificationsettingswithcompletionhandler:^(unnotificationsettings *settings) { 
  if (returnblock) { 
   returnblock(settings.authorizationstatus == unauthorizationstatusauthorized); 
  } 
 }]; 
#elif __iphone_os_version_max_allowed >= __iphone_8_0 
 returnblock([[uiapplication sharedapplication] isregisteredforremotenotifications]); 
#else 
 uiremotenotificationtype type = [[uiapplication sharedapplication] enabledremotenotificationtypes]; 
 if (returnblock) { 
  returnblock(type != uiremotenotificationtypenone); 
 } 
#endif 
} 

3.ios开发检测是否开启摄像头:

需要导入:

#import <avfoundation/avfoundation.h> 

代码如下:

+ (void)opencapturedeviceservicewithblock:(returnblock)returnblock 
{ 
#if __iphone_os_version_max_allowed >= __iphone_7_0 
  avauthorizationstatus authstatus = [avcapturedevice authorizationstatusformediatype:avmediatypevideo]; 
  if (authstatus == avauthorizationstatusnotdetermined) { 
    [avcapturedevice requestaccessformediatype:avmediatypevideo completionhandler:^(bool granted) { 
      if (returnblock) { 
        returnblock(granted); 
      } 
    }]; 
    return no; 
  } else if (authstatus == avauthorizationstatusrestricted || authstatus == avauthorizationstatusdenied) { 
    returnblock(no); 
  } else { 
    returnblock(yes); 
  } 
#endif 
} 

4.ios开发检测是否开启相册:

需要导入:

#import <photos/photos.h>#import <assetslibrary/assetslibrary.h> 

代码如下:

+ (void)openalbumservicewithblock:(returnblock)returnblock 
{ 
  bool isopen; 
#if __iphone_os_version_max_allowed >= __iphone_8_0 
  phauthorizationstatus authstatus = [phphotolibrary authorizationstatus]; 
  isopen = yes; 
  if (authstatus == phauthorizationstatusrestricted || authstatus == phauthorizationstatusdenied) { 
    isopen = no; 
  } 
#else 
  alauthorizationstatus author = [alassetslibrary authorizationstatus]; 
  isopen = yes; 
  if (author == alauthorizationstatusrestricted || author == alauthorizationstatusdenied) { 
    isopen = no; 
  } 
#endif 
  if (returnblock) { 
    returnblock(isopen); 
  } 
} 

5.ios开发检测是否开启麦克风:

需要导入:

#import <avfoundation/avfoundation.h> 

代码如下:

+ (void)openrecordservicewithblock:(returnblock)returnblock 
{ 
#if __iphone_os_version_max_allowed >= __iphone_8_0 
  avaudiosessionrecordpermission permissionstatus = [[avaudiosession sharedinstance] recordpermission]; 
  if (permissionstatus == avaudiosessionrecordpermissionundetermined) { 
    [[avaudiosession sharedinstance] requestrecordpermission:^(bool granted) { 
      if (returnblock) { 
        returnblock(granted); 
      } 
    }]; 
  } else if (permissionstatus == avaudiosessionrecordpermissiondenied) { 
    returnblock(no); 
  } else { 
    returnblock(yes); 
  } 
#endif 
} 

6.ios开发检测是否开启通讯录:

需要导入:

#import <addressbook/addressbook.h>#import <contacts/contacts.h> 

代码如下:

+ (void)opencontactsservicewithbolck:(returnblock)returnbolck
{
#if __iphone_os_version_max_allowed >= __iphone_9_0
 cnauthorizationstatus cnauthstatus = [cncontactstore authorizationstatusforentitytype:cnentitytypecontacts];
 if (cnauthstatus == cnauthorizationstatusnotdetermined) {
  cncontactstore *store = [[cncontactstore alloc] init];
  [store requestaccessforentitytype:cnentitytypecontacts completionhandler:^(bool granted, nserror *error) {
   if (returnbolck) {
    returnbolck(granted);
   }
  }];
 } else if (cnauthstatus == cnauthorizationstatusrestricted || cnauthstatus == cnauthorizationstatusdenied) {
  if (returnbolck) {
   returnbolck(no);
  }
 } else {
  if (returnbolck) {
   returnbolck(yes);
  }
 }
#else
 //abaddressbookref addressbook = abaddressbookcreatewithoptions(null, null);
 abaddressbookref addressbook = abaddressbookcreate();
 abauthorizationstatus authstatus = abaddressbookgetauthorizationstatus();
 if (authstatus != kabauthorizationstatusauthorized) {
  abaddressbookrequestaccesswithcompletion(addressbook, ^(bool granted, cferrorref error) {
   dispatch_async(dispatch_get_main_queue(), ^{
    if (error) {
     nslog(@"error: %@", (__bridge nserror *)error);
     if (returnbolck) {
      returnbolck(no);
     }
    } else {
     if (returnbolck) {
      returnbolck(yes);
     }
    }
   });
  });
 } else {
  if (returnbolck) {
   returnbolck(yes);
  }
 }
#endif
}

7.ios开发检测是否开启蓝牙:

需要导入:

#import <corebluetooth/corebluetooth.h> 

代码如下:

+ (void)openperipheralservicewithbolck:(returnblock)returnbolck 
{ 
#if __iphone_os_version_max_allowed >= __iphone_7_0 
  cbperipheralmanagerauthorizationstatus cbauthstatus = [cbperipheralmanager authorizationstatus]; 
  if (cbauthstatus == cbperipheralmanagerauthorizationstatusnotdetermined) { 
    if (returnbolck) { 
      returnbolck(no); 
    } 
  } else if (cbauthstatus == cbperipheralmanagerauthorizationstatusrestricted || cbauthstatus == cbperipheralmanagerauthorizationstatusdenied) { 
    if (returnbolck) { 
      returnbolck(no); 
    } 
  } else { 
    if (returnbolck) { 
      returnbolck(yes); 
    } 
  } 
#endif 
} 

8.ios开发检测是否开启日历/备忘录:

需要导入:

#import <eventkit/eventkit.h> 

代码如下:

+ (void)openeventservicewithbolck:(returnblock)returnbolck withtype:(ekentitytype)entitytype 
{ 
  // ekentitytypeevent  代表日历 
  // ekentitytypereminder 代表备忘 
  ekauthorizationstatus ekauthstatus = [ekeventstore authorizationstatusforentitytype:entitytype]; 
  if (ekauthstatus == ekauthorizationstatusnotdetermined) { 
    ekeventstore *store = [[ekeventstore alloc] init]; 
    [store requestaccesstoentitytype:entitytype completion:^(bool granted, nserror *error) { 
      if (returnbolck) { 
        returnbolck(granted); 
      } 
    }]; 
  } else if (ekauthstatus == ekauthorizationstatusrestricted || ekauthstatus == ekauthorizationstatusdenied) { 
    if (returnbolck) { 
      returnbolck(no); 
    } 
  } else { 
    if (returnbolck) { 
      returnbolck(yes); 
    } 
  } 
} 

9.ios开发检测是否开启联网:

需要导入:

#import <coretelephony/ctcellulardata.h> 

代码如下:

+ (void)openeventservicewithbolck:(returnblock)returnbolck 
{ 
#if __iphone_os_version_max_allowed >= __iphone_9_0 
  ctcellulardata *cellulardata = [[ctcellulardata alloc] init]; 
  cellulardata.cellulardatarestrictiondidupdatenotifier = ^(ctcellulardatarestrictedstate state){ 
    if (state == kctcellulardatarestrictedstateunknown || state == kctcellulardatanotrestricted) { 
      if (returnbolck) { 
        returnbolck(no); 
      } 
    } else { 
      if (returnbolck) { 
        returnbolck(yes); 
      } 
    } 
  }; 
  ctcellulardatarestrictedstate state = cellulardata.restrictedstate; 
  if (state == kctcellulardatarestrictedstateunknown || state == kctcellulardatanotrestricted) { 
    if (returnbolck) { 
      returnbolck(no); 
    } 
  } else { 
    if (returnbolck) { 
      returnbolck(yes); 
    } 
  } 
#endif 
} 

10.ios开发检测是否开启健康:

需要导入:

#import <healthkit/healthkit.h> 

代码如下:

+ (void)openhealthservicewithbolck:(returnblock)returnbolck
{
#if __iphone_os_version_max_allowed >= __iphone_8_0
 if (![hkhealthstore ishealthdataavailable]) {
  if (returnbolck) {
   returnbolck(no);
  }
 } else {
  hkhealthstore *healthstore = [[hkhealthstore alloc] init];
  hkobjecttype *hkobjecttype = [hkobjecttype quantitytypeforidentifier:hkquantitytypeidentifierheight];
  hkauthorizationstatus hkauthstatus = [healthstore authorizationstatusfortype:hkobjecttype];
  if (hkauthstatus == hkauthorizationstatusnotdetermined) {
   // 1. 你创建了一个nsset对象,里面存有本篇教程中你将需要用到的从health stroe中读取的所有的类型:个人特征(血液类型、性别、出生日期)、数据采样信息(身体质量、身高)以及锻炼与健身的信息。
   nsset <hkobjecttype *> * healthkittypestoread = [[nsset alloc] initwitharray:@[[hkobjecttype characteristictypeforidentifier:hkcharacteristictypeidentifierdateofbirth],[hkobjecttype characteristictypeforidentifier:hkcharacteristictypeidentifierbloodtype],[hkobjecttype characteristictypeforidentifier:hkcharacteristictypeidentifierbiologicalsex],[hkobjecttype quantitytypeforidentifier:hkquantitytypeidentifierbodymass],[hkobjecttype quantitytypeforidentifier:hkquantitytypeidentifierheight],[hkobjecttype workouttype]]];
   // 2. 你创建了另一个nsset对象,里面有你需要向store写入的信息的所有类型(锻炼与健身的信息、bmi、能量消耗、运动距离)
   nsset <hksampletype *> * healthkittypestowrite = [[nsset alloc] initwitharray:@[[hkobjecttype quantitytypeforidentifier:hkquantitytypeidentifierbodymassindex],[hkobjecttype quantitytypeforidentifier:hkquantitytypeidentifieractiveenergyburned],[hkobjecttype quantitytypeforidentifier:hkquantitytypeidentifierdistancewalkingrunning],[hkobjecttype workouttype]]];
   [healthstore requestauthorizationtosharetypes:healthkittypestowrite readtypes:healthkittypestoread completion:^(bool success, nserror *error) {
    if (returnbolck) {
     returnbolck(success);
    }
   }];
  } else if (hkauthstatus == hkauthorizationstatussharingdenied) {
   if (returnbolck) {
    returnbolck(no);
   }
  } else {
   if (returnbolck) {
    returnbolck(yes);
   }
  }
 }
#endif
}

11.ios开发检测是否开启touch id:

需要导入:

#import <localauthentication/localauthentication.h>

代码如下:

#pragma mark - 开启touch id服务
+ (void)opentouchidservicewithblock:(returnblock)returnblock
{
#if __iphone_os_version_max_allowed >= __iphone_8_0
 lacontext *lacontext = [[lacontext alloc] init];
 lacontext.localizedfallbacktitle = @"输入密码";
 nserror *error;
 if ([lacontext canevaluatepolicy:lapolicydeviceownerauthenticationwithbiometrics error:&error]) {
  nslog(@"恭喜,touch id可以使用!");
  [lacontext evaluatepolicy:lapolicydeviceownerauthenticationwithbiometrics localizedreason:@"需要验证您的指纹来确认您的身份信息" reply:^(bool success, nserror *error) {
   if (success) {
    // 识别成功
    if (returnblock) {
     [[nsoperationqueue mainqueue] addoperationwithblock:^{
      returnblock(yes);
     }];
    }
   } else if (error) {
    if (returnblock) {
     [[nsoperationqueue mainqueue] addoperationwithblock:^{
      returnblock(no);
     }];
    }
    if (error.code == laerrorauthenticationfailed) {
     // 验证失败
    }
    if (error.code == laerrorusercancel) {
     // 用户取消
    }
    if (error.code == laerroruserfallback) {
     // 用户选择输入密码
    }
    if (error.code == laerrorsystemcancel) {
     // 系统取消
    }
    if (error.code == laerrorpasscodenotset) {
     // 密码没有设置
    }
   }
  }];
 } else {
  nslog(@"设备不支持touch id功能,原因:%@",error);
  if (returnblock) {
   returnblock(no);
  }
 }
#endif
}

12.ios开发检测是否开启apple pay:

需要导入:

#import <passkit/passkit.h> 

代码如下:

#pragma mark - 开启apple pay服务 
+ (void)openapplepayservicewithblock:(returnblock)returnblock 
{ 
#if __iphone_os_version_max_allowed >= __iphone_9_0 
  nsarray<pkpaymentnetwork> *supportednetworks = @[pkpaymentnetworkamex, pkpaymentnetworkmastercard, pkpaymentnetworkvisa, pkpaymentnetworkdiscover]; 
  if ([pkpaymentauthorizationviewcontroller canmakepayments] && [pkpaymentauthorizationviewcontroller canmakepaymentsusingnetworks:supportednetworks]) { 
    if (returnblock) { 
      returnblock(yes); 
    } 
  } else { 
    if (returnblock) { 
      returnblock(no); 
    } 
  } 
#endif 
} 

13.ios开发检测是否开启语音识别:

需要导入:

#import <speech/speech.h> 

代码如下:

#pragma mark - 开启语音识别服务
+ (void)openspeechservicewithblock:(returnblock)returnblock
{
#if __iphone_os_version_max_allowed >= __iphone_10_0
 sfspeechrecognizerauthorizationstatus speechauthstatus = [sfspeechrecognizer authorizationstatus];
 if (speechauthstatus == sfspeechrecognizerauthorizationstatusnotdetermined) {
  [sfspeechrecognizer requestauthorization:^(sfspeechrecognizerauthorizationstatus status) {
   if (status == sfspeechrecognizerauthorizationstatusauthorized) {
    dispatch_async(dispatch_get_main_queue(), ^{
     if (returnblock) {
      returnblock(yes);
     }
    });
   } else {
    dispatch_async(dispatch_get_main_queue(), ^{
     if (returnblock) {
      returnblock(yes);
     }
    });
   }
  }];
 } else if (speechauthstatus == sfspeechrecognizerauthorizationstatusauthorized) {
  if (returnblock) {
   returnblock(yes);
  }
 } else{
  if (returnblock) {
   returnblock(no);
  }
 }
#endif
}

14.ios开发检测是否开启媒体资料库/apple music:

需要导入:

#import <mediaplayer/mediaplayer.h> 

代码如下:

#pragma mark - 开启媒体资料库/apple music 服务
+ (void)openmediaplayerservicewithblock:(returnblock)returnblock
{
#if __iphone_os_version_max_allowed >= __iphone_9_3
 mpmedialibraryauthorizationstatus authstatus = [mpmedialibrary authorizationstatus];
 if (authstatus == mpmedialibraryauthorizationstatusnotdetermined) {
  [mpmedialibrary requestauthorization:^(mpmedialibraryauthorizationstatus status) {
   if (status == mpmedialibraryauthorizationstatusauthorized) {
    dispatch_async(dispatch_get_main_queue(), ^{
     if (returnblock) {
      returnblock(yes);
     }
    });
   }else{
    dispatch_async(dispatch_get_main_queue(), ^{
     if (returnblock) {
      returnblock(no);
     }
    });
   }
  }];
 }else if (authstatus == mpmedialibraryauthorizationstatusauthorized){
  if (returnblock) {
   returnblock(yes);
  }
 }else{
  if (returnblock) {
   returnblock(no);
  }
 }
#endif
}

15.ios开发检测是否开启siri:

需要导入:

#import <intents/intents.h> 

代码如下:

#pragma mark - 开启siri服务
+ (void)opensiriservicewithblock:(returnblock)returnblock
{
#if __iphone_os_version_max_allowed >= __iphone_10_0
 insiriauthorizationstatus siriautostatus = [inpreferences siriauthorizationstatus];
 if (siriautostatus == insiriauthorizationstatusnotdetermined) {
  [inpreferences requestsiriauthorization:^(insiriauthorizationstatus status) {
   if (status == insiriauthorizationstatusauthorized) {
    dispatch_async(dispatch_get_main_queue(), ^{
     if (returnblock) {
      returnblock(yes);
     }
    });
   } else {
    dispatch_async(dispatch_get_main_queue(), ^{
     if (returnblock) {
      returnblock(yes);
     }
    });
   }
  }];
 } else if (siriautostatus == insiriauthorizationstatusauthorized) {
  if (returnblock) {
   returnblock(yes);
  }
 } else{
  if (returnblock) {
   returnblock(no);
  }
 }
#endif
}

在.h文件中申明block

#if ns_blocks_available 
typedef void(^returnblock)(bool isopen); 
#endif 

由于ios10的权限原因,需要在工程的info.plist(右击选择open as - source code)中添加

<!-- 相册 --> 
<key>nsphotolibraryusagedescription</key> 
<string>app需要您的同意,才能访问相册</string> 
<!-- 相机 --> 
<key>nscamerausagedescription</key> 
<string>app需要您的同意,才能访问相机</string> 
<!-- 麦克风 --> 
<key>nsmicrophoneusagedescription</key> 
<string>app需要您的同意,才能访问麦克风</string> 
<!-- 位置 --> 
<key>nslocationusagedescription</key> 
<string>app需要您的同意,才能访问位置</string> 
<!-- 在使用期间访问位置 --> 
<key>nslocationwheninuseusagedescription</key> 
<string>app需要您的同意,才能在使用期间访问位置</string> 
<!-- 始终访问位置 --> 
<key>nslocationalwaysusagedescription</key> 
<string>app需要您的同意,才能始终访问位置</string> 
<!-- 日历 --> 
<key>nscalendarsusagedescription</key> 
<string>app需要您的同意,才能访问日历</string> 
<!-- 提醒事项 --> 
<key>nsremindersusagedescription</key> 
<string>app需要您的同意,才能访问提醒事项</string> 
<!-- 运动与健身 --> 
<key>nsmotionusagedescription</key>
 <string>app需要您的同意,才能访问运动与健身</string> 
<!-- 健康更新 --> 
<key>nshealthupdateusagedescription</key> 
<string>app需要您的同意,才能访问健康更新 </string> 
<!-- 健康分享 --> 
<key>nshealthshareusagedescription</key> 
<string>app需要您的同意,才能访问健康分享</string> 
<!-- 蓝牙 --> 
<key>nsbluetoothperipheralusagedescription</key> 
<string>app需要您的同意,才能访问蓝牙</string> 
<!-- 媒体资料库 --> 
<key>nsapplemusicusagedescription</key> 
<string>app需要您的同意,才能访问媒体资料库</string>
<!-- 语音识别 --> 
<key>nsspeechrecognitionusagedescription</key> 
<string>app需要您的同意,才能使用语音识别</string>

以上这篇ios开发检测是否开启定位、是否允许消息推送等权限的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

上一篇:

下一篇: