How to load address in webview in iPhone? [duplicate] - iphone

This question already has an answer here:
Forward Geocode Example using CLGeocoder
(1 answer)
Closed 4 months ago.
Currently i am working in iPhone application, Using UIWebview to load address like "America", then run the application, the map not shown on the screen. I want to shown the location from User enter search bar field, How to fix this iuissue? please help me
Thanks in Advance
I tried this:
webView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 40, 320,376)];
webView.delegate=self;
webView.scalesPageToFit=YES;
NSString *mapurl = #"http://maps.google.com/maps?q=";
NSString *urlString1 = [mapurl stringByAppendingFormat:#"America"];
NSString *OmitSpace = [urlString1 stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSURL *url=[NSURL URLWithString:OmitSpace];
NSURLRequest *requestObj=[NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
Screen shot:

NSString *OmitSpace = [urlString1 stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
This will omit the whitespace only. It will not convert other escape characters. So replace above line with this.
NSString *OmitSpace = [urlString1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

Related

Passing NSString in NSURL causes app crash [duplicate]

This question already has answers here:
NSURL returns Nil Value
(2 answers)
Closed 8 years ago.
I am passing NSString to NSURL and app crashes. I think this is due to 's used in NSString
NSLog of the NSString is
http://www.test.com/?AssessmentID=040714114412 &QuestionID=113&ResponseText=yes&AssessmentName=Housekeeping&AssessmentDate=11:44:21&AssessmentQuestion=Use the guest’s or employee’s name.&ResponseComment=No comment&DepartmentID=9&SectionName=Service Standards
here is the exception
[NSURL initWithString:relativeToURL:]: nil string parameter'
I think it is crashing due to guest's 's
The problem is your URL is not properly encoded. Take this & modify your code accordingly.
NSString *relativeURL = #"http://www.something with Space and parameters";
NSURL *url = [NSURL URLWithString:[relativeURL stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
This is the right approach rather than replacing white spaces with %20
Hope that helps.
Well i tried out the following piece of code and it is working for me... can you do this as per the following code!
NSString *relativeToURL = #"http://www.test.com/?AssessmentID=040714114412 &QuestionID=113&ResponseText=yes&AssessmentName=Housekeeping&AssessmentDate=11:44:21&AssessmentQuestion=Use the guest’s or employee’s name.&ResponseComment=No comment&DepartmentID=9&SectionName=Service Standards";
NSURL *url = [[NSURL alloc] initWithString:relativeToURL];
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];

Pass special characters through NSURL and NSData [duplicate]

This question already has answers here:
Objective-C and Swift URL encoding
(13 answers)
Closed 9 years ago.
I just learned the hard way that you cannot pass special characters through NSURL. I am in need of a function that will help me pass characters like "&, %, ", ñ" and others via NSURL and NSData.
My code is below. You can see below that I replace the line breaks (\n) and spaces with %20. Is there a function or simple way I can have the types of characters listed above pass through NSURL? Any help would be great! Thank you!
NSString *var1_pre = [myTextView.text stringByReplacingOccurrencesOfString:#" "
withString:#"%20"];
NSString *var1 = [var1_pre stringByReplacingOccurrencesOfString:#"\n" withString:#"%20"];
NSString *strURL = [NSString stringWithFormat:#"http://www.website.com/page.php?
var1=%#",var1];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
Use
NSString *strUrl=[#"YOURURL" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:strUrl];
NSString *urlString = [NSString stringWithFormat:#"URL_STRING"];
NSURL *MyUrl = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
stringByAddingPercentEscapesUsingEncoding. convert Legal URL String.

How to adapte a NSString with a NSUrl parameter? [duplicate]

This question already has answers here:
How do I URL encode a string
(24 answers)
Closed 9 years ago.
As you know no URL contain a space between the word of there parameters
and I want to pass MyString=#"hello every body" to my URL parameters, like this
[#"http://www.site.com/index.php?contenu=" stringByAppendingString:MyString];
And I don't know how I can convert MyString to a valid format for URL
Try with Following Code :
NSString *urlString = [NSString stringWithFormat:#"http://www.site.com/index.php?contenu=%#", MyString];
NSURL *myURL = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
AlsoRead This Official Documentation about String Format Specifiers.
Why don't you do the whole thing in this manner ?
First make a string with parameter in the following way :
NSString *stringURL = [NSString stringWithFormat:#"http://www.site.com/index.php?contenu=%#",MyString];
Convert the string to URL :
NSURL *url = [NSURL URLWithString:MyString];
Now you can use that url safely.
for space u should use %20, for that use url encode
Try This
Or use stringByReplacingPercentEscapesUsingEncoding
You can try this by replacing space with %20
Code ::
NSString *reqString;
reqString = [NSString stringWithFormat:#"%s%#", Server_URL, your_paramater];
reqString = [reqString stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSLog(#"... URL :::: %#", reqString);
Try it once, It may be help you.
Thanks.
try like this ,
NSString *encodedUrlString = [urlString stringByReplacingOccurrencesOfString:#" " withString:#"%20"];

Cant launch Map from app on iOS 6 [duplicate]

This question already has answers here:
Programmatically open Maps app in iOS 6
(12 answers)
Closed 10 years ago.
I am trying to launch a map from within my app, and I am using the following:
- (void) showMap:(id) button {
UIApplication *application = [UIApplication sharedApplication];
NSString *address = [((PhoneButton *) button).cell.client fullAddress];
NSString *addressUrl = [NSString stringWithFormat: #"http://maps.apple.com/?q=%#", address];
NSLog(#"Maps String: %#", addressUrl);
NSURL *map_url = [NSURL URLWithString:addressUrl];
[application openURL:map_url];
};
Well, it is not working. I tried to find the issue but looks like I am doing it right. So, what am I missing?
PS: My address format is like "800, Madison Ave, New York, NY"
Try this.
Your address format is like
"800, Madison Ave, New York, NY"
which contain space between words. Remove space with "+" sign. your final URL should be like below URL.
with this you can launch Map from app on iOS 6.
http://maps.apple.com/?q=800,+Madison+Ave,+New+York,+NY
URLs can't have spaces. Spaces and other special characters need to be escaped properly. You want:
NSString *escapedAddress = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *addressUrl = [NSString stringWithFormat: #"http://maps.apple.com/?q=%#", escapedAddress];

How to a invoke google search on a subview with a search string passed from Main view on an iPhone app

My application has two views and I want to invoke a google search on Second view with a search string (it is name of a product. For example 'Samsung Mobile') passed from application's Main view.
I dont want to enter product name in Google search field manually. It should be done automatically when I press the button on the Main View and the result page should be displayed on Sub View.
-(void) setLabelText:(NSString *) myNewText
{
[productName setText:myNewText];
NSURL *theURL =[NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:theURL];
[webSearchView loadRequest:theRequest];
}
Just wondering is it possible to pass the search string as a parameter with the above function.
Try this:
NSString *urlString = [NSString stringWithFormat:#"http://google.com?q=%#", searchString];
NSURL *theURL =[NSURL URLWithString:urlString];
Don't forget to escape urlString before passing it to NSURL if it contains spaces, special characters, etc.
As per H2CO3's answer, but I think the Google search URL may have changed. I also prefer https over http:
NSString *urlString = [NSString stringWithFormat:#"https://google.com/search?q=%#", searchString];
NSURL *theURL =[NSURL URLWithString:urlString];