Change WebView url from AppDelegate

泪湿孤枕 提交于 2021-01-19 06:17:12

问题


I'm receiving notifications from Firebase in the AppDelegate class.
This notification contains a String named "notif_url". I've put this value in a var named "desired_url" and now I need to change my WebView url with the "desired_url" value.

But I can't access to the webview to change it url like this :

@IBOutlet weak var my_web_view: UIWebView!

func load_url(server_url: String){
    let url = URL(string: server_url);
    let request = URLRequest(url: url!);
    my_web_view.loadRequest(request);
}

load_url(server_url: desired_url);

Do you know if I can do that and if yes, how ?

Images :

EDIT 1:
After adding breakPoint to know the wrong line, it seem't that the line is this one :

my_web_view.loadRequest(request)

EDIT 2:
If need, that's a part of my AppDelegate class code.

import UIKit
import UserNotifications

import Firebase

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    let gcmMessageIDKey = "gcm.message_id"
    @IBOutlet weak var my_web_view: UIWebView!

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

// [START ios_10_message_handling]
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate{

    // Receive displayed notifications for iOS 10 devices.
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void){
        print("Step : 12");
        let userInfo = notification.request.content.userInfo
        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey]{
            print("Message ID: \(messageID)")
        }

        // Print full message.
        print(userInfo)
        var url: String = userInfo[AnyHashable("url")] as! String;
        load_url(server_url: url);

        // Change this to your preferred presentation option
        completionHandler([])
    }

    func load_url(server_url: String){
        /*
        let url = URL(string: server_url);
        let request = URLRequest(url: url!);
        my_web_view.loadRequest(request);
         */

        guard let url = URL(string: server_url) else {
            print("Invalid URL")
            return
        }

        print("TRY : "+server_url);

        let request = URLRequest(url: url)
        my_web_view.loadRequest(request)
    }
}

EDIT 3:
If need, that's my ViewController class code.

import Foundation
import UIKit
import SafariServices
import UserNotifications

class ViewController: UIViewController, UIWebViewDelegate{

    @IBOutlet weak var my_web_view: UIWebView!
    @IBOutlet weak var my_loading_view: UIView!
    @IBOutlet weak var spinner : UIActivityIndicatorView!
    @IBOutlet weak var app_logo : UIImageView!

    @IBOutlet weak var deadlinePicker: UIDatePicker!
    @IBOutlet weak var titleField: UITextField!

    var new_url: String = "";

    override func viewDidLoad(){
        super.viewDidLoad()

        let server_url = "https://www.sortirauhavre.com/";

        NotificationCenter.default.addObserver(self, selector: #selector(self.rotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
        rotated();

        spinner.startAnimating();


        my_web_view.scrollView.bounces = false;
        my_web_view.scrollView.isScrollEnabled = true;

        let url = URL(string: server_url);
        let request = URLRequest(url: url!);
        my_web_view.loadRequest(request);
    }

    // CETTE FONCITON SE LANCE A LA ROTATION DE L'APPAREIL
    func rotated(){
        app_logo.center = my_loading_view.center;
        let y = app_logo.frame.origin.y;
        let h = app_logo.frame.size.height
        app_logo.frame.origin.y = y-(h/2);

        spinner.center = my_loading_view.center;
    }

    // CETTE FONCTION MET EN ARRIERE PLAN L'ANNIMATION DE CHARGEMENT
    func removeLoader(){
        self.view.addSubview(my_web_view);
    }

    // CETTE FONCTION MET EN PREMIER PLAN L'ANNIMATION DE CHARGEMENT
    func addLoader(){
        self.view.addSubview(my_loading_view);
    }

    // CETTE FONCTION SE DECLANCHE QUAND LES PAGES DE LA WEBVIEW COMMENCE A CHANGER
    func webViewDidStartLoad(_ webView: UIWebView){
        addLoader();
        let server_url = "https://www.sortirauhavre.com/";
        _ = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.removeLoader), userInfo: nil, repeats: false);
        if let text = webView.request?.url?.absoluteString{
            if text.hasPrefix(server_url){
            }
            else if text != ""{
                UIApplication.shared.openURL(URL(string: text)!)
                my_web_view.goBack()
            }
        }
    }

    // CETTE FONCTION SE DECLANCHE QUAND LES PAGES DE LA WEBVIEW FINI DE CHANGER
    func webViewDidFinishLoad(_ webView: UIWebView){
        let server_url = "https://www.sortirauhavre.com/";
        _ = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.removeLoader), userInfo: nil, repeats: false);
        if let text = webView.request?.url?.absoluteString{
            if text.hasPrefix(server_url){
            }
            else if text != ""{
                UIApplication.shared.openURL(URL(string: text)!)
                my_web_view.goBack()
            }
        }
    }
}

回答1:


You are force unwrapping url which is not a valid URL. I would suggest adding a guard statement to prevent the crash if a invalid URL is created:

func load_url(server_url: String) {
    guard let url = URL(string: server_url) else {
        print("Invalid URL")
        return
    }

    let request = URLRequest(url: url)
    my_web_view.loadRequest(request)
}

As you are obtaining the URL in the AppDelegate you cannot simply update the UIWebView from this class. You will need to call a function in the my_web_view's parent class which updates the URL.

// App Delegate

var serverURL: String?

func load_url(server_url: String) {
    serverURL = server_url
    let notificationName = Notification.Name("updateWebView")
    NotificationCenter.default.post(name: notificationName, object: nil)
}


// View Controller

override func viewDidLoad() {
    let notificationName = Notification.Name("updateWebView")
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.updateWebView), name: notificationName, object: nil)

    updateWebView()
}

func updateWebView() {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let serverURL = appDelegate.serverURL

    guard let url = URL(string: serverURL) else {
        print("Invalid URL")
        return
    }

    let request = URLRequest(url: URL)
    my_web_view.loadRequest(request)
}



回答2:


Instead of creating a new instance of your view controller, or trying to duplicate the outlet, you just need to access the current instance of your view controller. You can use either:

  1. A global value for the view controller, or
  2. A singleton-like pattern.

Then you can access the instance from your app delegate, by calling either myGlobalViewController.webView or ViewController.instance.webView.

So, here's an example:

import UIKit

private var thisViewController: ViewController? // Will hold the instance.

class ViewController: UIViewController {

    static var instance: ViewController {
        guard let thisViewController = thisViewController else { fatalError() } // Don't do this unless you're 100% sure that you'll never access this before the instance is loaded.
        return thisViewController
    }

    @IBOutlet weak var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        thisViewController = self // Set the property to self.
    }
...
}

After this, you can access the web view from your app delegate:

func load_url(server_url: String){
    guard let url = URL(string: server_url) else {
        return
    }

    let request = URLRequest(url: url)
    ViewController.instance.webView.loadRequest(request)
}


来源:https://stackoverflow.com/questions/43408531/change-webview-url-from-appdelegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!