- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to URL encode a string (NSString) in iPhone?
When developing API based web applications we definitely need to interect with Multiple web services and URLs. The url may contain special character, search terms, queries, headers and many other things depending on the service we need. That’s why we need to have some kind of encoding so that the URL we are creating and the URL being called are same.
To achieve the same with Objective C we can use −
#import "NSString+URLEncoding.h" @implementation NSString (URLEncoding) -(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding { return (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)self, NULL, (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ", CFStringConvertNSStringEncodingToEncoding(encoding)); } @end
Another way to achieve URL encoding in Objective C is −
NSString *sUrl = @"http://www.myService.com/search.jsp?param= name"; NSString *encod = [sUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
Similarly, URL encoding can be achieved in Swift like −
func getURL(str: String ) { return str.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) }
Which is going to return an encoded URL string and can be used like,
var sURL = " http://www.myService.com/search.jsp?param= name" print(getURL(sURL))
Which will print the following as a result.
http://www.myService.com/search.jsp?param= name
- Related Articles
- How to encode and decode a URL in JavaScript?
- How to encode a URL using JavaScript function?
- How can I encode a URL in Android?
- How to encode and decode URl in typescript?
- How to encode a string in JavaScript?
- How to encode the string in android?
- How can I encode a string to Base64 in Swift?
- How to replace a character in Objective-C String for iPhone SDK?
- How to get parameters from a URL string in PHP?
- Encode string array values in Numpy
- How to check if a string is a valid URL in Golang?
- Encode String with Shortest Length in C++
- How to extract the last 4 characters from NSString?
- Java program to check for URL in a String
- C# program to check for URL in a String

Advertisements