The first two days of processing Iosapp process (I was written in swift language, less information), was a "string" spent the night to the second genius to deal with, here, see this article of the students one day encounter the situation can be three minutes to fix not wasting time:
First look at the following code
Func GetBalance () {
Let URL = Nsurl (string: "http://www.weather.com.cn/data/sk/101110101.html")
Let data = NSData (contentsofurl:url!)
if data! = Nil {
Let Dict:nsdictionary = (try! Nsjsonserialization.jsonobjectwithdata (data!, Options:. allowfragments)) as! Nsdictionary
Let Weatherinfo = Dict.objectforkey ("Weatherinfo") as! Nsdictionary
Print ("Get Weather Data")
var city = Weatherinfo.objectforkey ("City") as! String
Print ("Get City Name")
... ...
}
}
is not feel nothing, direct access on it, yes, but this is just the most basic URL address, if our address is like this?
http://183.252.21.19:81/bhys/d/api/?apitype=check&rqsjson={"Cardno": "999000002111", "pwd": "000211", "Name": " Minsheng Card "}&sign=123456789
Will find that we have double quotation marks in the string, parameters have special characters such as Chinese, this time, then directly access what will happen? No, there is no access, because obviously we need to let URL = Nsurl (string: "/http/...") in the string to escape, with our slash/"but after the turn is still unable to pass, Xcode will error: URL is nil
Because I am also the first contact with app development, no OC experience, do not know whether iOS is not recognized/escaped or Swift does not recognize, after all, the new language.
The solution to the problem is another "escape"--coding, the URL is encoded encode, the above URL will be double quotation marks and curly braces are encode encoded as follows:
http://183.252.21.19:81/bhys/d/api/?apiType=check&rqsJson=%7B%22cardNo%22:%22999000002111%22,%22pwd%22:% 22pwd%22,%22name%22:%22 Minsheng Card 11%22%7d&sign=123456789
This time not only can be accessed, you can also add parameters, as follows:
Http://183.252.21.19:81/bhys/d/api/?apiType=check&rqsJson=%7B%22cardNo%22:%22 "+idnumber+"%22,%22pwd%22:%22 "+pwdnumber+"%22,%22name%22:%22 "+name+"%22%7d&sign=123456789
Remember:
One, there is code decoding, but decoding that is the service side of the matter, mobile developers need to know this matter.
Second, the above is just to provide a way of thinking, so that the students who encounter the URL problem know that there is encode coding this matter, the specific transcoding also depends on the service side, some service side only provide a part of the transcoding, such as above, only turn
{"Cardno": "999000002111", "pwd": "000211", "name": "Minsheng Card 11"}, and Http://183.252.21.19:81/bhys/d/api/?apiType=check &rqsjson= no transcoding, this is possible, but also need to be flexible, not copied.
URL handling for iOS