Sunday, June 26, 2022
HomeiOS Developmentios - Methods to name perform from different perform in swift?

ios – Methods to name perform from different perform in swift?


I would like that the locationManager perform (on the underside of the code) calls the updateLocation perform (within the WebView struct). I’ve tried a number of issues reminiscent of putting the category contained in the struct, making a callback variable, but it surely would not work. Does anybody is aware of the right way to repair this?

Thankyou very a lot

import Basis
import SwiftUI
import WebKit
import CoreLocation

struct WebView : UIViewRepresentable {

    let url: URL
    var webView = WKWebView()
    var supervisor = LocationManagerService()
   
    func makeUIView(context: Context) -> WKWebView  {
        let request = URLRequest(url: url)
        webView.load(request)
        return webView
    }
    
    // AND IT HAS TO CALL THIS (OR UPDATE THE WEBVIEW)
    func updateLocation(lat:String, lon:String){
        webView.evaluateJavaScript("deviceLocation(" + lat + ", " + lon + ");")
    }


    func updateUIView(_ uiView: WKWebView, context: Context) {
        uiView.navigationDelegate = context.coordinator
    }

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    //Conform to WKNavigationDelegate protocol right here and declare its delegate
    class Coordinator: NSObject, WKNavigationDelegate {
        var mother or father: WebView

        init(_ mother or father: WebView) {
            self.mother or father = mother or father
        }

        func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
            // Test if there's a token within the url
            let token = getQueryStringParameter(url: navigationAction.request.url!.absoluteString, param: "token")
            // Save token if obtainable
            let defaults = UserDefaults.normal
            if token != nil{
                defaults.set(token, forKey: "token")
            }
            
            // Test if a token is current in storage
            if let memoryToken = defaults.string(forKey: "token") {
                print("Reminiscence token: " + memoryToken)
                // Add token to new url
                let reloadUrl = insertQueryItems(url: navigationAction.request.url!.absoluteString, key: "token", worth: memoryToken)!
                print("Reload url: " + reloadUrl.absoluteString)
                // Test if the url id totally different from the present url
                if navigationAction.request.url?.absoluteString != reloadUrl.absoluteString{
                    print("Reload")
                    webView.load(URLRequest(url: reloadUrl))
                }
            } else {
                print("No token in reminiscence")
            }

            
            //Change padding on login web page
            if ((navigationAction.request.url?.absoluteString.incorporates("sso")) != false){
                // TODO: add padding to prime
//                self.mother or father.padding(<#T##SwiftUI.EdgeInsets#>)
            } else {
                // Take away the padding
            }
            decisionHandler(.enable)
        }
        
        func getQueryStringParameter(url: String, param: String) -> String? {
          guard let url = URLComponents(string: url) else { return nil }
          return url.queryItems?.first(the place: { $0.title == param })?.worth
        }
        
        func insertQueryItems(url: String, key: String, worth: String) -> URL?{
            var urlObject = URLComponents(string: url)
            var urlComponents = urlObject?.queryItems
            if(urlComponents == nil){
                urlComponents = []
            }
            urlComponents?.removeAll(the place: { $0.title == key })
            urlComponents?.append(URLQueryItem(title: key, worth: worth))
            urlObject?.queryItems = urlComponents
            return urlObject?.url
        
        }
    }
}

class LocationManagerService: NSObject, ObservableObject, CLLocationManagerDelegate {
    var supervisor: CLLocationManager = CLLocationManager()
    @Printed var location: CLLocation?
    @Printed var enabled: Bool = false
    
    override init() {
        tremendous.init()
        supervisor.delegate = self
        
        if CLLocationManager.locationServicesEnabled() {
            supervisor.requestWhenInUseAuthorization()
            supervisor.startUpdatingLocation()
        }
    }
    
    func locationManager(_ supervisor: CLLocationManager, didUpdateLocations areas: [CLLocation]) {
        print("location modified")
        location = areas.first
//            let user_lat = String(format: "%f", location?.coordinate.latitude)
//            let user_long = String(format: "%f", location?.coordinate.longitude)
//        THIS FUNCTION UPDATES
    }
    
    func locationManager(_ supervisor: CLLocationManager, didFailWithError error: Error) {
        print(error)
    }
    
    func locationManagerDidChangeAuthorization(_ supervisor: CLLocationManager) {
        enabled = CLLocationManager.locationServicesEnabled()
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments