Entering textfield values into an URL - objective-c

So I'm in the progress of developing an app but I'm kinda stuck at the moment. I have 2 textfields right now that the user will have to enter:
Ordernr:xxxxxxxx
Referencenr:xxxxxxx
these 2 values have to be added to this link:
http://www.artis-web.de/cgi-bin/WebObjects/Artis.woa/wa/detailedTrack?referencenr=xxxxxxxx&ordernr=xxxxxxxxxx
now I want safari to open this link. The ideal situation would be the user entering the 2 textfields values and then pressing a button called "open in safari"
What would be the best way to implement this?

Just add the following code to your button action:
NSString *urlString = [NSString stringWithFormat:#"http://www.artis-web.de/cgi-bin/WebObjects/Artis.woa/wa/detailedTrack?referencenr=%#&ordernr=%#", referenceNrTextField.text, orderNrTextField.text];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: urlString]];

To create the URL you can use...
NSURL *url = [NSURL urlWithString:[NSString stringWithFormat:#"http://www.artis-web.de/cgi-bin/WebObjects/Artis.woa/wa/detailedTrack?referencenr=%#&ordernr=%#", referenceTextField.text, ordernoTextField.text]];
To open in safari...
[[UIApplication sharedApplication] openURL:url];

Check whether the both fields have data and then hit the url with the given data.This condition would be sufficient.
if (Ordernr.text.length!=0 && Referencenr.text.length!=0)
{
NSLog(#"Hit the Url with the entered data");
}
else
{
NSLog (#"Show Error Alert Message");
}

Related

Sending data to a webpage that is not mine

Is it possible to have a viewController in an iOS app with some textboxes where the user inputs some strings and then with a button sends it to a webpage which is NOT mine? Here is an example of a webpage (http://www.nattklubbvictoria.se/#/list) with some choices, text boxes and a Send button. The websites is using some sort of widget. If it is possible, how?
Currently I have a uiweview but it's not so good looking and it's slow to load it. So it would be awesome if I could do this!
You could do something as simple as construct a GET URL & forward the user to that URL when the user clicks the button.
For example (not tested):
NSString *stringToSendUserTo = #"http://www.site.com/form.php?foreignParameterA=";
stringToSendUserTo = [stringToSendUserTo stringByAppendingString:firstTextField.text];
stringToSendUserTo = [stringToSendUserTo stringByAppendingString:#"&foreignParameterB="];
stringToSendUserTo = [stringToSendUserTo stringByAppendingString:secondTextField.text];
stringToSendUserTo = [stringToSendUserTo stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *URLToSendUserTo = [NSURL URLWithString:stringToSendUserTo];
if ([[UIApplication sharedApplication] canOpenURL:URLToSendUserTo]) {
[[UIApplication sharedApplication] openURL:URLToSendUserTo];
} else {
// do something
}

How to load a facebook page link on iOS Facebook App

I'm trying to open a Facebook link and using the following code would conform with the URL protocol on iOS.
NSString *url = [self.dataSource getFacebookURL];
url = [url stringByReplacingOccurrencesOfString:#"http://www.facebook.com/" withString:#"fb://profile/"];
NSLog(#"LINK: %#", url);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
But when trying to load a profile or a page (i.e. of an organization) it always brings me to the screen that was left open last. Is there a way of linking directly to the page and profile? I followed the instructions as described here, but no success.
The page I want to link to is "https://www.facebook.com/junction11" if that helps...
Thanks, Max
FIX
So I found a solution which is to load the group/person/page's graph values first, parse them and then make a new url with the Facebook-ID. The code works (don't know how well) and looks like this:
// Look at graph page instead of www page
NSString *dataURL = [url stringByReplacingOccurrencesOfString:#"www" withString:#"graph"];
// Load data and parse using JSON parser
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:dataURL]];
NSError *error;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (!error) { NSLog(#"ERROR: %#", error); return; }
// If no error occurred, make new url and replace the username by the facebook-ID
url = [url stringByReplacingOccurrencesOfString:[dictionary objectForKey:#"username"] withString:[dictionary objectForKey:#"id"]];
// And voilà, it works :]
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
Thank you google and time...
NSURL *url = [NSURL URLWithString:#"fb://profile/<id>"];
[[UIApplication sharedApplication] openURL:url];
Schemes
fb://profile – Open Facebook app to the user’s profile
fb://friends – Open Facebook app to the friends list
fb://notifications – Open Facebook app to the notifications list (NOTE: there appears to be a bug with this URL. The Notifications page opens. However, it’s not possible to navigate to anywhere else in the Facebook app)
fb://feed – Open Facebook app to the News Feed
fb://events – Open Facebook app to the Events page
fb://requests – Open Facebook app to the Requests list
fb://notes – Open Facebook app to the Notes page
fb://albums – Open Facebook app to Photo Albums list

Remove part of the url

I am working with Sencha Touch and PhoneGap. The code is for iOS and it's waiting for url with suffix #phonegap-external ..
- (BOOL) webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
if ( ([url fragment] != NULL) && ([[url fragment] rangeOfString:#"phonegap=external"].location != NSNotFound))
{
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
return NO;
}
}
return [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
}
So because I haven't written any line of code in Obj-C, I need your help. Can someone edit code, so that it would open url without suffix.
EDIT:
If user opens url in app, it would open it inside webview but on occasion I would prefer that url is opened in Safari. So this code checks if url has suffix like this - http://google.com#phonegap-external and than it opens it in Safari. Only thing what bugs me, is url is not changed into http://google.com and it opens given url http://google.com/#phonegap-external. Could someone please fix this.
If you're sure that the part of the URL that indicates whether it's to be opened inline or externally (i. e. the #phonegap-external string) is always the last one in the URL, then you can try removing this suffix by writing something like as follows:
NSString *orig = [url absoluteString];
size_t frLen = [#"phonegap-external" length];
NSString *stripped = [orig substringToIndex:orig.length - frLen];
NSURL *newURL = [NSURL URLWithString:stripped];
[[UIApplication sharedApplication] openURL:newURL];

open a facebook page -is it true?

I
have read everywhere so many different things about opening the facebook app from another app, and some says its not official and can be changed ,some say its ok,
but anyway it doesnt work for me (and for many others-with broken pages)
i use :
NSURL *url = [NSURL URLWithString:#"fb://pages/MY-APP-PAGE"];
[[UIApplication sharedApplication] openURL:url];
its open the facebook app, but with a white page. on my mac- this link is working.
so, is this thing is real ?
since i KNOW many people know how to do that , and are not answering,
well , thats how its done :
NSURL *fanPageURL = [NSURL URLWithString:#"fb://profile/PAGE ID"];
[[UIApplication sharedApplication] openURL:fanPageURL];
Where PAGE ID is the long id number at the end of your facebook page .
Thanks for this answer.
To support more users, I used this one:
NSURL *nsurl = [ [ NSURL alloc ] initWithString: #"fb://profile/463560897057026" ];
if (![[UIApplication sharedApplication] canOpenURL:nsurl])
nsurl = [ [ NSURL alloc ] initWithString: #"http://www.facebook.com/SmileyGames" ];
[[UIApplication sharedApplication] openURL:nsurl];

Xcode openURL doesn't read link

I have an animal list and special button. When I press the button, I would like to go to Wikipedia and read about this animal more. So I wrote this code:
-(IBAction)goWiki:(id)sender
{
NSString *wikiUrl = "http://ru.wikipedia.org/wiki/";
NSString *url = [NSString stringWithFormat:#"%#%#",wikiUrl,animalTitle];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:url]];
NSLog(#"%#",url);
}
NSLog shows that url was written correctly, however, nothing happened. I am 99,9% sure its because of animalTitle. My native language is russian and animalTitle is also an animal name in russian.
So if link is like http://ru.wikipedia.org/wiki/Frog its fine and it works but if its like
http://ru.wikipedia.org/wiki/Лягушка nothing happens.
Any ideas, how can I move to a russian article?
Thanks!
use stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding as follows -
-(IBAction)goWiki:(id)sender
{
NSString *wikiUrl = #"http://ru.wikipedia.org/wiki/";
NSString *url = [NSString stringWithFormat:#"%#%#",wikiUrl,animalTitle];
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:url]];
NSLog(#"%#",url);
}
Try passing the string animalTitle through CFURLCreateStringByAddingPercentEscapes first.