I want a Google web search to open in a Safari for a certain search term, that is set by the app.
I am aware of the openURL method and have used it but I can't get a custom search opened in Safari.
This is the code I've got so far
NSString *searchTerm = [NSString stringWithFormat:#"Soccer World Cup"];
searchTerm = [searchTerm stringByReplacingOccurrencesOfString:#" " withString:#"+"];
NSString *url = [NSString stringWithFormat:#"http://www.google.com/#sclient=psy&hl=en&site=&source=hp&q=%#&aq=f&aqi=&aql=&oq=&gs_rfai=&pbx=1&fp=a20cfd04ba3c5cf9",searchTerm];
NSURL *googleURL = [NSURL URLWithString:url];
if(![[UIApplication sharedApplication] openURL:googleURL]){
NSLog(#"Failed to open URL");
}
This code ends up opening google in Safari but with no search term
Any help will be appreciated! Thanks
# defines a URL fragment, and is typically used to jump to a specific named anchor on the page. Some servers will process that info, but it is rare.
You want to use a ? instead to start your URL query parameters
http://www.google.com/?sclient=....
Looks to me like your url is incorrect. Open up Safari on your Iphone and do a search in the google search field. Copy the url from that. To be more specific, your url should probably begin like this: http://www.google.com/search? (note the question mark, that signals the beginning of the parameters in your url). Each parameter is then a name-value pair like for instance source=hp in your own code example. These pairs are separated with the & character. Most of the parameters in your code example can probably be skipped.
Second, you are going to need to replace more than just spaces in your search term (you need to urlencode it).
Related
Im opening a map like this:
NSString *urlString = [NSString stringWithFormat:#"http://maps.google.com/maps?q=%#,%#", destLat, destLong];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
Is there any way to choose with which app will open the map? For example, Safari, or the maps app, or Tom Tom app...
Yes, you should use one of the iPhone URL schemes. There is a pretty hefty list available here.
Note that you should first check if the application you want to launch is available before using a certain URL scheme.
If Tom Tom exposes some url scheme you can try that one. Otherwise the default url scheme handlers are "hard coded" to the system and you can't override / change them i.e. "http:" will be always opened by Safari except for "maps.google.com" domains, etc.
I want to open iphone apps like Contacts and Calendar from within my phonegap app, I don't mind that doing so will put my app in the background. I can open the browser and using window.open but how do I open other apps?
eg window.open("contacts://", '_blank'); doesn't work
The only way that one app, PhoneGap-based or otherwise, can cause another app to launch is to open an URL that uses the target app's custom URL scheme. So, if the Contacts app supports some custom scheme, you're in luck. If not, you're out of luck.
You are going to need to write a custom phonegap plugin so that you can access custom methods you write in objective C.
The official phonegap documentation is here.
I'll briefly explain how you will do this.
In your javascript you will call this code :
PhoneGap.exec("OpenMailAppPlugin.openMailApp",parameter1);
In objective C you will create a new file OpenMailAppPlugin class. Read the link above for exact instuctions, but the important method will be soemthing like this.
-(void) openMailApp:(NSMutableArray*)paramArray withDict:(NSMutableDictionary*)options {
NSString *parameter1 = [paramArray objectAtIndex:0]; //recieves information from javascript function
NSURL* mailURL = [NSURL URLWithString: #"mailto:%#?cc=bar#example.com&subject=Greetings%20from%Cupertino!&body=Wish%20you%20were%20here!",paramter1];
[[UIApplication sharedApplication] openURL: mailURL];
}
additionally, you may be interested in sending information back to your phonegap application. You can do this by injecting a javascript call that sends parameters. In your objective C function you would do something like this.
NSString * jsCallBack = [NSString stringWithFormat:#"myJavascriptFunction('%#');",parameter];
[self.webView stringByEvaluatingJavaScriptFromString:jsCallBack];
As previously answered, using a URL scheme can work. If you have several apps and you want to be able to open one from the other, then it's really simple when using PhoneGap build: you just need to add the URL scheme to your config.xml file, eg:
// In config.xml
<gap:url-scheme>
<scheme>app1scheme</scheme>
</gap:url-scheme>
Then from your other app you'll just have a link to app1scheme://, eg simply
Start App1
Here is the list of all the iOS/iPhone app url
Video - video:
Music - music:
Youtube - youtube.com
iTunes store - itms:
iBooks - itms-books:
App Store - itms-apps:
Mail sending - mailto:
Telephone - tel:
More can be found at this outdated link
http://www.wiki.akosma.com/IPhone_URL_Schemes
Look at this on how you can implement your's url
https://appurl.org/docs-handling-android
Need to use a plugin, unfortunately you need native ios code:
This one works:
https://github.com/phonegap/phonegap-plugins/tree/master/iOS/ExternalFileUtil
I have a URL containing a fragment part, used to indicate a certain state of the webpage.
I want to open this link in Safari on the iPhone by using something like the following:
NSURL* fragmentURL = [NSURL URLWithString:#"http://www.exampleurl.com/index.html#foo"];
[[UIApplication sharedApplication] openURL:fragmentURL];
The link is successfully opened by Safari, but the fragment part is stripped.
Has anybody got an idea on how to make Safari also use the fragment part?
When you say "the fragment part is stripped", do you mean your web server sees a request with no fragment identifier? That's normal since the UA (browser) is supposed to strip that part.
See https://en.wikipedia.org/wiki/Fragment_identifier#Processing (pun intended!)
I've got an app that I want to be able to use Custom URL schemes for. I want users to be able to open Tweetie using the Custom URL protocol however I need to populate the tweet with dynamic website link which I get using currentItem.link.
I found this code which launches Tweetie and populates a message with static information:
NSString *shortened_url = #"http://your.url.com";
NSString *stringURL = [NSString stringWithFormat:#"tweetie://%#", shortened_url];
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
So using the above code how would I populate the message with currentItem.link information?
Thanks.
It depends entirely on the application on the receiving end. You have to find out how their protocol works, then you can use their protocol as it is designed.
Adding a http:// protocol URL to the end of a tweetie:// protocol URL is not the correct method, and searching for how the Tweetie URL protocol works would be suggested.
The Tweetie protocol is documented, but it's not clear how much of this still applies since the client was converted to the official Twitter one. I believe that the format you want is:
NSString *stringURL = [NSString stringWithFormat:#"tweetie://post?message=%#", shortened_url];
I have already tried this to get the account selection parameter to work. The basic method works, but account selection does not for me.
I have this question that is unsolved for me.... I need to check if the current URL of my webview is a local file in the Documents directory of my app....how can I do this??
Thanks!
You can check:
[request.URL isFileURL];
More information here.
Check the URL's scheme. Assuming you have an NSURL* instance called myURL:
if ([[myURL scheme] isEqualToString:#"file"]) {
NSLog(#"URL %# is local", myURL);
}
Read Apple's NSURL specification to learn more about accessing different parts of an URL.
Assuming you have a UIWebView* called myWebView, you might call something like:
[[myWebView request] URL]
to get the NSURL* of what the web view currently has loaded. Then you can call that object's scheme as described above.
Check for the file:// protocol in the URL.