Passing coords of annotation Mapkit tapped - iphone

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!

Related

QLPreviewController - Displaying issue

code snippets
NSMutableArray *samplepdf = [[NSMutableArray alloc]initWithObjects:#"sam1.pdf",#"sam2.pdf",#"sam3.pdf",#"sam4.pdf", nil];
1) QLPreviewController *previewController = [[QLPreviewController alloc] init];
previewController.dataSource = self;
previewController.currentPreviewItemIndex = [indexPath row];
[self presentModalViewController:previewController animated:YES];
2)
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)previewController
{
return [samplepdf count];
}
// returns the item that the preview controller should preview
- (id)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)index
{
NSString *documentsDirectoryPath = [[NSBundle mainBundle]resourcePath];
NSString *dataPath =[documentsDirectoryPath stringByAppendingPathComponent:[samplepdf objectAtIndex:index]];
NSURL *url = [NSURL fileURLWithPath:dataPath isDirectory:YES];
return url;
}
This code Work perfectly in iOS5 ,but when iam running in ios6 some space is visible.
How can i solve this issue?
Thanks in Advance :)
The call to presentModalViewController:animated: is deprecated in iOS6. You might want to specify a modal transition style and modal presentation style prior to calling presentViewController:animated:. You could try something like this:
[previewController setModalPresentationStyle:UIModalPresentationFullScreen];
[previewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentViewController:previewController animated:YES completion:nil];

iphone mapview get directions

I would like to find a simple way to get directions. Is there any way to do this with mapview?
Below is my current code.
- (IBAction)goToMap:(id)sender {
[self.view addSubview:self.mapUIView];
[self.mapUIView setFrame:CGRectMake(0, 0, 320, 460)];
self.mapview.mapType = MKMapTypeStandard;
CLLocationCoordinate2D coordinate;
coordinate.latitude = [[self.find lat] floatValue];
coordinate.longitude = [[self.find lng] floatValue];
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:coordinate];
[annotation setTitle:[self.find name]];
//[annotation setSubtitle:#"Hello"];
[self.mapview addAnnotation:annotation];
[annotation release];
[self zoomMapViewToFitAnnotations:self.mapview animated:YES];
}
You cannot use MapKit for driving directions. It does not support this.
If you want to show the current position of device (like while driving) you have to add CoreLocation framework in your project and should use "updateLocation" method for getting current location but if you want to draw a path between two location you have to use Google Api for this.
If you want to plot directions use:
https://github.com/kishikawakatsumi/MapKit-Route-Directions
CLLocationManager *manager = [[CLLocationManager alloc] init] ;
CLLocationCoordinate2D currentLocation = [manager location].coordinate;
CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(37.365502,-94.394531);
NSString *fromLocation = [NSString stringWithFormat: #"%f,%f",
currentLocation.latitude, currentLocation.longitude];
NSString *toLocation = [NSString stringWithFormat: #"%f,%f",
Coordinate.latitude, Coordinate.longitude];
NSString *url = [NSString stringWithFormat:
#"http://maps.google.com/maps?saddr=%#&daddr=%#", fromLocation, fromLocation];
UIWebView *webView = [[UIWebView alloc]init];
NSURL *targetURL = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];

how to send MMS from iPhone app

In my new iOS Project I'd like the end user to be able to MMS text and/or images(from TextField) in a UIButton Action . I've seen similar apps that has this functionality (with text, haven't seen one with images yet).
I have search in google but could not find how to do this, any help much appreciated
This will work fine
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.persistent = YES;
pasteboard.image = [UIImage imageNamed:#"PDF_File.png"];
NSString *phoneToCall = #"sms:";
NSString *phoneToCallEncoded = [phoneToCall stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [[NSURL alloc] initWithString:phoneToCallEncoded];
[[UIApplication sharedApplication] openURL:url];
if([MFMessageComposeViewController canSendText]) {
NSMutableString *emailBody = [[NSMutableString alloc] initWithString:#"Your Email Body"];
picker.messageComposeDelegate = self;
picker.recipients = [NSArray arrayWithObject:#"123456789"];
[picker setBody:emailBody];// your recipient number or self for testing
picker.body = emailBody;
NSLog(#"Picker -- %#",picker.body);
[self presentModalViewController:picker animated:YES];
NSLog(#"SMS fired");
}

Login form for Evernote in ios

i downloaed the evernote sample app from the evernote site for Iphone sdk.i integrated it with my application,i can save note to evernote and import back ,but the only problem is login form missing.the username and password is stored statically in the sample app.i dont know how to authenticate this to dynamically.it is using singlton desing.
in.h
extern NSString * const username;
extern NSString * const password;
#interface evernoteloginpage : UIViewController {
.m
NSString * const username = #"nips55";
NSString * const password = #"annyan555";
#implementation evernoteloginpage
but i need it dynamically,i have two textfiled and a login button i need to add the textfiled values to username and password.i tried a lot but no luck.if anyone knows how to add a loginform for evernote ,please help me.
thanks in advance.
EDIT
now i sucessfully login with the help of this code,but again ther problem is if the login is sucess it will redirect to next page as shown in the code,if the user enter the wrong username or password,ithe app crashes.how to handele the rror mamangement in this code.
-(IBAction)_clickevernotelogin:(id)sender
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Keep this key private
NSString *consumerKey = [[[NSString alloc]
initWithString: #"app" ] autorelease];
[[NSUserDefaults standardUserDefaults] setObject:consumerKey forKey:#"consumerkeyevrnote"];
NSString *consumerSecret = [[[NSString alloc]
initWithString: #"12345678"] autorelease];
// For testing we use the sandbox server.
NSURL *userStoreUri = [[[NSURL alloc]
initWithString: #"https://www.evernote.com/edam/user"] autorelease];
NSString *noteStoreUriBase = [[[NSString alloc]
initWithString: #"https://www.evernote.com/edam/note/"] autorelease];
// These are for test purposes. At some point the user will provide his/her own.
NSString *username = [[[NSString alloc]
initWithString: _txtevernoteUsername.text] autorelease];
NSString *password = [[[NSString alloc]
initWithString: _txtevernotepasswrd.text] autorelease];
[[NSUserDefaults standardUserDefaults] setObject:_txtevernoteUsername.text forKey:#"usernameevernote"];
[[NSUserDefaults standardUserDefaults] setObject:_txtevernotepasswrd.text forKey:#"passwrdevernote"];
THTTPClient *userStoreHttpClient = [[[THTTPClient alloc]
initWithURL:userStoreUri] autorelease];
TBinaryProtocol *userStoreProtocol = [[[TBinaryProtocol alloc]
initWithTransport:userStoreHttpClient] autorelease];
EDAMUserStoreClient *userStore = [[[EDAMUserStoreClient alloc]
initWithProtocol:userStoreProtocol] autorelease];
EDAMNotebook* defaultNotebook = NULL;
BOOL versionOk = [userStore checkVersion:#"Cocoa EDAMTest" :
[EDAMUserStoreConstants EDAM_VERSION_MAJOR] :
[EDAMUserStoreConstants EDAM_VERSION_MINOR]];
if (versionOk == YES)
{
EDAMAuthenticationResult* authResult =
[userStore authenticate:username :password
:consumerKey :consumerSecret];
EDAMUser *user = [authResult user];
NSString *authToken = [authResult authenticationToken];
NSLog(#"Authentication was successful for: %#", [user username]);
NSLog(#"Authentication token: %#", authToken);
NSURL *noteStoreUri = [[[NSURL alloc]
initWithString:[NSString stringWithFormat:#"%#%#",
noteStoreUriBase, [user shardId]] ]autorelease];
[pool drain];
// this is next page if the login sucess
evernotemainpage *detailViewController = [[evernotemainpage alloc] initWithNibName:#"evernotemainpage" bundle:nil];
//detailViewController.firstString = firstString;
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
}
Unfortunately, the Eevernote API currently does not contain any code samples for iOS. If you need information or an example on how to do this check out this article.
http://digitalpericope.net/?p=27

Get Twitter oauth key in similar way to Facebook oauth key

I have an iPhone app which currently loads up a UIWebView using the facebook graph api oauth url (https://graph.facebook.com/oauth/authorize?...) - a user then enters their login details, and on submitting I retrieve the the access token from the URL. (In a manner as this I believe http://www.capturetheconversation.com/technology/iphone-facebook-oauth2-graph-api) This is then passed to my java server which performs the various actions using RestFB
My question is, is there a similar way to do this with Twitter? Currently it seems the only way to get the data is to download a library and use that; yet this is an approach I would rather not use as it would take up unnecessary space seeing as all the commands will actually be run from my Java server.
Any advice would be greatly appreciated
Thanks,
Dan
Twitter uses OAuth 1.0a authentication, but not OAuth 2.0.
Steps to get an access token on Twitter:
Create your own application on Twitter and get consumer key and secret.
Download open source OAuth library, OAuthConsumer in my case.
Write registration code, see example below.
p.s. Set application type as web application, it turns on callback instead of PIN code.
static NSString* kMyApplicationConsumerKey = #"XXXXXXXXXXX";
static NSString* kMyApplicationConsumerSecret = #"XXXXXXXXXXX";
#interface TwitterConnector : NSObject {
OAConsumer* consumer;
OAToken* requestToken;
OAToken* accessToken;
}
- (void)start;
#end
#implementation TwitterConnector
- (void)start {
consumer = [[OAConsumer alloc] initWithKey:kMyApplicationConsumerKey secret:kMyApplicationConsumerSecret];
NSURL* requestTokenUrl = [NSURL URLWithString:#"http://api.twitter.com/oauth/request_token"];
OAMutableURLRequest* requestTokenRequest = [[[OAMutableURLRequest alloc] initWithURL:requestTokenUrl
consumer:consumer
token:nil
realm:nil
signatureProvider:nil] autorelease];
OARequestParameter* callbackParam = [[[OARequestParameter alloc] initWithName:#"oauth_callback" value:#"twitter://authorized"] autorelease];
[requestTokenRequest setHTTPMethod:#"POST"];
[requestTokenRequest setParameters:[NSArray arrayWithObject:callbackParam]];
OADataFetcher* dataFetcher = [[[OADataFetcher alloc] init] autorelease];
[dataFetcher fetchDataWithRequest:requestTokenRequest
delegate:self
didFinishSelector:#selector(didReceiveRequestToken:data:)
didFailSelector:#selector(didFailOAuth:error:)];
}
- (void)didReceiveRequestToken:(OAServiceTicket*)ticket data:(NSData*)data {
NSString* httpBody = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
requestToken = [[OAToken alloc] initWithHTTPResponseBody:httpBody];
NSURL* authorizeUrl = [NSURL URLWithString:#"https://api.twitter.com/oauth/authorize"];
OAMutableURLRequest* authorizeRequest = [[[OAMutableURLRequest alloc] initWithURL:authorizeUrl
consumer:nil
token:nil
realm:nil
signatureProvider:nil] autorelease];
NSString* oauthToken = requestToken.key;
OARequestParameter* oauthTokenParam = [[[OARequestParameter alloc] initWithName:#"oauth_token" value:oauthToken] autorelease];
[authorizeRequest setParameters:[NSArray arrayWithObject:oauthTokenParam]];
UIWebView* webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[[[UIApplication sharedApplication] keyWindow] addSubview:webView];
[webView release];
webView.delegate = self;
[webView loadRequest:authorizeRequest];
}
- (void)didReceiveAccessToken:(OAServiceTicket*)ticket data:(NSData*)data {
NSString* httpBody = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
accessToken = [[OAToken alloc] initWithHTTPResponseBody:httpBody];
// FINISHED!
}
- (void)didFailOAuth:(OAServiceTicket*)ticket error:(NSError*)error {
// ERROR!
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark UIWebViewDelegate
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[[request URL] scheme] isEqualToString:#"twitter"]) {
// Extract oauth_verifier from URL query
NSString* verifier = nil;
NSArray* urlParams = [[[request URL] query] componentsSeparatedByString:#"&"];
for (NSString* param in urlParams) {
NSArray* keyValue = [param componentsSeparatedByString:#"="];
NSString* key = [keyValue objectAtIndex:0];
if ([key isEqualToString:#"oauth_verifier"]) {
verifier = [keyValue objectAtIndex:1];
break;
}
}
if (verifier) {
NSURL* accessTokenUrl = [NSURL URLWithString:#"https://api.twitter.com/oauth/access_token"];
OAMutableURLRequest* accessTokenRequest = [[[OAMutableURLRequest alloc] initWithURL:accessTokenUrl
consumer:consumer
token:requestToken
realm:nil
signatureProvider:nil] autorelease];
OARequestParameter* verifierParam = [[[OARequestParameter alloc] initWithName:#"oauth_verifier" value:verifier] autorelease];
[accessTokenRequest setHTTPMethod:#"POST"];
[accessTokenRequest setParameters:[NSArray arrayWithObject:verifierParam]];
OADataFetcher* dataFetcher = [[[OADataFetcher alloc] init] autorelease];
[dataFetcher fetchDataWithRequest:accessTokenRequest
delegate:self
didFinishSelector:#selector(didReceiveAccessToken:data:)
didFailSelector:#selector(didFailOAuth:error:)];
} else {
// ERROR!
}
[webView removeFromSuperview];
return NO;
}
return YES;
}
- (void)webView:(UIWebView*)webView didFailLoadWithError:(NSError*)error {
// ERROR!
}
#end