Xcode openURL doesn't read link - objective-c

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.

Related

iOS: Instagram hook with special characters

I am trying to open Instagram from my app. The tag should contain some norwegian special characters, but the Instagram app is not lauching when I try to execute the code below. I tried with the code example below, and with UTF8 encoding, but without success. As soon as I add a tag without these characters, Instagram launches. Any tips on how to handle this characters?
NSString* hashtag = [NSString stringWithFormat:#"%#", #"instagram://tag?name=øæå"];
NSURL *instagramURL = [NSURL URLWithString:hash];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
[[UIApplication sharedApplication] openURL:instagramURL];
}
Try with the percent escapes like this:
NSString* hashtag = [NSString stringWithFormat:#"%#", #"instagram://tag?name=%C3%B8%C3%A6%C3%A5"];

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];

Xcode HTTP GET Request

I am making an app in which you type a value into a textbox, and the app sends an HTTP GET request to my webserver, ie www.mywebserver.server.com/ihiuahdiuehfiuseh?' + textbox variable' However, I have no idea how to work with Xcode. I am experienced in basic PHP and HTML, and advanced C++, but I am so baffled by this Xcode stuff. In all other languages I have worked with, you could look up something like "how to play a sound file in (language)", and you will get something like "oh yeah just do play(mp3url). But, with Xcode, you have to initiate the connection, initiate the variables, etc etc. I bought 2 $30 books, but I am still so confused. So, back to the point, I just need the textbox numerical number to be parsed after the ? in the URl to be parsed as a variable.
This is an example of a non-http get synchronized
You can find more
-(void)aget:(NSString *)iurl{
NSURL*url = [NSURL URLWithString:iurl];
NSURLRequest *res = [NSURLRequest requestWithURL:url];
NSOperationQueue*que=[NSOperationQueue new];
[NSURLConnection sendAsynchronousRequest:res queue:que completionHandler:^(NSURLResponse*rep,NSData*data,NSError*err){
if ([data length]> 0 && err == nil) {
NSString* rel=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"%#",rel);
}else{
NSLog(#"isnull");
}
}
];
}
Through this launch him
NSString * str = [self getDataFrom: # "your url"];
     NSLog (# "% #", str);
If you have a UITextFiled, you would do the following
NSString baseUrl = #"www.mywebserver.server.com/ihiuahdiuehfiuseh?"
NSString variable = textField.text;
NSString absoluteURL = [NSString stringWithFormat:#"%#%#", baseUrl, variable];
//Send the absolute variable now to the server
If you go for a solution like Omar Abdelhafith suggests, don't forget to url-encode your 'querystring'. There is a method in the string class for this: "stringByAddingPercentEscapesUsingEncoding", but it's not perfect.
I've recently used the solution suggested here: http://simonwoodside.com/weblog/2009/4/22/how_to_really_url_encode/.

Make a phone call when click on a phone number String

I have a phone number like this: 025639879. I got it from the database as a String.
Now i want to make a Phone call exactly when the user click on that number, i have tried to do like this:
NSString *phoneNumber=[#"tel://"stringByAppendingString:myAppGlobalVariables.telephoneTheme];
NSString *html = [NSString stringWithFormat:#"<html><body>Téléphone:%#</body
</html>",phoneNumber];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
[webView loadHTMLString:html baseURL:nil];
What i got in my app is this:
Téléphone:tel://025639879
Am i missing something? thanx in advance.
You can do it in two ways:
skip the tel:// and make your webview recognize phone numbers as links (there will be false positives so be careful with this).
make a number link:
NSString *html = [NSString stringWithFormat:#"<html><body><p>Téléphone:%#</p></body></html>", myAppGlobalVariables.telephoneTheme, myAppGlobalVariables.telephoneTheme];
[webView loadHTMLString:html baseURL:nil];

Declare an NSString With Format Specifiers and use it as a URL to open in UIWebview

I have an int containing a number. I am wanting to declare an NSString so I can use use format specifiers when assigning a value to it.
I thought it might be something like this:
NSString[NSString stringWithFormat] myString;
myString = [#"http://myurl.com/%d",myInt];
I gather this is not the case, so question one is: How do I declare an NSString that can handle format specifiers and then assign it a value using format specifiers? The purpose of this NSString is to hold a URL, exactly like the second line above.
Question two is, How do I then use this string as a URL to open in a UIWebView?
I assume I use something like this:
[webView loadRequest:
Sadly, this is as far as my knowledge stretches. Is there a way I can tell my UIWebView (webView above) to use the NSString with the URL I mentioned earlier?
I intend on having the NSString as a global variable, as it will be assigned it's value inside a C function. And 'webView' will use it inside a (what I think is a) method. All of this code is in the same file, the Delegate.m file. It is all executed on launch of the application.
Your string should look like this:
NSString *myString = [NSString stringWithFormat:#"http://myurl.com/%d", myInt];
What you missed: adding the * to indicate a pointer, and thinking that you had to/could first state that the string would have a format and then later state the format. It all happens at once, creating the string with the specified format.
Edited to add NSURL
To create a url you're creating an object of class NSURL, like this:
NSURL *myURL = [[NSURL alloc] initWithString:myString];
And then you create the url request:
NSURLRequest *request = [NSURLRequest requestWithURL:myURL];
And finally, tell your webView to load the request:
[webView loadRequest:request];
For your first part:
NSString *myString = [NSString stringWithFormat:#"http://myurl.com/%d", myInt];
Then, based on a tutorial from iphonesdkarticles.com:
//Create a URL object.
NSURL *url = [NSURL URLWithString:myString];
//URL Request Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];