IOS URL Processing and iOSURL Processing

Source: Internet
Author: User

IOS URL Processing and iOSURL Processing

During the process of processing iOSapp two days ago (I wrote it in swift language with less information), I was processed by a "string" for one night until the next day. I will write down the following, it is expected that students who have read this article will be able to handle the problem in three minutes one day without 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)! NSDictionary

Let weatherInfo = dict. objectForKey ("weatherinfo")! NSDictionary

Print ("get weather data ")

Var city = weatherInfo. objectForKey ("city")! String

Print ("Get the city name ")

......

}

}

Do you think there is nothing, just access it directly? Yes, but this is just the most basic URL address. What 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 11"} & sign = 123456789

We will find that our strings contain double quotation marks and parameters have special characters such as Chinese. What will happen when we access them directly? No, it cannot be accessed at all, because it is clear that we need to go to let url = NSURL (string: "http ://...... ") escape the string using our slash/", but it still fails after conversion. Xcode will report the error: url is nil

 

This is because I was the first to get started with app development and have no OC experience. I don't know whether iOS is not recognized/escaped or swift is not recognized. After all, the new language is used.

 

The solution to the problem is another "escape"-encoding. encode the URL with an encode. The above URL encodes double quotation marks and braces as follows:

Http: // 183.252.21.19: 81/bhys/d/api /? ApiType = check & rqsJson = % 7B % 22 cardNo % 22: % 22999000002111% 22, % 22pwd % 22: % 22pwd % 22, % 22 name % 22: % 22 livelihood card 11% 22% 7D & sign = 123456789

In this case, you can not only access it, but also add parameters as follows:

Http: // 183.252.21.19: 81/bhys/d/api /? ApiType = check & rqsJson = % 7B % 22 cardNo % 22: % 22 "+ IDNumber +" % 22, % 22pwd % 22: % 22 "+ pwdNumber +" % 22, % 22 name % 22: % 22 "+ Name +" % 22% 7D & sign = 123456789

 

Remember:

1. decoding is possible if there is encoding, but decoding is a matter on the server. mobile developers need to know about this.

II. The above only provides an idea to let the users who encounter URL problems know about encode encoding. The specific transcoding needs to be done on the server. Some servers only provide part of transcoding, as shown in the above section, transfer only

{"CardNo": "999000002111", "pwd": "000211", "name": "Minsheng card 11"}, http: // 183.252.21.19: 81/bhys/d/api /? ApiType = check & rqsJson = No transcoding. This is all possible. It must be changed and cannot be copied.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.