0

I am building iOS app where I insert an input string and search this string using Google , then I get the data string back and use it to search for specific word or series of characters

I use URLSessionDataTask to do this


let baseURL = "https://www.google.com/search?q=~+%22education%22%20-intitle:%22profiles%22%20-inurl:%22dir/+%22+site:ca.linkedin.com/in/+OR+site:ca.linkedin.com/pub/" 

if let url = URL(string: baseURL) {
            let session = URLSession(configuration: .default)
            let task = session.dataTask(with: url) { data, response, error in
                if error != nil {
                    print(error)
                    return
                }
                if let safeData = data {
                    let dataString = String(data: safeData, encoding: .ascii)
                    
                    let decodedString = dataString?.utf8
                    
                    print(dataString)
                }
                
            }
            task.resume()
        }

the problem is that the dataString I got back is very strange and I cant deal with it , so how can I get it as normal string so every thing is clear enough ???

if let url = URL(string: baseURL) {
            let session = URLSession(configuration: .default)
            let task = session.dataTask(with: url) { data, response, error in
                if error != nil {
                    print(error)
                    return
                }
                if let safeData = data {
                    let dataString = String(data: safeData, encoding: .ascii)
                    
                    let decodedString = dataString?.utf8
                    
                    print(dataString)
                }
                
            }
            task.resume()
        }
4
  • 1
    .utf8 instead of .ascii perhaps Commented Jul 11, 2023 at 15:53
  • 1
    Joakim's guess of using .utf8 seems like a good bet. You will need to edit your question to tell us what you are expecting and what you're getting if you want more specific help. Commented Jul 11, 2023 at 16:50
  • I've tried .utf8 but got the same problem , I want the text shown on google page to be clear so I can extract the text I want inside the app Commented Jul 12, 2023 at 11:49
  • "the problem is that the dataString I got back is very strange and I cant deal with it " You get a HTML page... It starts with "<!DOCTYPE html><html lang=", what did you expect? Commented Jul 17, 2023 at 9:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.