Open url with variable syntax - iphone

I have a variable agencyWebsite and a label that should open the website when clicked with this method:
- (void)website1LblTapped {
NSURL *url = [NSURL URLWithString:self.agencyWebsite];
[[UIApplication sharedApplication] openURL:url];
}
I get a warning in the compiler saying:
Incompatible pointer types sending UILabel* to parameter of type NSString*
And the app crashes when the link is clicked. Any suggestions?
Edit: Here is what I am doing to make the label clickable
UITapGestureRecognizer* website1LblGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(website1LblTapped)];
// if labelView is not set userInteractionEnabled, you must do so
[self.agencyWebsite setUserInteractionEnabled:YES];
[self.agencyWebsite addGestureRecognizer:website1LblGesture];
What i used to get it working
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://%#", self.agencyWebsite.text]];

If agencyWebsite is of type UILabel*, you need to access its text property instead of passing the object itself to URLWithString:.
- (void)website1LblTapped {
NSURL *url = [NSURL URLWithString:self.agencyWebsite.text];
[[UIApplication sharedApplication] openURL:url];
}
Calling self.agencyWebsite will return your UILabel* object, whereas self.agencyWebsite.text will return a NSString* object containing the text from the label.

Related

Return to application after call is cancelled

I want to use telprompt:// scheme to return to my application after it makes a call. But I have a problem.
If remote person is busy two buttons are shown to me: Redial and Cancel. So if I press Cancel I won't return to my application. Is there any way to handle it?
P. S. Isn't telprompt:// in Private API?
Here You can Use the WebView and Load Request on it.
UIWebView *callWebview = [[UIWebView alloc] init];
NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:#"tel://%#", phoneNumber]
[callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
I am Sure It would work with Charm.....
I think you need:
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:[NSString stringWithFormat:#"telprompt:%#",num]]];

"Current Location" on iPhone Maps.app

I am using this code to run the maps.app from my app.
NSString* urlStr = [NSString stringWithFormat: #"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f", s_lat, s_long, d_lat, d_long];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:urlStr]];
The issue is that I didnt manage to get the current location.
I want something like this:
poi1.url = [NSString stringWithFormat:#"http://maps.google.com/maps?saddr=Current Location&daddr=%f,%f",lat,lon];
Have you tried appending the your label after the position data?
http://maps.google.com/maps?q=37.771008,+-122.41175+(You+can+insert+your+text+here)&iwloc=A&hl=en
Edit: And don't forget to encode your URL correctly!
NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

uibutton onclick does not call/open web page

i have two buttons defined. one of them when clicked must open a compose mail screen and the other when clicked must call. i have this defined as below. but when the button is pressed, it does not open either
-(IBAction) phoneButtonPressed:(id) sender{
NSString *phoneNumber = [[NSString alloc] initWithString:#"4216483330"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
}
-(IBAction) mailButtonPressed:(id) sender{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"mailto:askalibrarian#mail.pitt.edu?subject=ULS Library"]];
}
For the phone#, the url should be prefixed with "tel:" like this:
NSString *phoneNumber = [[NSString alloc] initWithString:#"tel:4216483330"];
For the mail, the problem is you have a space in the url string (in "ULS Library") which needs to be escaped before giving it to NSURL:
NSString *urlString = #"mailto:askalibrarian#mail.pitt.edu?subject=ULS Library";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[urlString
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
See the Apple URL Scheme Reference.

how to create a hyperlink for the NSString variable

I am new to iPhone developemnt.
NSString myUrl = #"www.google.com";
I need to know how to create a hyperlink for the above NSString variable.
In the MFMailComposeViewController I need to use like below
[mailViewController setMessageBody:myUrl isHTML:YES];
Please help me out.
Thanks for any help.
The easiest way is to make the UILabel into a UIButton, style it (use Custom type to get rid of button look). Then connect to an Action that opens safari.
The action should do this:
NSURL *url = [[[ NSURL alloc ] initWithString: #"http://www.example.com" ] autorelease];
[[UIApplication sharedApplication] openURL:url];

Text as Hyperlink in Objective-C

I have a text label with the text set as Contact Us.. When user taps on this label it should launch safari to open the web page.My doubt is how to make Contact Us as hyperlink.Now I can't modify my code to include a UIWebView..Please help me guys..I am in final stages of my project..If possible please help me with sample code..thanks for all your time
The easiest way is to make the UILabel into a UIButton, style it (use Custom type to get rid of button look). Then connect to an Action that opens safari.
The action should do this:
NSURL *url = [[[ NSURL alloc ] initWithString: #"http://www.example.com" ] autorelease];
[[UIApplication sharedApplication] openURL:url];
for XOS use NSWorkspace instead:
- (IBAction)btnPressed:(id)sender {
NSURL *url = [[NSURL alloc] initWithString: #"https://www.google.com"];
[[NSWorkspace sharedWorkspace] openURL:url];
}