/PKHUrlManager

URL Parser

Primary LanguageSwift

PKHUrlManager

๐ŸŒ Redirecting Web urls to Native functions

์›น url์—์„œ json์œผ๋กœ ๋ฏธ๋ฆฌ ์ง€์ •ํ•ด๋†“์€ ๊ฐ’์ด ๋“ค์–ด์˜ฌ ๊ฒฝ์šฐ ํŠน์ • ๋„ค์ดํ‹ฐ๋ธŒ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•ด์ฃผ๋Š” UrlManager ๐Ÿ‘ฃ

blogimg

JSON Sample

Define url custom action lists like this.

funNames

  • ClassName + FunctionName
  • ex. WebViewController.urlFuncGoogle
{
    "webActionUrlList": [
        {
            "description":"",
            "title":"google",
            "name": "google",
            "type": "CustomUrlAction",
            "checkUrls": [
                "https://www.google.co.kr/"
            ],
            "funNames": [
                "WebViewController.urlFuncGoogle"
            ]
        },
        {
            "description":"",
            "title":"naver",
            "name": "naver",
            "type": "naver",
            "checkUrls": [
                "https://m.naver.com/"
            ],
            "funNames": [
                "WebViewController.urlFuncNaver"
            ]
        }
    ]
}

Core Functions

func runFunction(url: String, instanc: NSObject? = nil, data: [String: Any]? = nil) -> Bool {
        if type == .webAction && webFunctionRun == true {
            return true
        }
        
        webFunctionRun = true
        defer {
            webFunctionRun = false
        }
        guard let funNames = self.funNames else { return true }
        
        
        
        
        
        var toData: [String: Any?]!
        if data == nil {
            toData = [URLParserCallNames: [self.name], "data": self.data]
        }
        else {
            toData = data
            toData.updateValue(self.data, forKey: "data")
            if var names = toData[URLParserCallNames] as? [String] {
                names.append(self.name)
                toData.updateValue(names, forKey: URLParserCallNames)
            }
            else {
                toData.updateValue([self.name], forKey: URLParserCallNames)
            }
        }
        
        
        for (classType, function) in funNames {
            
            if let aInstanc = instanc , aInstanc.responds(to: function) {
                if let _ = aInstanc.perform(function, with: url, with: toData) {
                    returnValue = true
                }
                else {
                    returnValue = false
                }
                #if DEBUG
                print("\t ๐ŸŒˆ RunFunction \(aInstanc.className).\(function), data: \(toData), return: \(returnValue)\n")
                #else
                #endif
                
            }
            else {
                guard let nsobjAbleType = classType as? NSObject.Type else {
                    preconditionFailure(" >>>>> Class Not NSObject")
                    continue
                }
                let obj = nsobjAbleType.init()
                guard obj.responds(to: function) else {
                    preconditionFailure(" >>>>> \(classType) Not function")
                    continue
                }
                
                if let _ = obj.perform(function, with: url, with: toData) {
                    returnValue = true
                }
                else {
                    returnValue = false
                }
                #if DEBUG
                print("\t ๐ŸŒˆ RunFunction \(nsobjAbleType.className)).\(function), data: \(toData), return: \(returnValue)\n")
                #else
                #endif
                
            }
            
        }
        
        return returnValue
    }