ios - Check NSString is encoded or not -


how detect nsstring encoded or not.?

i'm encoding string below. before encoding want verify weather [product url] encoded or not.

nsstring *encodedurlstring=[[product url] stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; 

you try decoding string , see if original string , decoded string same or not. if same wasn't encoded yet.

nsstring *original = product.url; nsstring *decoded = [original stringbyreplacingpercentescapesusingencoding:nsutf8encoding]; if ([original isequaltostring:decoded]) {     // url not encoded yet     nsstring *encodedurlstring=[[product url] stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; } else {     // url encoded } 

btw - these methods deprecated of ios 9 if app's deployment target ios 9.0 or later should use newer methods.

for ios 9 or later should use:

nsstring *original = product.url; nsstring *decoded = [original stringbyremovingpercentencoding]; if ([original isequaltostring:decoded]) {     // url not encoded yet     nsstring *encodedurlstring=[[product url] stringbyaddingpercentencodingwithallowedcharacters:[nscharacterset alphanumericcharacterset]]; } else {     // url encoded } 

Comments

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -