i am the fresher in ios and i want to make the autocomplete search bar using google API... please tell me how can i make it ... only autocomplete search bar using google API ..
i used this following url ,, but how to complete with search bar ...
- (NSString *)description
{
return [NSString stringWithFormat:#"Query URL: %#", [self googleURLString]];
}
- (NSString *)googleURLString
{
NSMutableString *url = [NSMutableString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=%#&sensor=%#&key=%#",
[self.input stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
SPBooleanStringForBool(self.sensor), self.key];
if (self.offset != NSNotFound) {
[url appendFormat:#"&offset=%u", self.offset];
}
if (self.location.latitude != -1) {
[url appendFormat:#"&location=%f,%f", self.location.latitude, self.location.longitude];
NSLog(#"%#",url);
}
if (self.radius != NSNotFound) {
[url appendFormat:#"&radius=%f", self.radius];
}
if (self.language) {
[url appendFormat:#"&language=%#", self.language];
}
return url;
}
Check out https://github.com/spoletto/SPGooglePlacesAutocomplete
It's an objective-c wrapper for the Google Places Autocomplete API.
Good Luck!
Related
I am trying to add the facebook comment box to my web based app but it does not show up when I install on my phone. Is there some special plugin or methodology for getting the facebook comment plugin to work with phonegap and/or ionic? I currently have this plugin in my config:
<gap:plugin name="com.phonegap.plugins.facebookconnect" version="0.9.0">
I managed to do it with iframe also.
I solved resizing with https://github.com/davidjbradshaw/iframe-resizer
for login i had to change the native code.
iOS: MainViewController.m
- (BOOL) webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
if (![url isFileURL] && (navigationType == UIWebViewNavigationTypeLinkClicked))
{ //this is for loading links in external browser
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
return NO;
}
} else if ([[url absoluteString] containsString:#"m.facebook.com"] && [[url absoluteString] containsString:#"login"]) {
NSString *forceInAppBrowserJS = [NSString stringWithFormat:
#"var loginWindow = cordova.InAppBrowser.open('%#', '_blank', 'location=yes');\n\
loginWindow.addEventListener('exit', function(){\n\
window.fbIframe.src = window.fbIframe.src;\n\
});", url]; //reload the iframe after login, you have to assign the iframe in js
[theWebView stringByEvaluatingJavaScriptFromString: forceInAppBrowserJS];
return NO;
}
return [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
}
Android: CordovaWebViewClient.java
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!url.contains("file://")) {
if (url.contains("m.facebook.com") && url.contains("login")) {
String forceInAppBrowserJS = String.format("var loginWindow = cordova.InAppBrowser.open('%s', '_blank', 'location=yes,hardwareback=yes'); loginWindow.addEventListener('exit', function(){window.fbIframe.src = window.fbIframe.src;});", url);
view.evaluateJavascript(forceInAppBrowserJS, null);
return true;
}
if (!url.startsWith("http://") && !url.startsWith("https://"))
url = "http://" + url;
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(browserIntent); //open link in external browser
return true;
}
return helper.shouldOverrideUrlLoading(view, url);
}
The best solution I have found so far is to call the comments box from inside an iframe. The only problems with this methodology so far is logging in and setting the iframe height. I am still working on a solution
I am trying to authorize LinkedIn with iPhone..
I am using following code to redirect url
NSString *authUrl = [NSString stringWithFormat:#"https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=%#&scope=%#&state=%#&redirect_uri=%#" ,
API_KEY ,
#"r_fullprofile",
#"ASDKASIIWER23432KKQ",
#"http://www.myappname.com"
];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: authUrl]];
In my Url types, i have added URL Scheme as http:// and url identifier as
www.myappname.com
however after authorizing , i don't get back to my application from browser.
Any ideas where i am wrong?
i have used a diff approach now, i have used webView inside app and is using it. Works perfect so far.
- (void)viewDidLoad
{
[super viewDidLoad];
[self.myWebView setDelegate:self];
self.indicator = [[CustomActivityViewer alloc] initWithView:self.view];
NSString *authUrl = [NSString stringWithFormat:#"https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=%#&scope=%#&state=%#&redirect_uri=%#" ,
API_KEY ,
#"r_fullprofile rw_nus r_emailaddress r_network w_messages",
SCOPE_CODE
REDIRECT_URI
];
authUrl = [authUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:authUrl]]];
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
[self.indicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[self.indicator stopAnimating];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[self.indicator stopAnimating];
}
- (BOOL) webView: (UIWebView *) webView shouldStartLoadWithRequest: (NSURLRequest *) request navigationType: (UIWebViewNavigationType) navigationType
{
NSURL *url = request.URL;
NSLog(#"%#", url.absoluteString);
if ( [url.host isEqualToString:HOST])
{
URLParser *parser = [[URLParser alloc] initWithURLString:url.absoluteString];
NSString *code = [parser valueForVariable:#"code"];
if (code != nil)
{
NSString *authUrl = [NSString stringWithFormat:#"https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code&code=%#&redirect_uri=%#&client_id=%#&client_secret=%#",
code,
REDIRECT_URI_OAUTH,
API_KEY,
SECRET_KEY];
NSLog(#"%#" , authUrl);
authUrl = [authUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[Utilities responseFromURL:[NSURL URLWithString:authUrl] completionBlock:^(NSString *response, NSError *err)
{
if (err != nil)
{
[Utilities errorDisplay];
}
else
{
NSDictionary *results = [response JSONValue];
[defaults setObject:[results objectForKey:#"access_token"] forKey:#"access_token"];
}
}];
}
}
return YES;
}
You are going to need to register a custom URL scheme, as iOS and OS X don't have a way to redirect a specific host within a URL scheme.
Generally, you can use something like x-myapp: as the url scheme.
Further, the URL Identifier is not a host, but an identifier that describes the URL scheme, much like a UTI identifier for a file type describes a specific file type. For example, you could use com.myCompany.myApp.url or something similar as the identifier.
You should be fine if you create a scheme of form x-myapp: and then use that as the redirect URL.
An example from a proposed Info.plist would be:
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>com.myCompany.myApp.url</string>
<key>CFBundleURLSchemes</key>
<array>
<string>x-myapp</string>
</array>
</dict>
The CFBundleURLName corresponds in the Xcode GUI to URL Identifier.
So I check if the string starts with "http://" using the code below, and then I want to add "http://" so that I'm able to open the page in UIWebView.
NSString *firstString = [NSString stringWithFormat:#"%#", URL.text];
NSString *check = [NSString stringWithFormat:#"%#", #"http://"];
if (firstString != check) {
NSString *newString = [NSString stringWithFormat:#"http://%#", URL.text];
newString = [newString substringWithRange:NSMakeRange(7, newString.length - 7)];
URL.text = newString;
}
[WebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:URL.text]]];`
This doesn't work for some reason.
Do you know why?
Somebody posted as I was writing, but either way, here's an answer:
You are making this too difficult. You simply need to use hasPrefix to check for "http". As an example, I use this for my unified search/url bar.
- (IBAction)go:(id)sender {
NSString *inputString = [searchField stringValue];
NSString *outputString = [inputString stringByReplacingOccurrencesOfString:#" " withString:#"+"];
if ([inputString hasPrefix:#"http://"]) {
//Has Prefix
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:inputString]]];
}
else
{
//Does not have prefix. Do what you want here. I google it.
NSString *googleString = #"http://google.com/search?q=";
NSString *searchString = [NSString stringWithFormat:#"%#%#", googleString, outputString];
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:searchString]]];
}
}
That is for googling instead, you could keep using NSString *newString = [NSString stringWithFormat:#"http://%#", URL.text];
You could add a few more checks as well if you wanted to. Good luck!
firstString != check checks to see if both objects point to the same location in memory. [firstString isEqualToString:check] checks if the two strings are equal. However, what you most likely want to do is if(![firstString hasPrefix:check]). This will check to make sure firstString does not start with check, then you can append check to the start of it. Alternatively, you can do firstString = [firstString stringByReplacingOccurrencesOfString:#"http://" withString:#""];, and then you know it will never start with #"http://"
Try printing what URL.text is before the loadRequest line via NSLog(#"%#",URL.text);
Also the preferred check condition would be if (![firstString isEqualToString:check]) {}
I am trying to make a simple iphone program that will allow me to search a keyword when entered into a textfield and then search for it by clicking a rect button. I have searched the site and there is nothing that relates to my current situation. to reiterate, I have a textfield where I would like to enter some text and search for it by clicking a rect button. I have the information i want to search for in a txt file. What is the appropriate way to to go about doing this in xcode for iphone. I appreciate the help, thanks.
If I understand your question correctly, You are trying to find a user defined phrase in a text file.
What you should do is read the file to an NSString, and then search the string for the user defined string.
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/SearchingStrings.html
Googled it quickly and found an example (Not written by me!):
NSString *searchFor = #"home";
NSRange range;
for (NSString *string in stringList)
{
range = [word rangeOfString:searchFor];
if (range.location != NSNotFound)
{
NSLog (#"Yay! '%#' found in '%#'.", searchFor, string);
}
}
If I missunderstood you, please update your question so it is better understandable.
Try this:
-(void)searchWord:(NSString *)wordToBeSearched{
NSString* path = [[NSBundle mainBundle] pathForResource:#"wordFile"
ofType:#"txt"];
NSString* content = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:NULL];
NSString *delimiter = #"\n";
NSArray *items = [content componentsSeparatedByString:delimiter];
NSString *character = #" ";
BOOL isContain = NO;
for (NSString *wordToBeSearched in items) {
isContain = ([string rangeOfString:wordToBeSearched].location != NSNotFound);
}
return isContain;
}
You can go here for more details
What's an efficient way to take an NSURL object such as the following:
foo://name/12345
and break it up into one string and one unsigned integer, where the string val is 'name' and the unsigned int is 12345?
I'm assuming the algorithm involves converting NSURL to an NSString and then using some components of NSScanner to finish the rest?
I can only add an example here, the NSURL class is the one to go. This is not complete but will give you a hint on how to use NSURL:
NSString *url_ = #"foo://name.com:8080/12345;param?foo=1&baa=2#fragment";
NSURL *url = [NSURL URLWithString:url_];
NSLog(#"scheme: %#", [url scheme]);
NSLog(#"host: %#", [url host]);
NSLog(#"port: %#", [url port]);
NSLog(#"path: %#", [url path]);
NSLog(#"path components: %#", [url pathComponents]);
NSLog(#"parameterString: %#", [url parameterString]);
NSLog(#"query: %#", [url query]);
NSLog(#"fragment: %#", [url fragment]);
output:
scheme: foo
host: name.com
port: 8080
path: /12345
path components: (
"/",
12345
)
parameterString: param
query: foo=1&baa=2
fragment: fragment
This Q&A NSURL's parameterString confusion with use of ';' vs '&' is also interesting regarding URLs.
NSURL has a method pathComponents, which returns an array with all the different path components. That should help you get the integer part. To get the name I'd use the host method of the NSURL. The docs say, that it should work if the URL is properly formatted, might as well give it a try then.
All in all, no need to convert into a string, there seems to be plenty of methods to work out the components of the URL from the NSURL object itself.
Actually there is a better way to parse NSURL. Use NSURLComponents. Here is a simle example:
Swift:
extension URL {
var params: [String: String]? {
if let urlComponents = URLComponents(url: self, resolvingAgainstBaseURL: true) {
if let queryItems = urlComponents.queryItems {
var params = [String: String]()
queryItems.forEach{
params[$0.name] = $0.value
}
return params
}
}
return nil
}
}
Objective-C:
NSURLComponents *components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
NSArray *queryItems = [components queryItems];
NSMutableDictionary *dict = [NSMutableDictionary new];
for (NSURLQueryItem *item in queryItems)
{
[dict setObject:[item value] forKey:[item name]];
}
Thanks to Nick for pointing me in the right direction.
I wanted to compare file urls but was having problems with extra slashes making isEqualString useless. You can use my example below for comparing two urls by first de-constructing them and then comparing the parts against each other.
- (BOOL) isURLMatch:(NSString*) url1 url2:(NSString*) url2
{
NSURL *u1 = [NSURL URLWithString:url1];
NSURL *u2 = [NSURL URLWithString:url2];
if (![[u1 scheme] isEqualToString:[u2 scheme]]) return NO;
if (![[u1 host] isEqualToString:[u2 host]]) return NO;
if (![[url1 pathComponents] isEqualToArray:[url2 pathComponents]]) return NO;
//check some properties if not nil as isEqualSting fails when comparing them
if ([u1 port] && [u2 port])
{
if (![[u1 port] isEqualToNumber:[u2 port]]) return NO;
}
if ([u1 query] && [u2 query])
{
if (![[u1 query] isEqualToString:[u2 query]]) return NO;
}
return YES;
}