5 Replies
      Latest reply on Oct 15, 2019 2:34 AM by eskimo
      icefire_wang Level 1 Level 1 (0 points)

        Is there any way to get  CFNetwork version number?

        • Re: get CFNetwork Version number
          eskimo Apple Staff Apple Staff (12,015 points)

          What do you need that value for?

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"

            • Re: get CFNetwork Version number
              icefire_wang Level 1 Level 1 (0 points)

              em... it is hard to explatin.

              Put simply, we want do make a User-Agent content exactly same with iOS system.

              If we can get CFNetwork version ,we can combine it.

              If not, we can only send a request and get it from the server and send it back to APP.

                • Re: get CFNetwork Version number
                  eskimo Apple Staff Apple Staff (12,015 points)

                  we want do make a User-Agent content exactly same with iOS system. If we can get CFNetwork version, we can combine it.

                  The problem with this approach is that the default User-Agent string has a complex structure, where the CFNetwork version is just one component of that structure, and there’s no guarantee that this structure will be the same from release to release of the system.  So once you get the CFNetwork version string, there’s no supported way to build the default User-Agent string from that.

                  Indeed, while digging into this today I noticed that iOS and macOS have different default user agent strings!

                  Regardless of what else you do here, my recommendation is that you file an enhancement request for an API to get this string, making sure to include a detailed justification as to why you need it.  Please post your bug number, just for the record.

                  If not, we can only send a request and get it from the server and send it back to APP.

                  Honestly, this seems like a better approach to me.


                  Notwithstanding the above, you can get the CFNetwork version from its bundle with the code shown below:

                  func cfNetworkVersion() -> String? {
                      guard
                          let bundle = Bundle(identifier: "com.apple.CFNetwork"),
                          let versionAny = bundle.infoDictionary?[kCFBundleVersionKey as String],
                          let version = versionAny as? String
                      else { return nil }
                      return version
                  }

                  Make sure to handle a nil result here; there’s a bunch of ways that this code could break in the future.

                  Share and Enjoy

                  Quinn “The Eskimo!”
                  Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                  let myEmail = "eskimo" + "1" + "@apple.com"

                    • Re: get CFNetwork Version number
                      icefire_wang Level 1 Level 1 (0 points)

                      Thank you very much for your help. For now, we know NSURLSession send a request with empty User-Agent, will auto add a defult User-agent like :

                      (app name)/1.0.4.14 CFNetwork/758.3.15 Darwin/15.6.0

                      and we already know Darwin version can get from API. and if the app name contains no ASIC code, it will be URI encode. So If we can get CFNetwork version, I think we can create same User-Agent as iOS system in most situations.

                       

                      And why we want's to create User-agent:

                      We are using NSURLProtocol to get user request and make it to cross our proxy server, and we don't want to change anything of user's request, but when user's request come to protocol we can't get the user-agent in the reqeust header, so i think we need to create it by ourself.

                      If not, we can only send a request and get it from the server and send it back to APP.

                      Honestly, this seems like a better approach to me.

                      This way is a little complex to us, and it also need more time than just create the User-Agent, since our proxy service need start the faster the better。

                       

                       

                      Regardless of what else you do here, my recommendation is that you file an enhancement request for an API to get this string, making sure to include a detailed justification as to why you need it. Please post your bug number, just for the record.

                       

                      Feedback num: FB7380014

                        • Re: get CFNetwork Version number
                          eskimo Apple Staff Apple Staff (12,015 points)

                          I think we can create same User-Agent as iOS system in most situations.

                          Until your code moves to a different platform, like the Mac.  Or Apple changes how it calculates the default user agent string, which we’ve done many times in the past and it wouldn’t surprise me if we did in the future.  Doing this is inherently brittle.

                          Then again…

                          We are using NSURLProtocol

                          This is also massively brittle.  Are you doing this in order to intercept the requests issued by a UIWebView?

                          Share and Enjoy

                          Quinn “The Eskimo!”
                          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                          let myEmail = "eskimo" + "1" + "@apple.com"