Zhan Zhan - 13 days ago 8
Swift Question

How to display a specific ViewController in OneSignal Swift

I do app with

OneSignal
and
Swift 3
. I got push. How to display a specific
ViewController
with
WebView
in
OneSignal
when notification is clicked. In push in additional data with field
"link"
I got link, but can't display this link in my
WebView
.

I try use global variable
tempURL
to put url from
additional data
. But it not work.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

OneSignal.initWithLaunchOptions(launchOptions, appId: "MYID", handleNotificationAction: { (result) in
let payload = result?.notification.payload
print("This is Payload \(payload)")
var fullMessage = payload?.title
let messageTitle = "OneSignal Example"

if (result?.action.actionID) != nil {
let additionalData = payload?.additionalData
let url = additionalData?["link"] as! String?
tempURL = url!
fullMessage = fullMessage! + "\nPressed ButtonId:\(url)"

}

let alertController = UIAlertController(title: messageTitle, message: fullMessage, preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default)
alertController.addAction(okAction)
alertController.show(alertController, sender: nil)

})
return true
}


Try to show:

func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {

let aViewController = ViewController()
aViewController.loadAddressURL(url: tempURL)
UIApplication.shared.keyWindow?.rootViewController?.present(aViewController, animated: true, completion:nil)

}


I have error:


fatal error: unexpectedly found nil while unwrapping an Optional value

Answer

first of all you can't pass data in your way. You should instantiate ViewController with StoryboardID. I explained how to use it with this link.

If you are pretty sure, your additionalData is not nil, you can pass your data with StoryboardID.

Comments