Retrieve data from website database+how to - iphone

I have made a project and when people put a keyword into UITextfield and click the button then my app will retrieve the data from website database.
It is not working here is the code:
- (IBAction) btnClickMe_Clicked:(id)sender {
NSString *kw = s.text; // <-----THIS IS THE UITextField
NSURL *url = [NSURL URLWithString:#"http://www.example.com/index.php?keyword=",kw];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[iMessageLabel loadRequest:request];
}
Any one could help me?

You aren't putting the keyword into the string. You need something like this:
NSString *kw = s.text;
NSString *encodedkw = [ky stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat: #"http://www.example.com/index.php?keyword=%#", encodedkw];
NSURL *url = [NSURL URLWithString:urlString];
Then look at how to use NSURLConnection to figure out how to put the data into the label.

Related

UIWebView and Safari content not looking the same

I have a UIWebView which is adapted to a mobile style of a forum I have. In Safari, it looks and works great.
However, inside my WebView there is a disastrous problem.
When quoting or editing a post, HTML is displayed inside of BBcode. Furthermore, posting code results in ignored line breaks.
The webview is loaded like this:
//display the webview
NSString *fullURL = #"http://www.mysite.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSMutableURLRequest *requestObj = [NSMutableURLRequest requestWithURL:url];
[_webTest loadRequest:requestObj];
Change your this line:
NSMutableURLRequest *requestObj = [NSMutableURLRequest requestWithURL:url];
With this line & check:
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
Do you want to load a html-string in your webView?
If it is the thing than following may help you:
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:bundlePath];
NSString *htmlString = [bundlePath pathForResource:#"index" ofType:#"html"] ;
[webView loadHTMLString:htmlString baseURL:baseURL];

NSURL add parameters to fileURLWithPath method

I use this line of code to load a local html file into a web view:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:#"html"]];
However I want to add some http parameters to the url with no luck so far.
I've tried this:
url = [url URLByAppendingPathComponent:#"?param1=1"];
But after this a html doesn't load in webview.
Is there a way to load local html file in webview with params ?
Do this:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:#"html"]];
NSString *URLString = [url absoluteString];
NSString *queryString = #"?param1=1";
NSString *URLwithQueryString = [URLString stringByAppendingString: queryString];
NSURL *finalURL = [NSURL URLWithString:URLwithQueryString];
NSURLRequest *request = [NSURLRequest requestWithURL:finalURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:(NSTimeInterval)10.0 ];
[web loadRequest:request];
The most upvoted answer by Prince doesn't always work.
In iOS 7 Apple introduced NSURLComponents class, we can leverage that to securely add a query into NSURL.
NSURL *contentURL = ...;
NSURLComponents *components = [[NSURLComponents alloc] initWithURL:contentURL resolvingAgainstBaseURL:NO];
NSMutableArray *queryItems = [components.queryItems mutableCopy];
if (!queryItems) queryItems = [[NSMutableArray alloc] init];
[queryItems addObject:[NSURLQueryItem queryItemWithName:#"access_token" value:#"token_here"]];
components.queryItems = queryItems;
NSURL *newURL = components.URL;
I use like this way:
NSURL *url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"xml"]];
If your file included into the Resource folder of project navigator. otherwise you have to set your full path in NSString and use that in your NSURL path.

Passing URL with parameter

I am passing url with some string value and NSInteger value in this url but when I put breakpoint on this and I tab on trace then it show me this message exc-bad-access in url I given bold please see that 'bold' I want to pass there value:
[ NSInteger day,NSInteger day1,NSString *fromDate1, NSString *fromDate1,NSString *OriginCode,NSString *DestinCode].
I get all value on url when I put the breakpoint but when I step into breakpoint my app crashes, why it crash? Help me. Where am I wrong?
-(void)sendRequest
{
stringWithFormat:#"http://www.google.com?AvailabilitySearchInputFRSearchView%24ButtonSubmit=Search%20For%20Flights%20&AvailabilitySeast=",day,day1,DestinCode,"2011-09","2011-09",OriginCode];
NSString *urlString = [NSString stringWithFormat:#"http://www.bookairways tickt.com/Sales/FRSearch.aspx?AvailabilitySearchInputFRSearchView%24ButtonSubmit=Search%20For%20Flights%20&AvailabilitySearchInputFRSearchView%24DropDownListMarketDay1=**%i**&AvailabilitySearchInputFRSearchView%24DropDownListMarketDay2=**%i**&AvailabilitySearchInputFRSearchView%24DropDownListMarketDestination1=**%#**&AvailabilitySearchInputFRSearchView%24DropDownListMarketMonth1=**%#**&AvailabilitySearchInputFRSearchView%24DropDownListMarketMonth2=**%#**&AvailabilitySearchInputFRSearchView%24DropDownListMarketOrigin1=**%#**&AvailabilitySearchInputFRSearchView%24DropDownListPassengerType_ADT=1&AvailabilitySearchInputFRSearchView%24DropDownListPassengerType_CHD=0&AvailabilitySearchInputFRSearchView%24DropDownListPassengerType_INFANT=0&AvailabilitySearchInputFRSearchView%24RadioButtonFlowSelector=FlightAndCar&AvailabilitySearchInputFRSearchView%24RadioButtonMarketStructure=RoundTrip&AvailabilitySearchInputFRSearchView%24discountPax=0&__EVENTARGUMENT=&__EVENTTARGET=&__VIEWSTATE=%2FwEPDwUBMGRkg4UKvNNb1NbM14%2F2n9zUxhNQ%2B%2BA%3D&errorlist=",day,day1,DestinCode,fromDate1,fromDate2,OriginCode];
//urlString=[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [NSURL URLWithString:urlString];
NSLog(#"************url:%#",url);
NSURLRequest *theRequest=[NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
webData = [[NSMutableData data] retain];
NSLog(#"%#",webData);
} else {
}
}
make your url properly like this:-
NSURL *url = [NSURL URLWithString:[*yourstring* stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Harish this will help u to in creating the url check this our http://wiki.akosma.com/IPhone_URL_Schemes
like this
NSString *template = #"appigotodo://com.example.xyzapp/import?name=%#&note=%#&due-date=%#&priority=%#&repeat=%#";
NSString *name = #"Buy%20some%20milk";
NSString *note = #"Stop%20on%20the%20way%20home%20from%20work.";
NSString *dueDate = #"2009-07-16";
NSString *priority = #"1";
NSString *repeat = #"101";
NSString *stringURL = [NSString stringWithFormat:template, name, note, dueDate, priority, repeat];
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
Two things:
The URL has many % symbols that are not being used as placeholders. The % symbols that are not between '**' in your code need to be escaped like so: %%. In other words, SearchInputFRSearchView%24Button should be SearchInputFRSearchView%%24Button.
You are using %i to put integers into your string. You should be using %d instead.

feed://... in my UIWebView

I want to open RSS Feed in my UIWebView, but it isn't working and opening Safari.
My code:
NSString *link = [[NSString alloc] initWithFormat:#"feed://smartfiction.disqus.com/%#/latest.rss", slug];
NSURL *url = [NSURL URLWithString:link];
NSURLRequest *requestWeb = [NSURLRequest requestWithURL:url];
[webViewComment loadRequest:requestWeb];
I found right way. I have created rss parser and showed feed in UITableView. It's more beautiful and easy.
feed:// is a URL Scheme which is handled by Safari and so will only be opened by Safari... you could try :
NSString *link = [[NSString alloc] initWithFormat:#"feed://smartfiction.disqus.com/%#/latest.rss", slug];
NSURL *url = [NSURL URLWithString:link];
NSData *rssData = [NSData dataWithContentsOfURL:url];
[webViewComment loadData:rssData MIMEtype:#"application/rss+xml" textEncodingName:#"utf-8" baseURL:nil];

iPhone SDK: WebView how to specify a local URL?

I have HTML files that I want to load locally. I have included the files in the resources folder in XCode. I am not sure what the syntax is to load them.
This is the code I use to connect to say google.
NSString *urlAddress=#"http\\someurl";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webHelpView loadRequest:requestObj];
Could someone provide an example where a HTML file is loaded locally.
Thanks in advance.
This is the only code that worked for me.
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"SettingsHelp.html"];
NSURL *url = [NSURL fileURLWithPath:path isDirectory:NO];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
Thanks all for the help.
Can't you just do
NSURL *url = [NSURL URLWithString:#"file:///path/to/file"];
?
Sure, here is some code from an app that loads a locallly stored html page into a web view:
- (void) viewDidLoad
{
// Load the content into the view
NSString* path = [NSString stringWithFormat: #"%#/index.html",
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0]];
[webView_ loadRequest: [NSURLRequest requestWithURL: [NSURL fileURLWithPath: path]]];
}