I am developing ios application where I am waiting for response which should be uploaded image Url. I am converting NSData to NSString this way.
NSString* resultInString = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
when I log resultInString, I get __NSCFString * #"\"http:\/\/em.avatars.s3.amazonaws.com\/avatarsd765c404-887c-4c0e-a08b-f7066ec9befe.png\"" 0x17777080
I have no idea how to validate this url to set UIImageview in my application. please give me a hint.
before say something about solution i think your getting url from NSData is incorrect.
The response seems to be JSON-encoded. So simply decode the response string using a JSON library (SBJson, JsonKit etc.)
or you can user correct encode for your NSData.
after all you can create a NSUrl from your string & if it's exist so it's valid.
i write a sample code for you
you should remove some charectar from your url by this way or like this
[urlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
and for validation
NSUrl * url = [[NSURL alloc] initWithString:urlString];
if(url){ //valid url
}
I want to use customize QR, I have a url, if I pass that url in browser they display a QR in browser and if i use this url in our code they return null, this is my url that I want to use in our code:
http://api.qrcode.unitag.fr/api?t_pwd=degraded&setting={"EYES":{"EYE_TYPE":"LLLeft","COLOR_EHD":"8a9935","COLOR_IHD":"8a9935","COLOR_EBG":"71801f","COLOR_IBG":"71801f"},"BODY_TYPE":2,"LAYOUT":{"COLORBG":"ffffff","GRADIENT_TYPE":"HORI","COLOR1":"afc928","COLOR2":"d7eb67","FORCE_SHADOW":"L","COLOR_SHADOW":"b6b8a7"}}&data={"DATA":{"MESSAGE":"Hello","PHONE":"0505050505"},"TYPE":"smsto"}
This is my code for encode that url
NSString *unescaped = #"http://api.qrcode.unitag.fr/api?t_pwd=degraded&setting={%22EYES%22:{%22EYE_TYPE%22:%22LLLeft%22,%22COLOR_EHD%22:%228a9935%22,%22COLOR_IHD%22:%228a9935%22,%22COLOR_EBG%22:%2271801f%22,%22COLOR_IBG%22:%2271801f%22},%22BODY_TYPE%22:2,%22LAYOUT%22:{%22COLORBG%22:%22ffffff%22,%22GRADIENT_TYPE%22:%22HORI%22,%22COLOR1%22:%22afc928%22,%22COLOR2%22:%22d7eb67%22,%22FORCE_SHADOW%22:%22L%22,%22COLOR_SHADOW%22:%22b6b8a7%22}}&data={%22DATA%22:{%22MESSAGE%22:%22Hello%22,%22PHONE%22:%220505050505%22},%22TYPE%22:%22smsto%22}";
NSString *escapedString = [unescaped stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
NSLog(#"escapedString: %#", escapedString);
NSURL *url = [NSURL URLWithString:escapedString];
[img_barcode setImageWithURL:url placeholderImage:nil];
i am using above code but i am not getting image, please tell me about this process how i can get image successfully.
The URL in your code is already escaped. By escaping it again, you destroy it.
Either start with a properly URL escaped string and don't do any further processing on it:
NSString *escaped = #"http://api.qrcode.unitag.fr/api?t_pwd=degraded&setting={%22EYES%22{%22EYE_TYPE%22:%22LLLeft%22,%22COLOR_EHD%22:%228a9935%22,%22COLOR_IHD%22:%228a9935%22,%22COLOR_EBG%22:%2271801f%22,%22COLOR_IBG%22:%2271801f%22},%22BODY_TYPE%22:2,%22LAYOUT%22{%22COLORBG%22:%22ffffff%22,%22GRADIENT_TYPE%22:%22HORI%22,%22COLOR1%22:%22afc928%22,%22COLOR2%22:%22d7eb67%22,%22FORCE_SHADOW%22:%22L%22,%22COLOR_SHADOW%22:%22b6b8a7%22}}&data={%22DATA%22{%22MESSAGE%22:%22Hello%22,%22PHONE%22:%220505050505%22},%22TYPE%22:%22smsto%22}";
Or start with the base url and the parameters, then URL escape each parameter value and combine it into the complete URL:
Base URL: http://api.qrcode.unitag.fr/api
t_pwd: degraded
setting: {"EYES":{"EYE_TYPE":"LLLeft","COLOR_EHD":"8a9935","COLOR_IHD":"8a9935","COLOR_EBG":"71801f","COLOR_IBG":"71801f"},"BODY_TYPE":2,"LAYOUT":{"COLORBG":"ffffff","GRADIENT_TYPE":"HORI","COLOR1":"afc928","COLOR2":"d7eb67","FORCE_SHADOW":"L","COLOR_SHADOW":"b6b8a7"}}
data: {"DATA":{"MESSAGE":"Hello","PHONE":"0505050505"},"TYPE":"smsto"}
So the easiest solution is probably to remove the code lines that run the URL encoding.
Finally i solved the problem thank every one.
that was my url:
http://api.qrcode.unitag.fr/api?t_pwd=degraded&setting={"EYES":{"EYE_TYPE":"LLLeft","COLOR_EHD":"8a9935","COLOR_IHD":"8a9935","COLOR_EBG":"71801f","COLOR_IBG":"71801f"},"BODY_TYPE":2,"LAYOUT":{"COLORBG":"ffffff","GRADIENT_TYPE":"HORI","COLOR1":"afc928","COLOR2":"d7eb67","FORCE_SHADOW":"L","COLOR_SHADOW":"b6b8a7"}}&data={"DATA":{"MESSAGE":"Hello","PHONE":"0505050505"},"TYPE":"smsto"}
and i just added back slash before the (") (") like this \"EYES\", and after that i use this line of code:
NSString *escapedUrlString = [urlString stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
NSURL * imageURL = [NSURL URLWithString:escapedUrlString];
[img_barcode setImageWithURL:imageURL placeholderImage:nil];
and my problem is solved.
The project was created with xcode 4.3.
I have used AFNetworking Library (non-ARC) in this app.
NSURL *url = [NSURL URLWithString:urlstring];
[exhibitPortraitImageView setImageWithURL:url];
Here, when I print a NSLog value, am getting the URL but the imageview is not displayed.
How can I solve this?
Your code is fine, problem is with URL.
Your URL, don't have prefix, http://, I added it and the code is same as in quesiton.
NSString *urlstring = #"http://mdb.scicloudsolutions.com:8001/sites/default/files/genesis-book-of-beginnings.jpg";
NSURL *url = [NSURL URLWithString:urlstring];
[exhibitPortraitImageView setImageWithURL:url];
Here, you've three options to fix this,
1) If URLs are not coming from server (or some where else) you can fix it within the app, see how,
NSString *badUrlString = #"mdb.scicloudsolutions.com:8001/sites/default/files/genesis-book-of-beginnings.jpg";
NSString *goodUrlString = [NSString stringWithFormat:#"http://%#",badUrlString];
2) Or if its coming from server (or some where else) you can ask them to fix this from their side.
3) If its under 2nd option then, ask them if this will always happen (static) then you can also modify this from your side if server side developers not able to fix.
see this for more help, Check string containing URL for "http://"
Yes,
I also tried your url, it is working fine.
I loaded it as:
NSURL *correctURL = [NSURL URLWithString:#"http://mdb.scicloudsolutions.com:8001/sites/default/files/genesis-book-of-beginnings.jpg"];
_imgFromUrl.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:correctURL]];
And the result,
Have a look at the below code it's working properly
NSString *urlString = [NSString stringWithFormat:#"http://maps.google.com/maps?daddr=%f,%f&saddr=%f,%f", 23.0300, 72.5800, 22.3000, 70.7833];
NSString *escapedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:escapedString];
[[UIApplication sharedApplication]openURL:url];
but it opens map in safari browser.
after that i've tried below code
NSString *urlString = [NSString stringWithFormat:#"http://maps.apple.com/maps?daddr=%f,%f&saddr=%f,%f", 23.0300, 72.5800, 22.3000, 70.7833];
NSString *escapedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:escapedString];
[[UIApplication sharedApplication]openURL:url];
it open map app as i desired but not giving me the directional result with route or the same result that above google map gives how can i achieve the result as first code in map app?
please help!!
Apple's map does not have the same API as google maps, thus you URL will not work. With iOS 6 apple introduced the MKMapItem which allows developers to interact with the maps.app.
If you want to keep using the the maps via http then you should change you url:
NSString *urlString = [NSString stringWithFormat:#"http://maps.apple.com/?daddr=%f,%f&saddr=%f,%f", 23.0300, 72.5800, 22.3000, 70.7833];
NSString *escapedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:escapedString];
[[UIApplication sharedApplication]openURL:url];
As stated in the Apple URL Scheme Reference you should not add the /maps/ path.
URLs that contain no path parameters or that contain specific map
paths are opened in Safari and displayed there. For example, URLs
based on the paths http://maps.apple.com/, http://maps.apple.com/maps,
http://maps.apple.com/local, and http://maps.apple.com/m are all
opened in Safari. To open a URL in the Maps app, the path must be of
the form http://maps.apple.com/?q.
The rules for creating a valid map link are as follows:
The domain must be maps.apple.com.
The path cannot be /maps/*.
A parameter cannot be q=* if the value is a URL (so KML is not picked up).
The parameters cannot include view=text or dirflg=r
Is there a url (.txt) for the source code of each website?
If not, how can i get the source code of a webpage and be able to show it in a UITextView ?
You should be able to use something like this:
NSString *googleString = #"http://www.google.com";
NSURL *googleURL = [NSURL URLWithString:googleString];
NSError *error;
NSString *googlePage = [NSString stringWithContentsOfURL:googleURL
encoding:NSASCIIStringEncoding
error:&error];
This will put the contents of the google home page into googlePage and, if applicable, the error into error. If the page being loaded uses Unicode characters, try NSUTF8StringEncoding instead of NSASCIIStringEncoding.