Replacement for stringByAddingPercentEscapesUsingEncoding in ios9?
The deprecation message says (emphasis mine):
Use stringByAddingPercentEncodingWithAllowedCharacters(_:) instead, which always uses the recommended UTF-8 encoding, and which encodes for a specific URL component or subcomponent since each URL component or subcomponent has different rules for what characters are valid.
So you only need to supply an adequate NSCharacterSet
as argument. Luckily, for URLs there's a very handy class method called URLHostAllowedCharacterSet
that you can use like this:
let encodedHost = unencodedHost.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())
Update for Swift 3 -- the method becomes the static property urlHostAllowed
:
let encodedHost = unencodedHost.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
Be aware, though, that:
This method is intended to percent-encode an URL component or subcomponent string, NOT an entire URL string.
stringByAddingPercentEscapesUsingEncoding deprecated
Try below line of code:
NSString *encodedString = [modalData.imageURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
stringByAddingPercentEncodingWithAllowedCharacters:
Returns a new string made from the receiver by replacing all
characters not in the specified set with percent encoded characters.
Character sets are passed to - stringByAddingPercentEncodingWithAllowedCharacters
:
+ (NSCharacterSet *)URLUserAllowedCharacterSet;
+ (NSCharacterSet *)URLPasswordAllowedCharacterSet;
+ (NSCharacterSet *)URLHostAllowedCharacterSet;
+ (NSCharacterSet *)URLPathAllowedCharacterSet;
+ (NSCharacterSet *)URLQueryAllowedCharacterSet;
+ (NSCharacterSet *)URLFragmentAllowedCharacterSet;
Reference: https://developer.apple.com/reference/foundation/nsstring/1411946-stringbyaddingpercentencodingwit
stringByAddingPercentEscapesUsingEncoding was deprecated in 9.0 .How to do this?
If you want just fast example look at this code:
NSString * encodedString = [@"string to encode" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
Also check List of predefined characters sets
If you want explanation read the documents or at least this topic: How to encode a URL in Swift
Implicit conversion of 'NSStringEncoding' (aka 'unsigned long') to 'NSCharacterSet * _Nonnull' is disallowed with ARC
You are using the wrong method. Instead of using stringByAddingPercentEncodingWithAllowedCharacters
use this stringByAddingPercentEscapesUsingEncoding
If you wan't to use this method stringByAddingPercentEncodingWithAllowedCharacters
you have to pass a set of allowed characters.
Try this.
NSURL *candidatesURL = [NSURL URLWithString:[str_url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Or if you app development target is IOS 9.0 and above.
stringByAddingPercentEscapesUsingEncoding
is deprecated so you can use stringByAddingPercentEncodingWithAllowedCharacters
and use URLHostAllowedCharacterSet
.
NSURL *candidatesURL = [NSURL URLWithString:[str_url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
Encoding a comma with stringByAddingPercentEscapesUsingEncoding
As noted by @DayS, because comma is a legal URL character. However, if you would like to have control over which characters are escaped, look at CFURLCreateStringByAddingPercentEscapes()
.
NSString *toencode = @",";
NSString *result =
CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,
(__bridge CFTypeRef)toencode,
NULL,
CFSTR(","),
kCFStringEncodingUTF8));
NSLog(@"%@", result);
Cannot assign UIImage from NSURL because of string encoding
do like
func setImageWithString(stringbyName: String?) {
let urlStr = stringbyName.stringByAddingPercentEncodingWithAllowedCharacters(. URLQueryAllowedCharacterSet())
if let url = NSURL(string: urlStr) {
sd_setImageWithURL(url)
}
}
or use like
func setImageWithString(stringbyName: String?) {
let urlStr = stringbyName.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
if let url = NSURL(string: urlStr) {
sd_setImageWithURL(url)
}
}
for additional information see this link
NSData & NSURL - url with space having problem
Use: stringByAddingPercentEscapesUsingEncoding:
Returns a representation of the receiver using a given encoding to determine the percent escapes necessary to convert the receiver into a legal URL string.
-(NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding
A representation of the receiver using encoding to determine the percent escapes necessary to convert the receiver into a legal URL string. Returns nil if encoding cannot encode a particular character
Added per request by @rule
NSString* urlText = @"70.84.58.40/projects/igolf/TipThumb/GOLF 58B.jpg";
NSString* urlTextEscaped = [urlText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString: urlTextEscaped];
NSLog(@"urlText: '%@'", urlText);
NSLog(@"urlTextEscaped: '%@'", urlTextEscaped);
NSLog(@"url: '%@'", url);
NSLog output:
urlText: '70.84.58.40/projects/igolf/TipThumb/GOLF 58B.jpg'
urlTextEscaped: '70.84.58.40/projects/igolf/TipThumb/GOLF%2058B.jpg'
url: '70.84.58.40/projects/igolf/TipThumb/GOLF%2058B.jpg'
Replace occurrences of space in URL
The correct format for replacing space from url is :
Swift 4.2 , Swift 5
var urlString = originalString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
Swift 4
var urlString = originalString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
Objective C
NSString *urlString;//your url string.
urlString = [originalUrl stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
or
urlString = [originalUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
iOS 9 and later
urlString = [originalUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
Related Topics
Command /Usr/Bin/Codesign Failed with Exit Code 1
Duplicate Symbol Error in Nsmanagedobject Subclass
iPhone Gps in Background Never Resumes After Pause
How to Create Custom Mkannotationview and Custom Annotation Title and Subtitle
When Should I Use the Various Storage Mechanisms in iOS
What Are the Device-Width CSS Viewport Sizes of the Iphone6 and iPhone 6 Plus
Uitableview Checkmark Only One Row at a Time
Push Notification Not Receiving in Background iOS
How to Load an Uiimage into a Swiftui Image Asynchronously
How to Make a Button Continually Call a Function When Held Down (Spritekit)
Optional Binding with Try? and As? Still Produces an Optional Type
Nsurlconnection Get Request Returns -1005, "The Network Connection Was Lost"
Autolayout Is Ignored in Custom Uitableviewcell
iPhone App Under Test Crashes After a Few Days