Giter Club home page Giter Club logo

percentencoder's Introduction

PercentEncoder

Language CocoaPods License Carthage compatible Issues Platform

PercentEncoder is a lightweight library to escape string using so called URL encoding in Swift.

Introduction

PercentEncoder uses JavaScriptCore framework for URL-encoding although URL-encoding is executed with CFURLCreateStringByAddingPercentEscapes() or NSString.stringByAddingPercentEncodingWithAllowedCharacters() in most cases.
The followings is the reason and has motivated me to make this library.

  • CFURLCreateStringByAddingPercentEscapes() deprecated in iOS9
  • NSString.stringByAddingPercentEncodingWithAllowedCharacters() is introduced from iOS 7. However it won't work in multi-byte languages like Japanes, Chinese, etc and an app crashes as shown at the issue in Alamorefire.

Usage

PercentEncoder offers PercentEncoding enum and String extension methos for URL-encoding.

PercentEncoding

PercentEncoding has four values which are equivalent to Javscript functions(encodeURI, encodeURIComponent, decodeURI, decodeURIComponent).

let url = "http://tasanobu.jp?city=東京&year='20"

/// EncodeURI
let escaped = PercentEncoding.EncodeURI.evaluate(string: url)
// encoded to "http://tasanobu.jp?city=%E6%9D%B1%E4%BA%AC&year='20"

/// DecodeURI
PercentEncoding.DecodeURIComponent.evaluate(string: escaped)
// decoded back to "http://tasanobu.jp?city=東京&year='20"


let value = "東京" // 東京 means Tokyo which is the capital of Japan.

/// EncodeURI
let encoded = PercentEncoding.EncodeURIComponent.evaluate(string: value)
// encoded to "%E6%9D%B1%E4%BA%AC"

/// DecodeURI
PercentEncoding.DecodeURIComponent.evaluate(string: encoded)
// decoded back to "東京"

String extension

PercentEncoding offers String extension methods which are equivalent to Javscript functions(encodeURI, encodeURIComponent, decodeURI, decodeURIComponent).

/// encodeURI
"http://tasanobu.jp?city=東京&year='20".ped_encodeURI()

/// decodeURI
"http://tasanobu.jp?city=%E6%9D%B1%E4%BA%AC&year='20".ped_decodeURI()


/// encodeURIComponent
let encoded = "東京".ped_encodeURIComponent()
// encoded to "%E6%9D%B1%E4%BA%AC"

/// decodeURIComponent
encoded.ped_decodedURIComponent()
// decoded back to "東京"

Requirements

  • Swift 4.0
  • Xcode 9.0
  • iOS 8.0+

Installation

  • Install with Cocoapods

    platform :ios, '8.0'
    use_frameworks!
    
    pod 'PercentEncoder'
  • Git submodule

Release Notes

See https://github.com/tasanobu/PercentEncoder/releases

License

PercentEncoder is released under the MIT license. See LICENSE for details.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.