Ios 如何在任何uiviewcontroller中打开UIUserNotificationSettings
通常,我在Appdelegate.m中设置UIUserNotificationSettingsIos 如何在任何uiviewcontroller中打开UIUserNotificationSettings,ios,objective-c,uiviewcontroller,nsnotification,Ios,Objective C,Uiviewcontroller,Nsnotification,通常,我在Appdelegate.m中设置UIUserNotificationSettings - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //some codes UIUserNotificationSettings *settings = [UIUserNotificationSettings set
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//some codes
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert
| UIUserNotificationTypeBadge
| UIUserNotificationTypeSound
categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"Register Remote Notifications error:{%@}",[error localizedDescription]);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"Receive remote notification : %@",userInfo);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"Receive remote notification : %@",userInfo);
[[NSNotificationCenter defaultCenter]postNotificationName:@"Notification" object:self userInfo:userInfo];
}
但是现在我想在UIViewcontroller中使用它,比如UIViewcontroller中有一个UIButton,当我点击它时,它会调用这个函数,有什么想法吗?你做的基本上与按下按钮的反应相同,只需使用
UIApplication.shared
而不用AppDelegate
提供的application
,例如(Swift语法):
此外,我建议您实现自己的自定义UserNotificationManager
:
class UserNotificationManager: NSObject, UNUserNotificationCenterDelegate { ... }
试试这个:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"Receive remote notification : %@",userInfo);
YourViewController *login=[storyboard instantiateViewControllerWithIdentifier:@"YourViewContollerIDName"];
MainNavigationViewController *mainNavigation=[storyboard instantiateViewControllerWithIdentifier:@"MainNavigationViewController"];
[mainNavigation setViewControllers:@[login] animated:NO];
self.window.rootViewController=mainNavigation;
}
或
Appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//some codes
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert
| UIUserNotificationTypeBadge
| UIUserNotificationTypeSound
categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"Register Remote Notifications error:{%@}",[error localizedDescription]);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"Receive remote notification : %@",userInfo);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"Receive remote notification : %@",userInfo);
[[NSNotificationCenter defaultCenter]postNotificationName:@"Notification" object:self userInfo:userInfo];
}
YourViewController.m
-(void)viewWillAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveNotification:)
name:@"Notification"
object:nil];
}
- (void)receiveNotification:(NSNotification *) notification{
if ([[notification name] isEqualToString:@"Notification"]) {
//call your function
}
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
我希望下面的代码对您有所帮助 创建一个名为UIUserNotifications的新类,并将以下代码添加到.h文件中
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface UIUserNotificationsHandler : NSObject
+ (void)registerUserNotifications;
@end