iPhone QR code customize - ios

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.

Related

make response Url Valid Ios

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
}

NSURL URLWithString returns nil

I am stuck in a very obvious method of NSURL class URLWithString I am passing a string and while creating URL it returns nil, my string is perfect. When ever I uses same string in browser then it is working fine. I am using following method to convert NSString to NSURL:
NSURL *url = [NSURL URLWithString:urlString];
//urlString is my perfect string
I have also tried to encode my string first by using following
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
// using this line my output string when I log url is "url%10%10%10%10%10%10..."
%10 becomes the suffix of my url with around 100+ repetition.
If any one has idea what %10 is or what can be done to overcome this problem.
here is my urlString:
Use below code to resolve your problem.
NSString *str = msgDetail[#"thumbnail"];
NSString* webStringURL = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
NSURL* url = [NSURL URLWithString:webStringURL];
yes what was wrong in my string were many unidentified characters due to copying from internet site, if anyone of the reader facing this issue can copy and paste your string as see the count. And confirm.
Thanks
Try below solution
NSURL *url = [NSURL URLWithString:[urlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Check it..!

Pass an Url to image view through AFNetworkingLibrary

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,

IOS - How do I add segments to NSURL?

Suppose you have a URL that looks like http://localhost:5867 and your wanted to append '/something' to the url. How do I append segments to this URL?
I am trying this:
//_baseURL is NSURL* and documentid is NSString*
NSURL* url = [_baseURL URLByAppendingPathComponent:documentid];
Oddly.. xcode(4.6.2) will not output the resulting URL to console.
NSLog(#"%#", [url absoluteString]);//nothing output
Thanks!
Edit
I am working in a workspace. The code that I am debugging is a static lib which is in the workspace. Could this be the culprit of all this weirdness??
Fixed
I think the reason I was having problems was because my workspace was not configured correctly and I was actually executing an older version of my static lib. This explains why I wasn't getting NSLog'ing and why the result were unexpected. I found this post which helped me understand the workspace and how to configure it for a static library. Thanks a lot for everyone's time!
There is a class message,
+ (id)URLWithString:(NSString *)URLString relativeToURL:(NSURL *)baseURL
So,
NSURL *base = [NSURL URLWithString: #"http://localhost:5867"];
NSURL *child = [NSURL URLWithString: #"something" relativeToURL:base];
NSURL *base = [NSURL URLWithString:#"http://localhost:5867"];
NSURL *url = [base URLByAppendingPathComponent:#"something"];
NSLog(#"%#", [url absoluteString]);
This works for me and prints http://localhost:5867/something, so check your variables (maybe one of them is nil or an empty string?).

iOS: WebView Loading a url

I am trying to open the following url in UIWebView but it fails to load whereas changing it to:
http://www.google.com
works fine.
The url that I want to load is:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#%#%#%#%#",#"http://m.forrent.com/search.php?address=",[[bookListing objectForKey:#"Data"] objectForKey:#"zip"],#"&beds=&baths=&price_to=0#{\"lat\":\"0\",\"lon\":\"0\",\"distance\":\"25\",\"seed\":\"1622727896\",\"is_sort_default\":\"1\",\"sort_by\":\"\",\"page\":\"1\",\"startIndex\":\"0\",\"address\":\"",[[bookListing objectForKey:#"Data"] objectForKey:#"zip"],#"\",\"beds\":\"\",\"baths\":\"\",\"price_to\":\"0\"}"]]]];
UPDATE:
I have purposely escaped the double quotes otherwise it gives me an error.
I checked the url by opening in my browser (on laptop) and it works perfectly fine:
The url in browser:
http://m.forrent.com/search.php?address=92115&beds=&baths=&price_to=0#{%22lat%22:%220%22,%22lon%22:%220%22,%22distance%22:%2225%22,%22seed%22:%221622727896%22,%22is_sort_default%22:%221%22,%22sort_by%22:%22%22,%22page%22:%221%22,%22startIndex%22:%220%22,%22address%22:%2292115%22,%22beds%22:%22%22,%22baths%22:%22%22,%22price_to%22:%220%22}
Your line of code looks convoluted, but basically it is a very simple one.
You should breakup this code from a one liner to multiple lines that are more readable.
That will also allow you to log and check the URL you actually created, like so:
NSLog(#"My url: %#", urlString);
Update:
I see you added the full url. Webview indeed fails to load that url (UIWebkit error 101).
The part of the url that causes the problem is the '#' character and dictionary that follows in the params. You should url encode that part of the url.
Try this:
NSString *address = #"http://m.forrent.com/search.php?";
NSString *params1 = #"address=92115&beds=&baths=&price_to=0";
// URL encode the problematic part of the url.
NSString *params2 = #"#{%22lat%22:%220%22,%22lon%22:%220%22,%22distance%22:%2225%22,%22seed%22:%221622727896%22,%22is_sort_default%22:%221%22,%22sort_by%22:%22%22,%22page%22:%221%22,%22startIndex%22:%220%22,%22address%22:%2292115%22,%22beds%22:%22%22,%22baths%22:%22%22,%22price_to%22:%220%22}";
params2 = [self escape:params2];
// Build the url and loadRequest
NSString *urlString = [NSString stringWithFormat:#"%#%#%#",address,params1,params2];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
The escaping method I used:
- (NSString *)escape:(NSString *)text
{
return (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
(__bridge CFStringRef)text, NULL,
(CFStringRef)#"!*'();:#&=+$,/?%#[]",
kCFStringEncodingUTF8);
}
I would try encoding all of the key/value items in your url. Specifically the curly braces ({}) and the hash (#) symbols may be causing a problem.

Resources