您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

打造簡便的 iOS 提醒管理類 LKAlarmManager:當鬧鐘也能輕鬆搞定

編輯:iOS 開發綜合

本文分享一個方便好用的提醒管理類 LKAlarmManager,能快速把「您的提醒」加入行事曆或本地通知,並自動處理本地通知超過 64 個上限的情況。有問題或想交流改進,歡迎一起討論。

使用步驟一:轉發 AppDelegate 回調

把三個 UIApplication 回調交給 LKAlarmManager 處理:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [[LKAlarmMamager shareManager] didFinishLaunchingWithOptions:launchOptions];
    return YES;
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    [[LKAlarmMamager shareManager] handleOpenURL:url];
    return YES;
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    [[LKAlarmMamager shareManager] didReceiveLocalNotification:notification];
}

使用步驟二:加入提醒事件

建立一個 LKAlarmEvent,設定標題、重複類型與觸發時間,再交給管理器加入。系統會優先嘗試加入行事曆,若沒有權限則改加入本地通知:

LKAlarmEvent* event = [LKAlarmEvent new];
event.title = @"請加入行事曆事件中";
event.content = @"只有加入行事曆才有用,是行事曆中的備註";
event.repeatType = LKAlarmRepeatTypeWork; // 工作日提醒
event.startDate = [NSDate dateWithTimeIntervalSinceNow:60]; // 60 秒後提醒
// 也可強制加入本地提醒
// event.isNeedJoinLocalNotify = YES;

[[LKAlarmMamager shareManager] addAlarmEvent:event callback:^(LKAlarmEvent *alarmEvent) {
    dispatch_async(dispatch_get_main_queue(), ^{
        UILabel* label = ((ViewController*)_window.rootViewController).lb_haha;
        if (alarmEvent.isJoinedCalendar) {
            label.text = @"已加入行事曆";
        } else if (alarmEvent.isJoinedLocalNotify) {
            label.text = @"已加入本地通知";
        } else {
            label.text = @"加入通知失敗";
        }
    });
}];

使用步驟三:註冊回調接收提醒

註冊 LKAlarmManager 的代理後,當收到提醒時就能做你想做的事,例如彈出提示:

// 註冊代理
[[LKAlarmMamager shareManager] registDelegateWithObject:self];

- (void)lk_receiveAlarmEvent:(LKAlarmEvent *)event {
    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"接收到通知!" message:event.title delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];
    [alertView show];
}

透過這套封装,提醒的加入、去重與接收都被統一管理,無論是當作鬧鐘還是行事曆提醒,都能輕鬆勝任。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved