iPhone開發(fā)基礎(chǔ)學(xué)習(xí) 在程序里設(shè)置Push
iPhone開發(fā)基礎(chǔ)學(xué)習(xí) 在程序里設(shè)置Push是本文要介紹的內(nèi)容,最近做項(xiàng)目有一個(gè)需求,要在程序得系統(tǒng)設(shè)置里進(jìn)行push的設(shè)置。在網(wǎng)上搜了幾天資料沒找著啥。今天忽然心血來潮跟蹤系統(tǒng)注冊(cè)push時(shí)得代碼,居然發(fā)現(xiàn)有可行得解決方法,思路如下:
1、在iphone得framework里的UIApplication.h中有以下函數(shù):
- @interface UIApplication (UIRemoteNotifications)
- - (void)registerForRemoteNotificationTypes:(UIRemoteNotificationType)types __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
- - (void)unregisterForRemoteNotifications __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
- // calls -registerForRemoteNotificationTypes with UIRemoteNotificationTypeNone
- // returns the enabled types, also taking into account any systemwide settings; doesn't relate to connectivity
- - (UIRemoteNotificationType)enabledRemoteNotificationTypes __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
- @end
2、首先可以用[[UIApplication sharedApplication] enabledRemoteNotificationTypes]獲取到允許得push推送類型。然后再調(diào)用registerForRemoteNotificationTypes進(jìn)行修改。若要關(guān)閉程序得push服務(wù),可調(diào)用unregisterForRemoteNotifications.
3、補(bǔ)充:以上想法以實(shí)現(xiàn)。補(bǔ)充部分代碼。settingsData為tableview的數(shù)據(jù)源數(shù)組
a、獲取系push設(shè)置,用于顯示給用戶
- //push設(shè)置
- NSMutableArray * pushOptions = [[NSMutableArray alloc] init];
- UIRemoteNotificationType notificationType = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
- NSMutableDictionary * soundNotice = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
- @"聲音", @"name",
- @"0", @"status",
- nil];
- if (notificationType & UIRemoteNotificationTypeSound) {
- [soundNotice setValue:@"1" forKey:@"status"];
- }
- [pushOptions addObject:soundNotice];
- [soundNotice release];
- NSMutableDictionary * alertNotice = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
- @"提醒", @"name",
- @"0", @"status",
- nil];
- if (notificationType & UIRemoteNotificationTypeAlert) {
- [alertNotice setValue:@"1" forKey:@"status"];
- }
- [pushOptions addObject:alertNotice];
- [alertNotice release];
- NSMutableDictionary * badgeNotice = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
- @"標(biāo)記", @"name",
- @"0", @"status",
- nil];
- if (notificationType & UIRemoteNotificationTypeBadge) {
- [badgeNotice setValue:@"1" forKey:@"status"];
- }
- [pushOptions addObject:badgeNotice];
- [badgeNotice release];
- NSDictionary * pushConfig = [[NSDictionary alloc] initWithObjectsAndKeys:
- @"通知設(shè)置", @"groupName",
- pushOptions, @"data",
- nil];
- [self.settingsData addObject:pushConfig];
- [pushOptions release];
- [pushConfig release];
b、獲取用戶設(shè)置的數(shù)據(jù)放入pushdata,然后向系統(tǒng)提交設(shè)置
- NSArray * pushData = [[settingsData objectAtIndex:indexPath.section] objectForKey:@"data"];
- NSInteger length = [pushData count];
- UIRemoteNotificationType myType = 0;
- for (NSInteger i =0; i< length; i++) {
- if ([[[pushData objectAtIndex:i] objectForKey:@"status"] intValue] ==1) {
- switch (i) {
- case 0: myTypemyType = myType|UIRemoteNotificationTypeSound; break;
- case 1: myTypemyType = myType|UIRemoteNotificationTypeAlert; break;
- case 2: myTypemyType = myType|UIRemoteNotificationTypeBadge; break;
- default: break;
- }
- }
- }
- if (myType != 0) {
- [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myType];
- }else {
- [[UIApplication sharedApplication] unregisterForRemoteNotifications];
- }
希望以上思路對(duì)有這方面需求得人有幫助。以上方案我暫未用于代碼實(shí)現(xiàn)。若有問題。請(qǐng)留言共同商討。
小結(jié):iPhone開發(fā)基礎(chǔ)學(xué)習(xí) 在程序里設(shè)置Push的內(nèi)容介紹完了,希望本文對(duì)你有所幫助1更多相關(guān)內(nèi)容請(qǐng)參考編輯推薦。