Skip to content
Simple interface for Punycode and IDNA on String and NSString
Swift Objective-C C Ruby
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github
Mac (Swift)
Mac
Punycode.xcodeproj
PunycodeCocoa
PunycodeCocoaTouch
PunycodeSwiftTests
PunycodeTests
Shared
iOS
icumap2code
.gitignore
.travis.yml
LICENSE
PunyCocoa copy-Info.plist
Punycode-Cocoa.podspec
README.md

README.md

Punycode Cocoa

Build Status

v2.0 (2020) by Nate Weaver (Wevah)
https://derailer.org/
https://github.com/Wevah/Punycode-Cocoa

A simple punycode/IDNA String extension and NSString category, based on code and documentation from RFC 3492 and RFC 3490.

Use this to convert internationalized domain names (IDN) between Unicode and ASCII.

To use in your own projects, you need to include some files from the Shared/Swift folder, and make sure the uts46 data file is copied to your application's Resources folder. The required Swift files are:

  • UTS46.swift
  • UTS46+Loading.swift
  • Data+Extensions.swift
  • Scanner+Extensions.swift
  • String+Punycode.swift

(UTS46+Conversion.swift is for importing text files containing UTS #46 mappings and exporting to the binary format used by UTS46+Loading.swift, and isn't necessary for most projects.)

The Objective-C NSString+Punycode files are compatible with both ARC and Manual Retain Release modes.

Note: The Objective-C implementation currently on master is pretty out-of-date, but the Swift version has appropriate @objc extensions. I may update the Objective-C implementation eventually!

Note that these API currently don't do homograph detection.

Macros (Objective-C only)

Define PUNYCODE_COCOA_USE_WEBKIT to have Punycode Cocoa methods call internal WebKit methods instead of the custom implementations. Useful if you're already linking against WebKit, or want additional homograph attack protection. However, this probably won't be allowed on the App Store due to its use of private methods.

Define PUNYCODE_COCOA_USE_ICU to use ICU (by default the system's ICU). Using the system ICU may get your app rejected if you intend put it on the App Store. Compiling and bundling your own ICU libs (from http://sute.icu-project.org/) is almost certainly allowed, though you will want to build NSString+Punycode with the headers from your downloaded library/source.

Interface

String/NSString

Swift:
var idnaEncoded: String? { get }
Objective-C:
@property (readonly, copy, nullable) NSString *IDNAEncodedString;

If self contains non-ASCII, encodes the string's domain components as Punycode and prepends xn-- to the transformed components.


Swift:
var idnaDecoded: String? { get }
Objective-C:
@property (readonly, copy, nullable) NSString *IDNADecodedString;

Decodes a string returned by idnaEncoded/-IDNAEncodedString.


Swift:
var encodedURLString: String? { get }
var decodedURLString: String? { get }
Objective-C:
@property (readonly, copy, nullable) NSString *encodedURLString;
@property (readonly, copy, nullable) NSString *decodedURLString;

Performs Punycode encode/decode operations on each appropriate part (the domain bits) of an URL string, and URL encodes/decodes the path/query/fragment.


URL/NSURL

Swift:
init?(unicodeString: String)
Objective-C:
+ (nullable instancetype)URLWithUnicodeString:(NSString *)URLString;

Convenience initializer equivalent to URL(string: unicodeString.encodedURLString)/ [NSURL URLWithString:URLString.encodedURLString].


Swift:
var decodedURLString: String? { get }
Objective-C:
@property (readonly, copy, nullable) NSString *decodedURLString;

Convenience property equivalent to someURL.absoluteString.decodedURLString.


Swift:
init?(unicodeString: String, relativeTo url: URL?) {
Objective-C:
+ (nullable instancetype)URLWithUnicodeString:(NSString *)URLString relativeToURL:(nullable NSURL *)baseURL;

Convenience initializer for creating a URL from a Unicode string, relative to another URL.


© 2012–2020 Nate Weaver (Wevah)

You can’t perform that action at this time.