我能夠將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
。
對不起,我無法將其轉換成Swift。我在C中沒有背景,我開始使用Swift學習ios開發。 –
只是使用該方法,它會給你自動的建議,不用擔心太多。它會解決你的問題。 – Santo
謝謝!我做的!這很簡單! –