I'm using this code to assign the link of my button to the wiki page, while capturing the countryName.text in the UILabel to be a part of the URL, but Xcode gives me an error when I press it. Here's the code:
- (IBAction)openWiki:(id)sender {
NSString *sampleUrl = [[NSString alloc] initWithFormat:#"http://en.wikipedia.org/wiki/%#%#",self.countryName.text];
NSURL *wikiUrl = [[NSURL alloc] initWithString:sampleUrl];
[[UIApplication sharedApplication] openURL:wikiUrl];}
Thanks in advance.
In your format you expect two parameters, but give only one:
#"http://en.wikipedia.org/wiki/%#%#",self.countryName.text
// ^^
Remove one specifier:
- (IBAction)openWiki:(id)sender {
NSString *sampleUrl = [[NSString alloc]
initWithFormat:#"http://en.wikipedia.org/wiki/%#",self.countryName.text];
// ^^
NSURL *wikiUrl = [[NSURL alloc] initWithString:sampleUrl];
[[UIApplication sharedApplication] openURL:wikiUrl];
}
Related
I am trying to export a photo from my application to Instagram, using this code:
// Capture Screenshot
UIGraphicsBeginImageContextWithOptions(self.view.frame.size,self.view.opaque,0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/test.igo"];
NSURL *igImageHookFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:#"file://%#", jpgPath]];
dic = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:igImageHookFile]];
dic.UTI = #"com.instagram.photo";
dic.annotation = [NSDictionary dictionaryWithObject:#"my caption" forKey:#"InstagramCaption"];
NSURL *instagramURL = [NSURL URLWithString:#"instagram://app"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
[[UIApplication sharedApplication] openURL:instagramURL];
} else {
NSLog(#"No Instagram Found");
}
But it is not working. It crashes with the following error:
2013-01-19 20:40:41.948 Ayat[12648:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter'
I am not sure I'm properly:
1- Saving jpgpath as the screenshot I took .
2- Passing the "dic" document to Instagram.
In my app, I have also export photo to Instagram. (Image must be larger than 612x612 size)
Try this code:
-(void)shareImageOnInstagram:(UIImage*)shareImage
{
//Remember Image must be larger than 612x612 size if not resize it.
NSURL *instagramURL = [NSURL URLWithString:#"instagram://app"];
if([[UIApplication sharedApplication] canOpenURL:instagramURL])
{
NSString *documentDirectory=[NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *saveImagePath=[documentDirectory stringByAppendingPathComponent:#"Image.ig"];
NSData *imageData=UIImagePNGRepresentation(shareImage);
[imageData writeToFile:saveImagePath atomically:YES];
NSURL *imageURL=[NSURL fileURLWithPath:saveImagePath];
UIDocumentInteractionController *docController=[[UIDocumentInteractionController alloc]init];
docController.delegate=self;
[docController retain];
docController.UTI=#"com.instagram.photo";
docController.annotation=[NSDictionary dictionaryWithObjectsAndKeys:#"Image Taken via #App",#"InstagramCaption", nil];
[docController setURL:imageURL];
[docController presentOpenInMenuFromBarButtonItem:self.navigationItem.rightBarButtonItem animated:YES]; //Here try which one is suitable for u to present the doc Controller. if crash occurs
}
else
{
NSLog (#"Instagram not found");
}
}
I hope this will helps you.
On this line
dic = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:igImageHookFile]];
you are calling fileURLWithPath:. For a start this expects an NSString, not an NSURL, but from your error message it looks like igImageHookFile is nil.
It took a bunch of research but I finally found this at this site that helped. I just trimmed it up a bit to not include the caption as that was his original problem... but if you're only putting in the picture, here it is:
-(void)shareImageOnInstagram
{
//Remember Image must be larger than 612x612 size if not resize it.
NSURL *instagramURL = [NSURL URLWithString:#"instagram://app"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]){
NSString *urlString = [[NSString alloc] initWithFormat:#"file://%#", blendedImageURL];
NSURL *imageUrl = [[NSURL alloc] initWithString: urlString];
self.docController = [self setupControllerWithURL:imageUrl usingDelegate:self];
self.docController.UTI = #"com.instagram.exclusivegram";
// self.docController.annotation = [NSDictionary dictionaryWithObject:#"I want to share this text" forKey:#"InstagramCaption"];
[self.docController presentOpenInMenuFromRect: self.view.frame inView: self.view animated: YES ];}
}
I tried the following code and it worked:
- (void) shareImageWithInstagram
{
NSURL *instagramURL = [NSURL URLWithString:#"instagram://"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL])
{
UICachedFileMgr* mgr = _gCachedManger;
UIImage* photoImage = [mgr imageWithUrl:_imageView.image];
NSData* imageData = UIImagePNGRepresentation(photoImage);
NSString* captionString = [NSString stringWithFormat:#"%#%#",self.approvedData.approvedCaption,self.approvedData.tag];
NSString* imagePath = [UIUtils documentDirectoryWithSubpath:#"image.igo"];
[imageData writeToFile:imagePath atomically:NO];
NSURL* fileURL = [NSURL fileURLWithPath:[NSString stringWithFormat:#"file://%#",imagePath]];
self.docFile = [[self setupControllerWithURL:fileURL usingDelegate:self]retain];
self.docFile.annotation = [NSDictionary dictionaryWithObject: captionString
forKey:#"InstagramCaption"];
self.docFile.UTI = #"com.instagram.photo";
// OPEN THE HOOK
[self.docFile presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES];
}
else
{
[UIUtils messageAlert:#"Instagram not installed in this device!\nTo share image please install instagram." title:nil delegate:nil];
}
}
In my app, I have a set of location and their relative annotation. I have a button where cliking it, i go to a method that call a URL web service to get the google street view.
My code is:
- (NSString*)urlStreetview:(id<MKAnnotation>)annotation{
CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];
NSLog(#"lat:%f",pinLocation.coordinate.latitude);
NSLog(#"lon:%f",pinLocation.coordinate.longitude);
NSString *lat_string = [[NSString alloc] initWithFormat:#"%f", pinLocation.coordinate.latitude];
NSString *lon_string = [[NSString alloc] initWithFormat:#"%f", pinLocation.coordinate.longitude];
NSString *url1 = [NSString stringWithFormat:#"http:myweb/streetview.php"];
NSString *url2 = [url1 stringByAppendingString: #"?lat="];
NSString *url3 = [url2 stringByAppendingString:lat_string];
NSString *url4 = [url3 stringByAppendingString:#"&lon="];
NSString *url = [url4 stringByAppendingString:lon_string];
NSLog(#"url:%#",url);
return url;
}
I wrote the previous method to have a url that is composed with the LatLong of the annotation.
I have a button called PressMe on the annotation pin. When I push it, I want to to excute the previous method, to calle the url, but I do not understand how pass the selected annotation to the streetView method.
- (IBAction)PressMe:(id)sender
{
UIViewController *webViewController = [[[UIViewController alloc] init] autorelease];
UIWebView *uiWebView = [[[UIWebView alloc] initWithFrame: CGRectMake(0,0,320,480)] autorelease];
[uiWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: [self urlStreetview: ????]]]]; //HERE IS MY MY PROBLEM
[webViewController.view addSubview: uiWebView];
webViewController.title = #"web bar";
[self.navigationController pushViewController:webViewController animated:YES];
//[[self navigationController] pushViewController:thirdViewController animated:YES];
}
thanks in advance!
I am going to answer my question myself. I just resolve my problem an it work perfectly. The code allows to show a web view in order to have a steet View google service of the annotation map Kit annotation. Obviusly, as I am relatively new iphone developer,if someone wants to add/suggest/improve, he is welcome.
My button implementation:
- (IBAction)PressMe:(id)sender
{
UIViewController *webViewController = [[[UIViewController alloc] init] autorelease];
UIWebView *uiWebView = [[[UIWebView alloc] initWithFrame: CGRectMake(0,0,320,480)] autorelease];
[uiWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: self.urlStreetview]]];
[webViewController.view addSubview: uiWebView];
webViewController.title = #"web bar";
[self.navigationController pushViewController:webViewController animated:YES];
//[[self navigationController] pushViewController:thirdViewController animated:YES];
}
With this button I call my (following) method "urlStreetview" the, with the annotation selected, composes a string in order to have a complet url with the apropriete GET php parameter. The url is the direction of my (very simple) web service that allow to call the google API street View.
- (NSString*)urlStreetview{
//there can only be one selected annotation so get the one at index 0
id<MKAnnotation> selectedAnnotation = [_mapView.selectedAnnotations objectAtIndex:0];
CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:selectedAnnotation.coordinate.latitude longitude:selectedAnnotation.coordinate.longitude];
//Now I have the annotation coordinate selected, so now I am able to compound the urladdingo to my base url:http://myurl/streetview.php, the GET part, I mean the lat lon coords:
NSString *lat_string = [[NSString alloc] initWithFormat:#"%f", pinLocation.coordinate.latitude];
NSString *lon_string = [[NSString alloc] initWithFormat:#"%f", pinLocation.coordinate.longitude];
NSString *url1 = [NSString stringWithFormat:#"http://myurl/streetview.php"];
NSString *url2 = [url1 stringByAppendingString: #"?lat="];
NSString *url3 = [url2 stringByAppendingString:lat_string];
NSString *url4 = [url3 stringByAppendingString:#"&lon="];
NSString *url = [url4 stringByAppendingString:lon_string];
NSLog(#"url:%#",url);
return url;
}
I hope It can be useful!
I am trying to initiate a call from my iphone app,and I did it the following way..
-(IBAction) call:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Call Besito" message:#"\n\n\n"
delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Submit", nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex != [alertView cancelButtonIndex])
{
NSString *phone_number = #"0911234567"; // assing dynamically from your code
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"tel:%#", phone_number]]];
NSString *phone_number = #"09008934848";
NSString *phoneStr = [[NSString alloc] initWithFormat:#"tel:%#",phone_number];
NSURL *phoneURL = [[NSURL alloc] initWithString:phoneStr];
[[UIApplication sharedApplication] openURL:phoneURL];
[phoneURL release];
[phoneStr release];
}
}
by the above code..I am able to successfully make a call..but when I end a call,I'm not able to return to my app
So, I want to know how to achieve,that..also please tell me how we can initiate a call using webview...
This is my code :
NSURL *url = [NSURL URLWithString:#"telprompt://123-4567-890"];
[[UIApplication sharedApplication] openURL:url];
Use this so that after call end it will return to app.
UIWebView *callWebview = [[UIWebView alloc] init];
NSURL *telURL = [NSURL URLWithString:#"tel:+9196815*****"];
[callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
Please try this it will definitely let you Back to Your App.
NSString *phoneNumber = // dynamically assigned
NSString *phoneURLString = [NSString stringWithFormat:#"tel:%#", phoneNumber];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
[[UIApplication sharedApplication] openURL:phoneURL];
following code will not return to your app [no alertview will show before make call]
UIWebView *callWebview = [[UIWebView alloc] init];
NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:#"tel://%#",phoneNumber]];
[callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
following code will return to your app "alertview will show before make call"
UIWebView *callWebview = [[UIWebView alloc] init];
NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:#"telprompt://%#", phoneNumber]];
[callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
You can also use webview, this is my code :
NSURL *url = [NSURL URLWithString:#"tel://123-4567-890"];
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(50, 50, 150, 100)];
[self.view addSubview:btn];
UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectMake(50, 50, 150, 100)];
webview.alpha = 0.0;
[webview loadRequest:[NSURLRequest requestWithURL:url]];
// Assume we are in a view controller and have access to self.view
[self.view insertSubview:webview belowSubview:btn];
[webview release];
[btn release];
It's not possible to return back to the app after ending a call because it's an app.
i have one problem with my app.Im building an app having navigation controller.when i select a particular row,its related url will be loaded.it is working fine.but how to get back from this view to the previous view?I tried to place a navigation bar top of the view but it is not visible.could u please help me out.....
I placed the following coding in did selectrowmethod
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Get the selected country
NSString *selectedCountry = [listOfItems objectAtIndex:indexPath.row];
//NSString *url=#"";
if (indexPath.row==0) {
//url=#"http://www.osha.gov/dcsp/osp/stateprogs/alaska.html" ;
//label.text=url;
NSLog(#"======");
NSURL *url = [[NSURL alloc] initWithString:#"http://labor.state.ak.us/lss/oshhome.htm"];
[[UIApplication sharedApplication] openURL:url];
// self.label.text=#"url";
// NSLog(#"++++++:%#",self.label.text);
//textView.text=#"url";
}
else if(indexPath.row==1) {
NSURL *url = [[NSURL alloc] initWithString:#"http://www.ica.state.az.us/ADOSH/"];
[[UIApplication sharedApplication] openURL:url];
//url=#"http://www.osha.gov/dcsp/osp/stateprogs/arizona.html";
}
else if(indexPath.row==2) {
NSURL *url = [[NSURL alloc] initWithString:#"http://www.dir.ca.gov/occupational_safety.html"];
[[UIApplication sharedApplication] openURL:url];
//url=#"http://www.osha.gov/dcsp/osp/stateprogs/arizona.html";
}
else if(indexPath.row==3) {
NSURL *url = [[NSURL alloc] initWithString:#"http://www.ctdol.state.ct.us/osha/osha.htm"];
[[UIApplication sharedApplication] openURL:url];
//url=#"http://www.osha.gov/dcsp/osp/stateprogs/arizona.html";
}
}
Your code opens Safari. You can't go back from there. I suggest you check out UIWebView.
Ok, I understand that using strings that have special characters is an encoding issue.
However I am not sure how to adjust my code to allow these characters. Below is the code that works great for text that contains no special characters, but can you show me how and where to change the code to allow for the special characters to be used. Right now those characters crash the app.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) {
//iTunes Audio Search
NSString *stringURL = [NSString stringWithFormat:#"http://phobos.apple.com/WebObjects/MZSearch.woa/wa/search?WOURLEncoding=ISO8859_1&lang=1&output=lm&term=\"%#\"",currentSong.title];
stringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
}
}
And this:
-(IBAction)launchLyricsSearch:(id)sender{
WebViewController * webView = [[WebViewController alloc] initWithNibName:#"WebViewController" bundle:[NSBundle mainBundle]];
webView.webURL = [NSString stringWithFormat:#"http://www.google.com/m/search?hl=es&q=\"%#\"+letras",currentSong.title];
webView.webTitle = #"Letras";
[self.navigationController pushViewController:webView animated:YES];
}
Please show me how and where to do this for these two bits of code.
-[NSString stringByAddingPercentEscapesUsingEncoding:]
NSASCIIStringEncoding usually works best for URL encoding.
If you only want to escape certain characters, use CFURLCreateStringByAddingPercentEscapes().
You should use the percent-escapes only to the currentSong.title and not to the entire URL. Here's what it should look like:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) {
//iTunes Audio Search
NSString *stringURL = [NSString stringWithFormat:
#"http://phobos.apple.com/WebObjects/MZSearch.woa/wa/search?WOURLEncoding=ISO8859_1&lang=1&output=lm&term=\"%#\"",
[currentSong.title stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
}
}
-(IBAction)launchLyricsSearch:(id)sender{
WebViewController * webView = [[WebViewController alloc] initWithNibName:#"WebViewController" bundle:[NSBundle mainBundle]];
webView.webURL = [NSString stringWithFormat:
#"http://www.google.com/m/search?hl=es&q=\"%#\"+letras",
[currentSong.title stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
webView.webTitle = #"Letras";
[self.navigationController pushViewController:webView animated:YES];
}
For non-ASCII characters such as ñ, the NSUTF8StringEncoding should properly handle them. NSUTF8StringEncoding is the recommended encoding for URLs.
Ok Dave's answer works great for Part 1:
Working Code:
NSString *stringURL = [NSString stringWithFormat:#"http://phobos.apple.com/WebObjects/MZSearch.woa/wa/search?WOURLEncoding=ISO8859_1&lang=1&output=lm&term=\"%#\"",currentSong.title];
stringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
However, Part 2 isn't there yet (I know its me... but could you help?)
webView.webURL = [NSString stringwithFormat:#"http://www.google.com/m/search?hl=es&q=\"%#\"+letras",currentSong.title];
webView.webURL = [webView.webURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
What would be the correct code to get webView.webURL to allow spanish characters like "ñ"?