获取 URL 出价参数的值 我得到的是零值

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({[=11=].name == "bid"}).first
         let bnm = queryItems?.filter({[=11=].name == "bnm"}).first
         let cpm =  queryItems?.filter({[=11=].name == "cpm"}).first
         let bmt = queryItems?.filter({[=11=].name == "bmt"}).first
         let cid = queryItems?.filter({[=11=].name == "cid"}).first
         let enc = queryItems?.filter({[=11=].name == "enc_Policy%20Number"}).first
         let bdt = queryItems?.filter({[=11=].name == "bdt"}).first
         let bpr = queryItems?.filter({[=11=].name == "bpr"}).first
         let cnm = queryItems?.filter({[=11=].name == "cnm"}).first
         let bai = queryItems?.filter({[=11=].name == "bai"}).first

您的 URL 是:

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

您可以这样分析查询:

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

如果它是 &bid 而不是 ?bid,那么 bid 将是来自 bpay 的另一个查询项,但由于没有,它是来自 bpayuat.allsocialassets.com/pay.

的查询项目

你需要做同样的事情,在 URL 参数中寻找 URLQueryItem

这也可以通过检查您解析的内容来查看:

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

输出:

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")

如果您将 URL 更改为(&bid 而不是 ?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

您将获得:

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")

保持你的逻辑:

if let link = queryItems?.filter({[=15=].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: { [=15=].name == "bid" })
    print(linksub)
}

无关但是:

filter({[=24=].name == "something"}).first 应该是 first(where: { [=25=].name == "something" })。优化的比较好。