Interface iOS app with web address

I have an app that needs to send to a URL and receive in return a JSON encoded object. I need to do this from within the app without the user knowing about it so as not to bother the user. I can’t use Safari. The purpose is to look up the carrier of a phone number. That information is needed by the app.

How do I do this?

You will need a web service or application provider that will take a phone number and return the carrier information; you can’t just google it and parse the response. Well you could, but I think it’d be very unreliable!

Check out the Core Telephony API. I think it has the capabilities you are looking for.

I have the following code:

import Foundation
typealias CompletionHandler = () -> Void
class MySessionDelegate : NSObject, URLSessionDelegate, URLSessionTaskDelegate, URLSessionDataDelegate, URLSessionStreamDelegate {

var completionHandlers: [String: CompletionHandler] = [:]  

func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {  

    print("data", data.description)  

    data.forEach {  
  
        word in  
  
        print(word)  
  
    }  

    let string = String(data: data, encoding: .utf8)  

    print("DATA:\n\(string)\nEND DATA\n")  

}  

}

    let defaultConfiguration = URLSessionConfiguration.default  
    let ephemeralConfiguration = URLSessionConfiguration.ephemeral  
    let backgroundConfiguration = URLSessionConfiguration.background(withIdentifier: "com.myapp.networking.background")  
  
   
    let cachesDirectoryURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!  
    let cacheURL = cachesDirectoryURL.appendingPathComponent("MyCache")  
    var diskPath = cacheURL.path  
  
    let cache = URLCache(memoryCapacity:16384, diskCapacity: 268435456, diskPath: diskPath)  
    defaultConfiguration.urlCache = cache  
    defaultConfiguration.requestCachePolicy = .useProtocolCachePolicy  
   
    let delegate = MySessionDelegate()  
    let operationQueue = OperationQueue.main  
  
    let defaultSession = URLSession(configuration: defaultConfiguration, delegate: delegate, delegateQueue: operationQueue)  
    let ephemeralSession = URLSession(configuration: ephemeralConfiguration, delegate: delegate, delegateQueue: operationQueue)  
    let backgroundSession = URLSession(configuration: backgroundConfiguration, delegate: delegate, delegateQueue: operationQueue)  
  
    let urlString = "https://www.example.com/"  
  
    if let url = URL(string: urlString) {  
        let dataTask = defaultSession.dataTask(with: url)  
        dataTask.resume()  
    }  

It works fine when urlString equals “https://www.example.com/”, but when I change urlString to “http://www.carrierlookup.com/index.php/api/balance?key={145fdf639c68b4e48f53957e5f3830450fc40565}” it doesn’t work. Both urls work when I put them into a web browser, although for the second url it returns a message saying that the api key could not be found. What doe I need to do to get my code to work. The latter url returns a JSON encoded object.

Hi @brower

I would suggest you to encode your url string before using it. Try something like this:

let urlString = "https://www.example.com/".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)

Hope this helps.

Nikita

Sorry - misread your question. I thought you were looking for the carrier of your phone, not another number.

Ignore my response :slight_smile:

This topic was automatically closed after 166 days. New replies are no longer allowed.