2017-07-05 52 views
1

在xcode 8.3中使用swift3,這是一個webview應用程序。如何在前臺收到通知時彈出消息框?

下面是我在ViewController.swift函數來處理URL從推送通知收到

func redirectTo(url: String) { 
    let request = URLRequest(url: URL(string: url)!) 
    MyApp.loadRequest(request) 
} 

下面是我在AppDelegate.swift功能當我的應用程序在後臺運行處理didReceiveRemoteNotification

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 
    if let pushUrl = userInfo[AnyHashable("url")] as? String { 
     viewController?.redirectTo(url: pushUrl) 
    } 
} 

,當我點擊它時,它會將我的應用程序重定向到從通知中收到的網址。

然而,當在前臺運行我的應用程序,它會直接重定向我的WebView不撒手網址。

我知道這是因爲我每次收到通知時,它會調用redirectTo功能的loadRequest然後。

我的問題是,我如何添加一個消息框,有兩個按鈕,我的應用程序運行時,在forefround和收到通知它會要求用戶重定向或取消?

+0

只需在兩個選項中添加一個警報提示,並有兩個選項ok並取消,在ok點擊時重定向到url代碼。延遲後顯示警報。希望它可以幫助你 – Chandan

+0

@Chandan我完全是一個新的迅速。我會嘗試一下。真的很感激。 – Dreams

回答

0

您可以創建警報以獲取用戶的確認。像這樣的東西

let alert = UIAlertController(title: "YOUR_TITLE", message: "YOUR_MESSAGE", preferredStyle: .alert) 
    let okAction = UIAlertAction(title: "Ok", style: .default) { _ in 
     // Handle your ok action 
    } 
    alert.addAction(okAction) 
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { _ in 
     // Handle your cancel action 
    } 
    alert.addAction(cancelAction) 

    DispatchQueue.main.async { 
     self.present(alert, animated: true, completion: nil) 
    } 

由於URLRequest可能很貴,您可能想要在後臺實現它。

DispatchQueue.global().async { 
    let request = URLRequest(url: URL(string: url)!) 
    MyApp.loadRequest(request) 
} 
相關問題