Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在任何uiviewcontroller中打开UIUserNotificationSettings_Ios_Objective C_Uiviewcontroller_Nsnotification - Fatal编程技术网

Ios 如何在任何uiviewcontroller中打开UIUserNotificationSettings

Ios 如何在任何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

通常,我在Appdelegate.m中设置UIUserNotificationSettings

 - (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