0

i need for the pushwoosh sdk still AppDelegate... in the appdelegate - how can i access the webview and change the url (depending on push notifications received from appdelegate? pushnotifications and everything works... also the initial webpage is loaded on startup. but how can i change url within the app delegate?

MAIN APP:

 @main
struct XYApp: App {
    @UIApplicationDelegateAdaptor var delegate: FSAppDelegate
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

class FSAppDelegate: UIResponder, UIApplicationDelegate, PWMessagingDelegate {
    var window: UIWindow?
   

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        //initialization code
        //set custom delegate for push handling, in our case AppDelegate
        Pushwoosh.sharedInstance()?.delegate = self;
        
        //register for push notifications
        Pushwoosh.sharedInstance()?.registerForPushNotifications()
        
        print("token: ",Pushwoosh.sharedInstance()?.getPushToken())
      
            return true
    }
    }

contenview swift

  import SwiftUI

struct ContentView: View {
    @State private var showWebView = false
   
    var body: some View {
        WebView(url: URL(string: "https://www.xy.com/mobile")!)
        
          
    }
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

webview swift

import SwiftUI
import WebKit
 
struct WebView: UIViewRepresentable {
    var url: URL
    

    func makeUIView(context: Context) -> WKWebView {
        return WKWebView()
    }
 
    func updateUIView(_ webView: WKWebView, context: Context) {
        let request = URLRequest(url: url)
        webView.load(request)
    }
    }
zabby
  • 11
  • 2

1 Answers1

0

The easy way is to use a static var.

For example, create a variable on the FSAppDelegate(note this can be created on any file or class you want):

static var webView: WebView?

__

on makeUIView function, you can add a new line:

func makeUIView(context: Context) -> WKWebView {
   let webView = WKWebView()
   FSAppDelegate.webView = webView
   return webView
}

__

Then, on receive notification(didFinishLaunchingWithOptions will only be called if the app isn't running):

if let webView = FSAppDelegate.webView {
   webView.load(someRequest)
}

You could implement the didReceivePushNotification method instead.

But if you want to load the request on application open (touching the notification alert for example), maybe you could do something different.

application: UIApplication, didFinishLaunchingWithOptions method runs before any other method in the code. So webView will not be set yet. So you can store the URL String for example. So the static var will be a String:

static var urlString: String?

On your app launching, if you have the URL String, you can store like:

FSAppDelegate.urlString = someURLString

Then on the updateUIView method, make:

func updateUIView(_ webView: WKWebView, context: Context) {
    if let urlString = FSAppDelegate.urlString {
       let receivedURL = URL(string: urlString)
       let request = URLRequest(url: receivedURL)
       webView.load(request)
    } else {
       let request = URLRequest(url: url)
       webView.load(request)
    }
}

Note that im saying this is possible, but you could have a better software architecture if you explore other ways.

Sore
  • 176
  • 4
  • thx mate, try it just after my vacation ;) – zabby Feb 07 '22 at 15:32
  • i´ll do it completely new, as my old running ios app is 5 years old ;) and UIWebView is deprecated. so i want to do it completely new - already did in android, but i am a ios noob... what would be a better way? – zabby Feb 07 '22 at 15:33