0

how to implement this code from PHP to iOS Swift 3 ?

base64_encode("qywueifk85oflmvJ");

And result is something like this:

GJXIFSs5tA0LRmCANJS98g==

reza_khalafi
  • 6,230
  • 7
  • 56
  • 82

1 Answers1

2

To do this, you could create an extension on the String class. This would look like this:

extension String 
{
    func toBase64() -> String 
    {
        return Data(self.utf8).base64EncodedString()
    }
}

You can then simply call the function on the string.

let stringToEncode = "qywueifk85oflmvJ"
let encodedString = stringToEncode.toBase64()

If you don't want to use an extension you can simply create the function:

func base64(from: String) -> String 
{
    return Data(from.utf8).base64EncodedString()
}

This would make the call look like:

let encodedString = base64(from: "qywueifk85oflmvJ")