Facebook like a post - ios

i am trying to add like button on my application in ios . when i hit url in graph api explorer it works fine, and give me response true. but when i hit this in my code nothing happened
(void)likeFacebookPost:(NSString*)likeID
{
NSString *theWholeUrl = [NSURL URLWithString:[NSString stringWithFormat:
#"%#/%#/likes&access_token=%#",
GRAPH_API_BASEURL,likeID,
[_facebookToken stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
NSURL *facebookUrl = [NSURL URLWithString:theWholeUrl];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:facebookUrl];
[req setHTTPMethod:#"POST"];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&err];
NSString *content = [NSString stringWithUTF8String:[responseData bytes]];
NSLog(#"responseData: %#", content);
}

Related

Overpass API - URL fetching, not working on iPhone, working on mac

I've got this problem last time i did some coding in my project, and i just can't seem to find where the error should be located. When i try the URL in the browser on my mac, everything is working fine - and i get the json file displayed.
My code is as following:
NSURL *url = [NSURL URLWithString:#"http://www.overpass-api.de/api/interpreter?data=[out:json];(way(around:150,49.4873181,8.4710548)[\"maxspeed\"];);out body;>;out skel;"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:25];
[request setHTTPMethod: #"POST"];
NSError *requestError;
NSURLResponse *urlResponse = nil;
NSData *response1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];
NSLog(#"%#", response1);
NSLog(#"%#", requestError);
NSString *myString = [[NSString alloc] initWithData:response1 encoding:NSUTF8StringEncoding];
NSLog(#"myString: %#", myString);
The error that i'm getting is the following:
Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL"
UserInfo=0x1706753c0 {NSLocalizedDescription=unsupported URL,
NSUnderlyingError=0x17044f480 "unsupported URL"}
Bests, Jakob
Adjusted your code and this now seems to work. Basically split end of URL into the body of the POST.
// Split URL from Body
NSURL *url = [NSURL URLWithString:#"http://www.overpass-api.de/api/interpreter"];
NSString *body=#"?data=[out:json];(way(around:150,49.4873181,8.4710548)[\"maxspeed\"];);out body;>;out skel;";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:25];
// Add body to the request
[request setHTTPMethod: #"POST"];
[request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
NSError *requestError;
NSURLResponse *urlResponse = nil;
NSData *response1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];
NSLog(#"%#", response1);
NSLog(#"%#", requestError);
NSString *myString = [[NSString alloc] initWithData:response1 encoding:NSUTF8StringEncoding];
NSLog(#"myString: %#", myString);

Unable to get photo_reference from place details api

I am using google places api to get that place's photo but i am unable to locate "photo_reference" field in the returned JSON. Here is what i have done so far.
NSString *urlString = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/details/json?placeid=%#&key=%#",placeId,appKey];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"GET"];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue new] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (!connectionError) {
NSLog(#"Got Place Details");
NSDictionary *details = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSString *photo_reference = [[[[details valueForKey:#"result"] valueForKey:#"photos"] objectAtIndex:0] valueForKey:#"photo_reference"];
NSLog(#"");
NSString *urlString = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/photo?maxwidth=302&maxheight=275&photoreference=%#&key=%#",photo_reference,appKey];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"GET"];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue new] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(#"Inner Request");
if (!connectionError) {
}];
}
}];
Places API documentation says that photo_reference could either be found from "Place Search" or "Place Details" requests. I got it from none.
I requested place details using place_id but the json doesnt have any "photos" key.
Same issue here
https://stackoverflow.com/questions/28317739/places-api-dont-retrieve-photos-on-detailed-petition-since-yesterday
Something change since yesterday but I don't know what or why.
EDIT: Today it's working again, maybe some random error from google.

Send POST using NSURLConnection with HTTPS

I have been searching over the internet about the right configuration to send a POST to my server using HTTPS, some web pages say i need NSURLCredential and others say:
Just use a #"https://www.myhttpsite.com/" URL and it should work the same way as normal HTTP urls.
So, how is the right way? i need to send credentials of users from my iOS app to my server to authenticate them, so i need to protect this credentials with the HTTPS.
My server already works fine with the HTTPS using internet browsers.
So far what i have is this:
NSString *user = #"user";
NSString *pass = #"pass";
NSString *postData = [NSString stringWithFormat:#"user=%#&pass=%#", user, pass];
NSURL *url = [NSURL URLWithString:#"https://myserver.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = #"POST";
request.HTTPBody = [postData dataUsingEncoding:NSUTF8StringEncoding];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSString *strData = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"data %#", strData);
}];
Found this to be a good example, why don't you try it out?
NSString *urlstring =[[NSString alloc] initWithFormat:#"userName=%#&password=%#",userName.text,password.text];
NSURL *url=[NSURL URLWithString:#"https://www.example.com/mobile/Login.php"];
NSData *postData = [urlstring dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
NSError *error;
NSURLResponse *resp;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&resp error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];

Can I get URL through NSURLResponse?

I have one url and using the viglink api i created other one, now using viglink api I want to get the URL, if i am getting the response.
NSString *vigLinkApiKey = #"somekey";
NSString *url = self.textfieldProductURL.stringValue;
NSString *affiliatedUrl = [NSString stringWithFormat:#"http://api.viglink.com/api/click?key=%#&out=%#&loc=http://amazon.com[&cuid=%#][&reaf=0][&ref=http://amazon.com]", vigLinkApiKey, url, [PFUser currentUser].username];
NSLog(#"Product URL: %#", url);
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:affiliatedUrl] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
NSData *urlData;
NSURLResponse *response;
NSError *error;
// Make synchronous request
urlData = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
NSString *str = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"str:%#",str);
Now, at last i want to get the URL when i am getting response. Is it possible. How can can i get the URL?
Please send the answer.
Thanks.
NSURLResponse has a method - URL use it to get URL.
NSURL *url = [response URL];
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURLResponse_Class/Reference/Reference.html#//apple_ref/occ/instm/NSURLResponse/URL

GET Request uses too many parameters- iOS

I'm trying to load a URL in NSMutableURLRequest for a GET request as below:
NSString *serverAddress = [NSString stringWithFormat:#"http://test.us.to:8080/api/offers?altId=%#&provider=%#&otherId=%#&tenantId=%#&createdAt=%#&otherOfferId=%#&postId=%#&pageId=%#&sourceUrl=%#&name=%#&description=%#&text=%#&category=%#&caption=%#&startTime=%#&expirationTime=%#&minPurchase=%#&numPurchases=%#&value=%#&percent=%#&count=%#&currency=%#&terms=%#&campaignId=%#&partnerId=%#&tenantIdAtPartner=%#&issuerName=%#&claimLimit=%#&onePerUser=%#&emailTemplateFile=%#",#"test",#"Facebook",#"",#"test",#"null",#"test",#"",#"",#"http://test.us.to/offers/harvester_summer13.html",#"Harvester",#"Harvester 2for1 TakeAway",#"Enjoy!",#"voucher",#"Harvester 2for1 TakeAway",[NSNumber numberWithInt:0],#"test",[NSNumber numberWithInt:0],[NSNumber numberWithInt:0],#"1000",[NSNumber numberWithInt:0],[NSNumber numberWithInt:0],#"GBP",#"",[NSNumber numberWithInt:1259],#"null" ,#"null" ,#"null",[NSNumber numberWithInt:100],[NSNumber numberWithBool:false],#"templates/test.vm"];
NSURL *url = [NSURL URLWithString:[serverAddress urlEncodeUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadRevalidatingCacheData timeoutInterval:15.0];
NSString *authStr = [NSString stringWithFormat:#"%#:%#", #"testuser", #"testpwd"];
NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *authValue = [NSString stringWithFormat:#"Basic %#", [authData base64EncodedStringWithOptions:0]];
[request setValue:authValue forHTTPHeaderField:#"Authorization"];
[request setHTTPMethod: #"GET"];
NSError *requestError;
NSURLResponse *urlResponse = nil;
NSData *response1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];
NSLog(#"output data %#",response1);
I guess I'm loading way too many parameters using the url string. Is there a better way to pass parameters for a GET request ? The current output is null for response1
It looks to me like the issue is that you are encoding the whole url with some sort of encoding method, but you really just need to encode each parameter separately if it contains certain characters. For example:
NSString *serverAddress = #"http://test.us.to:8080/api/offers";
NSString *altIdParameter = [#"Escape?This?String?" urlEncodeUsingEncoding:NSUTF8StringEncoding];
NSString *aUrlParameter = [#"http://test.us.to/offers/harvester_summer13.html" urlEncodeUsingEncoding:NSUTF8StringEncoding];
NSString *getRequestUrl = [NSString stringWithFormat:%#?altId=%#&urlParam=%#", serverAddress, altIdParameter, aUrlParameter];
NSURL *url = [NSURL URLWithString:getRequestUrl];

Resources