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

Time:05-13

let strUrl = bank://bpay?link=https://bpayuat.allsocialassets.com/pay?bid=OU1200000NATGF&cpm=Policy Number:217307XX|DOB (DD-MMM-YYYY):07-MAR-1985&cnm=BBPS&bnm=10142&bpr=JUNE&bmt=360&bdt=2021-06-11&&bai=Policy Status:active|Product Name:ICICI Pru Heart Cancer Protect|Premium Amount:20&enc_Policy Number=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 Number"}).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

CodePudding user response:

Your URL is:

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

You could analyze the query as such:

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

You'll get:

link - Optional("https://bpayuat.allsocialassets.com/pay")
bid - Optional("OU1200000NATGF")
cpm - Optional("Policy Number:217307XX|DOB (DD-MMM-YYYY):07-MAR-1985")
cnm - Optional("BBPS")
bnm - Optional("10142")
bpr - Optional("JUNE")
bmt - Optional("360")
bdt - Optional("2021-06-11")
 - nil
bai - Optional("Policy Status:active|Product Name:ICICI Pru Heart Cancer Protect|Premium Amount:20")
enc_Policy Number - 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.

  • Related