2016-06-08 70 views
0

我能夠將APNS集成到我的應用程序中。現在,我想在用戶點擊它時或在用戶在使用應用程序時收到通知時處理通知。我用下面的代碼來顯示收到通知時的警告對話框:接收到APN時的運行功能

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 
     // display the userInfo 
     if let notification = userInfo["aps"] as? NSDictionary, 
      let alert = notification["alert"] as? String { 
      var alertCtrl = UIAlertController(title: "Time Entry", message: alert as String, preferredStyle: UIAlertControllerStyle.Alert) 
      alertCtrl.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) 
      // Find the presented VC... 
      var presentedVC = self.window?.rootViewController 
      while (presentedVC!.presentedViewController != nil) { 
       presentedVC = presentedVC!.presentedViewController 
      } 
      presentedVC!.presentViewController(alertCtrl, animated: true, completion: nil) 

      // call the completion handler 
      // -- pass in NoData, since no new data was fetched from the server. 
      completionHandler(UIBackgroundFetchResult.NoData) 
     } 
    } 

是否有可能運行在我MainViewController函數而不是顯示對話框的?我試着打電話給我的功能MainViewController.refreshData(),但它總是給我一個nil

回答

1

是的它可以通過發佈和添加觀察者。

使用NSNotificationCenter來製作它。

我張貼在目標C代碼中,嘗試轉換爲Swift。

@implementation AppDelegate 

static void displayStatusChanged(CFNotificationCenterRef center, void *observer, 
          CFStringRef name, const void *object, 
          CFDictionaryRef userInfo) { 
if (name == CFSTR("com.apple.springboard.lockcomplete")) { 
    [[NSUserDefaults standardUserDefaults] setBool:YES 
              forKey:@"kDisplayStatusLocked"]; 
    [[NSUserDefaults standardUserDefaults] synchronize]; 
} 
} 


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
// Override point for customization after application launch. 
CFNotificationCenterAddObserver(
           CFNotificationCenterGetDarwinNotifyCenter(), NULL, displayStatusChanged, 
           CFSTR("com.apple.springboard.lockcomplete"), NULL, 
           CFNotificationSuspensionBehaviorDeliverImmediately); 
return YES; 
} 

然後將此代碼粘貼在didRecieveRemoteNotification

[[NSNotificationCenter defaultCenter]postNotificationName:@"TestNotification" object:self]; 

而在你MainViewController

- (void)viewDidLoad { 

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(receiveTestNotification:) 
              name:@"TestNotification" 
              object:nil]; 


    } 


- (void) receiveTestNotification:(NSNotification *) notification 
{ 

if ([[notification name] isEqualToString:@"TestNotification"]) { 
    NSLog (@"Successfully received the test notification!"); 

    //perform your operations 
    [self refreshData]; 

    } 
} 
+0

對不起,我無法將其轉換成Swift。我在C中沒有背景,我開始使用Swift學習ios開發。 –

+0

只是使用該方法,它會給你自動的建議,不用擔心太多。它會解決你的問題。 – Santo

+0

謝謝!我做的!這很簡單! –

0
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 
    // display the userInfo 
    if let notification = userInfo["aps"] as? NSDictionary, 
     let alert = notification["alert"] as? String { 
      var alertCtrl = UIAlertController(title: "Time Entry", message: alert as String, preferredStyle: UIAlertControllerStyle.Alert) 
      alertCtrl.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (UIAlertAction) -> Void in 
          //Your Function call when you click Ok Button. 
          self.YourFunc() 
          })) 
      // Find the presented VC... 
      var presentedVC = self.window?.rootViewController 
      while (presentedVC!.presentedViewController != nil) { 
       presentedVC = presentedVC!.presentedViewController 
      } 
      presentedVC!.presentViewController(alertCtrl, animated: true, completion: nil) 

      // call the completion handler 
      // -- pass in NoData, since no new data was fetched from the server. 
      completionHandler(UIBackgroundFetchResult.NoData) 
    } 
} 
+0

哪裏是我的功能呢? –