iosswifturlencodeurlparse

Get the value of URL Parameters for bid i'm getting nil value


let strUrl = bank://bpay?link=https://bpayuat.allsocialassets.com/pay?bid=OU1200000NATGF&cpm=Policy%20Number:217307XX|DOB%20(DD-MMM-YYYY):07-MAR-1985&cnm=BBPS&bnm=10142&bpr=JUNE&bmt=360&bdt=2021%2D06%2D11&&bai=Policy%20Status:active|Product%20Name:ICICI%20Pru%20Heart%20Cancer%20Protect|Premium%20Amount:20&enc_Policy%20Number=4evSBNbzt7iU9xQq6J2nnaCsfb1983yu5EZn9XtMvWxtZG9YMSC4CHa1Rb4CrqZ7cAyjZDKP2boqUh1nCSqygDFN&cid=202289766745
        
         let url = strUrl!.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
         let queryItems = URLComponents(string: url!)?.queryItems
         let bid = queryItems?.filter({$0.name == "bid"}).first
         let bnm = queryItems?.filter({$0.name == "bnm"}).first
         let cpm =  queryItems?.filter({$0.name == "cpm"}).first
         let bmt = queryItems?.filter({$0.name == "bmt"}).first
         let cid = queryItems?.filter({$0.name == "cid"}).first
         let enc = queryItems?.filter({$0.name == "enc_Policy%20Number"}).first
         let bdt = queryItems?.filter({$0.name == "bdt"}).first
         let bpr = queryItems?.filter({$0.name == "bpr"}).first
         let cnm = queryItems?.filter({$0.name == "cnm"}).first
         let bai = queryItems?.filter({$0.name == "bai"}).first

Solution

  • Your URL is:

    bank://bpay?link=https://bpayuat.allsocialassets.com/pay?bid=OU1200000NATGF&cpm=Policy%20Number:217307XX|DOB%20(DD-MMM-YYYY):07-MAR-1985&cnm=BBPS&bnm=10142&bpr=JUNE&bmt=360&bdt=2021%2D06%2D11&&bai=Policy%20Status:active|Product%20Name:ICICI%20Pru%20Heart%20Cancer%20Protect|Premium%20Amount:20&enc_Policy%20Number=4evSBNbzt7iU9xQq6J2nnaCsfb1983yu5EZn9XtMvWxtZG9YMSC4CHa1Rb4CrqZ7cAyjZDKP2boqUh1nCSqygDFN&cid=202289766745
    

    You could analyze the query as such:

    bank://bpay
               ?link=https://bpayuat.allsocialassets.com/pay
                                                            ?bid=OU1200000NATGF
               &cpm=Policy%20Number:217307XX|DOB%20(DD-MMM-YYYY):07-MAR-1985
               &cnm=BBPS
               &bnm=10142
               &bpr=JUNE
               &bmt=360
               &bdt=2021%2D06%2D11&
               &bai=Policy%20Status:active|Product%20Name:ICICI%20Pru%20Heart%20Cancer%20Protect|Premium%20Amount:20
               &enc_Policy%20Number=4evSBNbzt7iU9xQq6J2nnaCsfb1983yu5EZn9XtMvWxtZG9YMSC4CHa1Rb4CrqZ7cAyjZDKP2boqUh1nCSqygDFN
               &cid=202289766745
    

    If it was &bid instead of ?bid, then the bid would have been another query item from bpay, but since there isn't, it's a query item from bpayuat.allsocialassets.com/pay.

    You need to do the same, looking for URLQueryItem inside the URL parameter.

    This can also be seen with checking what's you parsed:

    for aQueryItem in queryItems ?? [] {
        print("\(aQueryItem.name) - \(aQueryItem.value)")
    }
    

    Output:

    link - Optional("https://bpayuat.allsocialassets.com/pay?bid=OU1200000NATGF")
    cpm - Optional("Policy%20Number:217307XX|DOB%20(DD-MMM-YYYY):07-MAR-1985")
    cnm - Optional("BBPS")
    bnm - Optional("10142")
    bpr - Optional("JUNE")
    bmt - Optional("360")
    bdt - Optional("2021%2D06%2D11")
     - nil
    bai - Optional("Policy%20Status:active|Product%20Name:ICICI%20Pru%20Heart%20Cancer%20Protect|Premium%20Amount:20")
    enc_Policy%20Number - Optional("4evSBNbzt7iU9xQq6J2nnaCsfb1983yu5EZn9XtMvWxtZG9YMSC4CHa1Rb4CrqZ7cAyjZDKP2boqUh1nCSqygDFN")
    cid - Optional("202289766745")
    

    And if you change your URL to (the &bid instead of ?bid):

    bank://bpay?link=https://bpayuat.allsocialassets.com/pay&bid=OU1200000NATGF&cpm=Policy%20Number:217307XX|DOB%20(DD-MMM-YYYY):07-MAR-1985&cnm=BBPS&bnm=10142&bpr=JUNE&bmt=360&bdt=2021%2D06%2D11&&bai=Policy%20Status:active|Product%20Name:ICICI%20Pru%20Heart%20Cancer%20Protect|Premium%20Amount:20&enc_Policy%20Number=4evSBNbzt7iU9xQq6J2nnaCsfb1983yu5EZn9XtMvWxtZG9YMSC4CHa1Rb4CrqZ7cAyjZDKP2boqUh1nCSqygDFN&cid=202289766745

    You'll get:

    link - Optional("https://bpayuat.allsocialassets.com/pay")
    bid - Optional("OU1200000NATGF")
    cpm - Optional("Policy%20Number:217307XX|DOB%20(DD-MMM-YYYY):07-MAR-1985")
    cnm - Optional("BBPS")
    bnm - Optional("10142")
    bpr - Optional("JUNE")
    bmt - Optional("360")
    bdt - Optional("2021%2D06%2D11")
     - nil
    bai - Optional("Policy%20Status:active|Product%20Name:ICICI%20Pru%20Heart%20Cancer%20Protect|Premium%20Amount:20")
    enc_Policy%20Number - Optional("4evSBNbzt7iU9xQq6J2nnaCsfb1983yu5EZn9XtMvWxtZG9YMSC4CHa1Rb4CrqZ7cAyjZDKP2boqUh1nCSqygDFN")
    cid - Optional("202289766745")
    

    Keeping your logic:

    if let link = queryItems?.filter({$0.name == "link"}).first, let linkValue = link.value {
        let subQueryItems = URLComponents(string: linkValue)?.queryItems
        for aSubQueryItem in subQueryItems ?? [] {
            print("\(aSubQueryItem.name) - \(aSubQueryItem.value)")
        }
        let linksub = subQueryItems?.first(where: { $0.name == "bid" })
        print(linksub)
    }
    

    Unrelated but:

    filter({$0.name == "something"}).first should be first(where: { $0.name == "something" }). It's better optimized.