您的位置:首页 > 其它

实用知识:基于监听位置的本地通知 使用方法

2016-05-12 22:05 337 查看
#import "AppDelegate.h"
#import <CoreLocation/CoreLocation.h>

@interface AppDelegate () <CLLocationManagerDelegate>

@property (strong, nonatomic) CLLocationManager *manager;

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

/*================= 通知权限 =================*/
UIUserNotificationType type = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

// UIUserNotificationSettings 表示App可以使用的通知的展示类型(声音, 边缘数字, 弹窗)
// category 是用来配置 按钮的
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];

// 注册用户通知的配置(通知使用的类型), (会弹出授权窗口来请求)
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

/*================= 定位权限 =================*/
_manager = [[CLLocationManager alloc] init];
_manager.delegate = self;

//    [_manager requestWhenInUseAuthorization];
[_manager requestAlwaysAuthorization];

return YES;
}

#pragma mark - CLLocationManagerDelegate

//- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
//{
//
//}

//- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
//{
//
//}

@end


#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(@"点击了");

/*================= 召召路过云水瑶, 得到通知提醒 =================*/

// 1. 基于位置触发的本地通知
UILocalNotification *notification = [[UILocalNotification alloc] init];

// 2. 参数
//    notification.fireDate
notification.alertBody = @"欢迎召召, 请上3楼!";

// 触发通知的区域 (区域监听)   // When-In-Use与Always都可以使用
// 默认只执行一次
notification.region = [[CLCircularRegion alloc] initWithCenter:CLLocationCoordinate2DMake(23.133916, 113.395557) radius:500 identifier:@"YunShuiYao"];
// 基于区域的通知是否只执行一次
notification.regionTriggersOnce = NO;

// 3.添加到调度池中等待调度
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: