簡體   English   中英

檢查是否啟用了推送或啟用了位置的iOS SDK

[英]Check if push enabled or location enabled ios sdk

我正在使用以下代碼來檢測這2個(推送通知和定位服務)

    [postvars setObject:([CLLocationManager locationServicesEnabled])?@"true":@"false" forKey:@"locationServicesEnabled"];

    BOOL pushEnabled = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
    [postvars setObject:(pushEnabled)?@"true":@"false" forKey:@"pushServicesEnabled"];

但是問題是,即使我在應用程序中出現提示時點按“不允許”,我對這兩者也總是一視同仁。 同樣在設置應用程序中,我檢查了“位置”是否設置為“從不”,並且顯示了通知子標題。 此代碼有什么問題? 誰能在這方面指導我。

僅檢查[CLLocationManager locationServicesEnabled]是不夠的。

if([CLLocationManager locationServicesEnabled] && 
   [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied)
{
    [postvars setObject:@"true" forKey:@"locationServicesEnabled"];

}

對於通知,請查看此很棒的答案

BOOL pushEnabled = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
[postvars setObject:(pushEnabled)?@"true":@"false" forKey:@"pushServicesEnabled"];

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone) 
   {
    [postvars setObject:@"false" forKey:@"pushServicesEnabled"];
}

檢查是否啟用了推送通知或啟用了位置服務:

推送通知:

從8.0以上的iOS版本開始,即使用戶選擇退出推送,它也會注冊設備並提供令牌。

但是,在發送推送后,推送不會顯示給用戶,因此它將返回true

- (BOOL)isPushNotificationsEnabled {
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]) {
        UIUserNotificationType types = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];
        return (types & UIUserNotificationTypeAlert);
    }
    else {
        UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        return (types & UIRemoteNotificationTypeAlert);
    }
}

enabledRemoteNotificationTypes因為iOS8上已經過時了。

要在iOS8中檢查遠程通知狀態,您可以使用:

[[UIApplication sharedApplication] isRegisteredForRemoteNotifications];

在此處查看答案: 檢測iOS8的“允許通知”處於打開/關閉狀態

在此處閱讀文檔:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html#//apple_ref/occ/instm/UIApplication/currentUserNotificationSettings

也可以在這里閱讀線程: https : //forums.developer.apple.com/thread/16958

位置服務:

需要檢查locationServicesEnabledauthorizationStatus例如,

if([CLLocationManager locationServicesEnabled] && 
   [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied)
{
   //Enabled
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM