I've followed this tutorial - https://www.youtube.com/watch?v=AdV7bCWuDYg
Trying to draw route on map and I do successfully send latitude/longitude of origin/destination. The problem is its generated URL.
I keep getting this
http://maps.googleapis.com/maps/api/directions/json?&origin=37.661519,127.061106&destination=37.664185,127.062994&sensor=false
But as far as I know, the URL should end with |45.566346,18.667512 something like that. I don't know what I'm doing wrong because the tutorial claims it works and its by Google I think.
- (void)setDirectionsQuery:(NSDictionary *)query withSelector:(SEL)selector
withDelegate:(id)delegate{
NSArray *waypoints = [query objectForKey:#"waypoints"];
NSLog(#"***waypoints : %#", waypoints);
NSString *origin = [waypoints objectAtIndex:0];
int waypointCount = [waypoints count];
int destinationPos = waypointCount -1;
NSString *destination = [waypoints objectAtIndex:destinationPos];
NSString *sensor = [query objectForKey:#"sensor"];
NSMutableString *url =
[NSMutableString stringWithFormat:#"%#&origin=%#&destination=%#&sensor=%#",
kMDDirectionsURL,origin,destination, sensor];
if(waypointCount>2) {
[url appendString:#"&waypoints=optimize:true"];
int wpCount = waypointCount-2;
for(int i=1;i<wpCount;i++){
[url appendString: #"|"];
[url appendString:[waypoints objectAtIndex:i]];
}
}
url = [[url
stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding]mutableCopy];
_directionsURL = [NSURL URLWithString:url];
NSLog(#"url : %#", url);
[self retrieveDirections:selector withDelegate:delegate];}
After hours of research I've noticed on Google Directions page that the required URL format is a little different from the one I've been using but still no luck.
[NSMutableString stringWithFormat:#"%#origin=%#&destination=%#&key=[my api key],
kMDDirectionsURL,origin,destination];
Your url should work, I changed the origin to San Francisco and destination to Mountain View and it works:
http://maps.googleapis.com/maps/api/directions/json?&origin=san+francisco&destination=mountain+view&sensor=false
It is possible that Google Maps does not know how to get from 37.661519,127.061106 to 37.664185,127.062994
Related
for(int i=0; i<arrString.count;i++){
if([[arrString objectAtIndex:i] rangeOfString:#"www."].location != NSNotFound)
link = [arrString objectAtIndex:i];
}
NSString *url = (#"THIS IS WHERE HTTP:// would Be!%#", link);
NSLog(#"THIS IS WHERE HTTP:// would Be!%#", url);
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[_webView1 loadRequest:nsrequest];
When I use NSLog:
NSLog(#"THIS IS WHERE HTTP:// would Be!, url);
It logs the correct URL that begins with http://. However, when I set the string URL equal to http://"link", it returns nothing. Any help is much appreciated.
(Stack Overflow is not letting me put the real HTTP:// followed by an %#.)
It looks like you may need to use stringWithFormat:
NSString *domainName = #"aol.com";
NSString *url = [NSString stringWithFormat:#"http://www.%#", domainName];
I got a website where I make playlists with SoundCloud. Now I want to make an app for iPhone so the users can listen to the songs there as well.
http://developers.soundcloud.com/docs/api/ios-quickstart
In the example in the link the users have to sign in to listen and share, but I want my users only to listen. Is there a way around so they don't have to sign in?
Create a page that outputs the playlist in JSON, then in xcode create a class that downloads the JSON for the track dictionaries and play the downloaded content using AVPlayer (or AVQueuePlayer if you're playing the whole list).
Here's some abstract code:
playlistDownloader.m
- (void)downloadPlaylist{
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_sync(concurrentQueue, ^{
NSURL *url = [NSURL URLWithString:#"http://www.yourwebsite.com/playlist.json?id=1"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error;
id trackData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
if (!error) {
tempTrackArray = trackData;
} else {
NSLog(#"Playlist wasn't able to download");
}
});
}
tempTrackArray would be a property declared in the class.
Then in your player, you'd do something like this:
audioPlayer.m
- (void)instanciateAudioPlayer
{
NSDictionary *trackDictionary = [playListDownloader.tempTrackArray objectAtIndex:0];
NSString *urlString = [trackDictionary objectForKey:#"stream_url"];
AVAsset *asset = [AVAsset assetWithURL:streamURL];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
[avPlayer initWithPlayerItem:playerItem];
}
This is some really rough code but its the general gist of what you're trying to do. Should get you going in the right direction.
Trying the soundcloud quickstart I realised that your stream url needs to begin with https rather than http. Also you need to add your client id from your sound cloud app to the stream url:
NSDictionary *trackDictionary = [playListDownloader.tempTrackArray objectAtIndex:0];
NSString *streamURL = [trackDictionary objectForKey:#"stream_url"];
streamURL = [streamURL stringByReplacingOccurrencesOfString:#"http" withString:#"https"];
NSString *urlString = [NSString stringWithFormat:#"%#?client_id=%#", streamURL, #"a8e117d3fa2121067e0b29105b0543ef"];
From there you just setup the AVPlayer:
self._avPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:urlString]];
[self setupLayer:clayer];
[self._avPlayer play];
Everything should work fine. Hope it helps
I'm having some trouble pulling a Facebook access token from the web for a Facebook Feed App I'm writing. The problem isn't strictly related to gaining a Facebook token; this just frames the problem. When I go to https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=[APP_ID]&client_secret=[APP_SECRET], I am returned a token on a page that simply says:
access_token=464483653570261|cY9NHFBWCDJ9hSQfswWFg0FDZvw
How can I parse that from the webpage into my app? I'm relatively new to Objective C (and I've only got a year of basic coding experience), so I tried to use part of a method that I found online to get a JSON feed, combined with a simple parsing method, but it didn't work. The code is as follows:
id getToken = [self objectWithUrl:[NSURL URLWithString:#"https://graph.facebook.com/
oauth/access_token?grant_type=client_credentials&
client_id=464483653570261&
client_secret=55bb8395ed0293bf37af695f6cdaa1fb"]];
NSString *fullToken = (NSString *)getToken;
NSLog(#"fullToken: %#", fullToken);
NSArray *components = [fullToken componentsSeparatedByString:#"="];
NSString *token = [components objectAtIndex:1];
NSLog(#"token: %#", token);
Both of my NSLogs say that the respective Strings point to (null). I'm not really certain what I'm doing wrong, and I haven't had much luck finding answers on the internet, as I'm not sure what to call what I'm trying to do. I'd appreciate any help, or alternate methods, that you might have.
By the look of it, the value you're getting isn't JSON, it's just a string.
Try something like this:
NSURL * url = [NSURL URLWithString:#"https://graph.facebook.com/
oauth/access_token?grant_type=client_credentials&
client_id=464483653570261&
client_secret=55bb8395ed0293bf37af695f6cdaa1fb"]];
NSString * fullToken = [NSString stringWithContentsOfUrl: url];
NSLog(#"fullToken: %#", fullToken);
NSArray *components = [fullToken componentsSeparatedByString:#"="];
NSString *token = [components objectAtIndex:1];
NSLog(#"token: %#", token);
There is a more simple way of getting User's access_token using the ACAccountStore & ACAccountType. Check the Full Code Below:
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSDictionary *FBOptions = [NSDictionary dictionaryWithObjectsAndKeys:FACEBOOK_APP_ID, ACFacebookAppIdKey,#[#"email"],ACFacebookPermissionsKey, nil];
[accountStore requestAccessToAccountsWithType:accountType options:FBOptions completion:
^(BOOL granted, NSError *error) {
if (granted) {
NSArray *facebookAccounts = [accountStore accountsWithAccountType:accountType];
FBAccount = [facebookAccounts firstObject];
NSLog(#"token :%#",[[FBAccount credential] oauthToken]);
} else {
NSLog(#"error getting permission %#",error);
if([error code]== ACErrorAccountNotFound){
NSLog(#"Account not found. Please setup your account in settings app");
}
else {
NSLog(#"Account access denied");
}
}
}];
I'm using objectiveflickr to pull a list of images from a flickr.photos.search query.
I have it working where I pull in the thumbnail and the large size image, however sometimes an image doesn't have a large size available and so in the app it displays flickr's image unavailable image. I checked what is returned through the flickr API and the URL's are regular flickr URL's, so there's no way to tell if an image will or will not have a large image that way.
The only way I can think of doing this is to create another objective flickr delegate and have it do a flickr.photos.getSizes API call and check against each photo.
Anyone have any other suggestions?
Here's my code:
- (void)flickrAPIRequest:(OFFlickrAPIRequest *)inRequest didCompleteWithResponse:(NSDictionary *)inResponseDictionary
{
int requestedPhotoTotal = [[[inResponseDictionary objectForKey:#"photos"] valueForKey:#"perpage"] intValue];
for (int i=0; i < requestedPhotoTotal; i++)
{
NSDictionary *photoDict = [[inResponseDictionary valueForKeyPath:#"photos.photo"] objectAtIndex:i];
NSString *title = [photoDict valueForKey:#"title"];
NSURL *photoURL = [context photoSourceURLFromDictionary:photoDict size:OFFlickrLargeSize];
NSURL *thumbURL = [context photoSourceURLFromDictionary:photoDict size:OFFlickrThumbnailSize];
NSURL *photoSourcePage = [context photoWebPageURLFromDictionary:photoDict];
Photo *p = [[PhotoStore sharedStore] createPhoto];
[p setURL:photoURL];
[p setThumbURL:thumbURL];
[p setSource:photoSourcePage];
[p setTitle:title];
NSLog(#"%# - %#", title, [photoURL absoluteString]);
}
[self showGallery];
}
I have a simple question but I can't find the right answer. I have a song url saved to my database in a path like this.
aSound.path = [[item valueForProperty: MPMediaItemPropertyAssetURL] absoluteString];
How to convert back to a MPMediaItem object which has songname artist and artwork?
Is it possible?
Saving:
NSNumber* persistentID =
[mediaItem valueForProperty:MPMediaItemPropertyPersistentID];
Loading:
MPMediaPropertyPredicate * predicate =
[MPMediaPropertyPredicate
predicateWithValue:persistentID
forProperty:MPMediaItemPropertyPersistentID];
Another example:
NSNumber for MPMediaItemPropertyPersistentID to NSString and back again
Note song URLs are are unreliable because any DRM song returns a null url
Alternatively, a nasty hack that Works For Me (tm).
- (MPMediaItem *)getMediaItemForURL:(NSURL *)url {
// We're going to assume that the last query value in the URL is the media item's persistent ID and query off that.
NSString *queryString = [url query];
if (queryString == nil) // shouldn't happen
return nil;
NSArray *components = [queryString componentsSeparatedByString:#"="];
if ([components count] < 2) // also shouldn't happen
return nil;
id trackId = [components objectAtIndex:1];
MPMediaQuery *query = [[MPMediaQuery alloc] init];
[query addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:trackId forProperty:MPMediaItemPropertyPersistentID]];
NSArray *items = [query items];
if ([items count] < 1) // still shouldn't happen
return nil;
return [items objectAtIndex:0];
}
Comments appreciated for where this might go wrong or how to improve it. I prefer to pass the URL around instead of the PersistentID as I have multiple media sources, all of which can use URLs. Using the PersistentID would mean a lot of "if this is a media ID, do this, otherwise, do that with the URL".