Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 186023
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 10, 20222022-06-10T14:39:25+00:00 2022-06-10T14:39:25+00:00

Why is this URLSession.datatask not working in Swift 5 for macos

  • 0

[ad_1]

I am trying to make my own DynamicIP updater as a command line tool so I can set it up to run as a launch agent. I thought this would be a pretty simple thing to do, but I am not getting anything when I run this bit of code.

main.swift:

import AppKit

let userName = "yourUserName"
let password = "yourPassword"
let domain = "yourDomainName"
let ftp = "ftp"
let www = "www"
let checkIPURL = URL(string: "https://svc.joker.com/nic/checkip")
let domainUpdateURL = URL(string: "https://svc.joker.com/nic/update?username=\(userName)&password=\(password)&hostname=\(domain)")
let ftpUpdateURL = URL(string: "https://svc.joker.com/nic/update?username=\(userName)&password=\(password)&hostname=\(ftp).\(domain)")
let wwwUpdateURL = URL(string: "https://svc.joker.com/nic/update?username=\(userName)&password=\(password)&hostname=\(www).\(domain)")
var ipAddress = ""

if let url = checkIPURL {
    print("1 - \(url)")
    var request = URLRequest(url: url)
    print("2 - \(request.url!)")
    request.httpMethod = "POST"
    print("3")
    let session = URLSession.shared
    print("4")
    session.dataTask(with: request) { data, response, error in
        print("4.1")
        guard error == nil else {
            print("Error:", error ?? "")
            return
        }
        print("4.2")
        guard (response as? HTTPURLResponse)?
            .statusCode == 200 else {
            print("down")
            return
        }
        print("4.3")
        if let data = data {
            if let dataString = String(decoding: data, as: UTF8.self).removeHtmlTags() {
                if let startIndex = dataString.lastIndex(of: " ") {
                    let chars = dataString.distance(from: startIndex, to: dataString.endIndex)-1
                    ipAddress = String(dataString.suffix(chars))
                }
            }
            print(ipAddress)
        } else {
            print("No data")
        }
        print("up - \(response!)")
    }.resume()
    print("Done.")
}

extension String {
    // Credit - Andrew - https://stackoverflow.com/questions/25983558/stripping-out-html-tags-from-a-string
    func removeHtmlTags() -> String? {
        do {
            guard let data = self.data(using: .utf8) else {
                return nil
            }
            let attributed = try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
            return attributed.string
        } catch {
            return nil
        }
    }
}

Everything outside of the session prints, but nothing inside of it prints (4.x statements).

I deleted the AppSandbox because when I have AppSandbox as a Capability and turn on Outgoing Connections I get a crash with EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0).

But even with AppSandbox deleted it does not work.

The strange thing is this works fine in a playground (with a slight modification turning the String extension into a function within the playground), which really makes this a head scratcher for me.

Here’s my playground code:

import AppKit

let userName = "yourUserName"
let password = "yourPassword"
let domain = "yourDomainName"
let ftp = "ftp"
let www = "www"
let checkIPURL = URL(string: "https://svc.joker.com/nic/checkip")
let domainUpdateURL = URL(string: "https://svc.joker.com/nic/update?username=\(userName)&password=\(password)&hostname=\(domain)")
let ftpUpdateURL = URL(string: "https://svc.joker.com/nic/update?username=\(userName)&password=\(password)&hostname=\(ftp).\(domain)")
let wwwUpdateURL = URL(string: "https://svc.joker.com/nic/update?username=\(userName)&password=\(password)&hostname=\(www).\(domain)")
var ipAddress = ""

if let url = checkIPURL {
    print("1 - \(url)")
    var request = URLRequest(url: url)
    print("2 - \(request.url!)")
    request.httpMethod = "POST"
    print("3")
    let session = URLSession.shared
    print("4")
    session.dataTask(with: request) { data, response, error in
        print("4.1")
        guard error == nil else {
            print("Error:", error ?? "")
            return
        }
        print("4.2")
        guard (response as? HTTPURLResponse)?
            .statusCode == 200 else {
            print("down")
            return
        }
        print("4.3")
        if let data = data {
            //if let dataString = String(decoding: data, as: UTF8.self).removeHtmlTags() {
            if let dataString = removeHtmlTags(data: data) {
                if let startIndex = dataString.lastIndex(of: " ") {
                    let chars = dataString.distance(from: startIndex, to: dataString.endIndex)-1
                    ipAddress = String(dataString.suffix(chars))
                }
            }
            print(ipAddress)
        } else {
            print("No data")
        }
        print("up - \(response!)")
    }.resume()
    print("Done.")
}

func removeHtmlTags(data: Data) -> String? {
    do {
        let attributed = try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
        return attributed.string
    } catch {
        return nil
    }
}

Is there something else I need to do to get this to work within the command line tool app I am trying to build?

[ad_2]

  • 0 0 Answers
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 318 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 291 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 285 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.