Twitter iOS GET statuses/home_timeline using Parse - ios

This will be my first iPhone app and I am running into difficulties that I thought I'd be able to find a tutorial on, but alas.... nothing on the inter webs that I can find.
I'm trying to get an initial 20 or so tweets from a signed in user (signed in using Parse's Twitter authentication) and I can't seem to get it to work. I put a breakpoint in when I get an error and this is what it shows:
I'm contemplating abandoning Parse all together and doing my own authentication using https://github.com/nst/STTwitter but wanted to quickly see if there was a way to simply do what I am trying to. The code is question is at this github: https://github.com/johndangerstorey/twiz and outlined below as found in my MyLoginViewController.m file:
NSString *bodyString = [NSString stringWithFormat:#"https://api.twitter.com/1.1/statuses/home_timeline.json?screen_name=johnDANGRstorey"];
// Explicitly percent-escape the '!' character.
bodyString = [bodyString stringByReplacingOccurrencesOfString:#"!" withString:#"%21"];
NSURL *url = [NSURL URLWithString:bodyString];
NSMutableURLRequest *tweetRequest = [NSMutableURLRequest requestWithURL:url];
tweetRequest.HTTPMethod = #"GET";
tweetRequest.HTTPBody = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
[[PFTwitterUtils twitter] signRequest:tweetRequest];
NSURLResponse *response = nil;
NSError *error = nil;
// Post status synchronously.
NSData *data = [NSURLConnection sendSynchronousRequest:tweetRequest
returningResponse:&response
error:&error];
// Handle response.
if (!error) {
NSLog(#"Response: %#", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
} else {
NSLog(#"Error: %#", error);
}
Thanks for your reply and help.

I figured it out, GET requests don't require .body or .method requests so I just removed
tweetRequest.HTTPMethod = #"GET";
tweetRequest.HTTPBody = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
and was golden.

Related

Validating App store receipt gives DrmInvalidArgumentException

I'm following the documentation here.
https://developer.apple.com/library/content/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html
I have a receipt-data from my team that I'm trying to validate, they're getting error code 21002 which was malformed JSON. It looks like they had extra parameters appended to the base64 data, so I tried removing those and sending:
- (void)viewDidLoad {
[super viewDidLoad];
NSData *receipt; // Sent to the server by the device
// Create the JSON object that describes the request
NSError *error;
NSDictionary *requestContents = #{
#"receipt-data": #"<<$mybase64data>>", #"password" : #"<<$thepassword>>"};
NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents options:0 error:&error];
if (!requestData) { /* ... Handle error ... */ }
// Create a POST request with the receipt data.
NSURL *storeURL = [NSURL URLWithString:#"https://buy.itunes.apple.com/verifyReceipt"];
NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:storeURL];
[storeRequest setHTTPMethod:#"POST"];
[storeRequest setHTTPBody:requestData];
// Make a connection to the iTunes Store on a background queue.
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:storeRequest queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError) {
/* ... Handle error ... */
NSLog(#"conerror %#", connectionError);
} else {
NSError *error;
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSLog(#"hello %#", jsonResponse);
NSLog(#"error %#", error);
if (!jsonResponse) {
}
}
}];
}
result:
2017-03-03 22:45:47.454 receipttest[89851:352604] hello {
exception = "com.apple.jingle.mzfairplay.validators.DrmInvalidArgumentException";
status = 21002;
}
2017-03-03 22:45:47.455 receipttest[89851:352604] error (null)
Something to keep in mind: In this example the data is sent to Apple straight from the App, but you might do this from a server too. When you testing your server App don't use NSLog() to print your base64 data, NSLog truncates data.
I had this issue when using a receipt from a test user that was a couple days old with a yearly auto-renewable subscription.
I checked the above helpful responses about extra characters etc (I also checked I had supplied my app secret for auto-renewable) with no joy.
In the end I tried creating A NEW SANDBOX user and it worked first time
with no other changes other than the new Receipt!
Hope this helps someone.
I also received the same error response for serveral receipts. The solution for me was to remove all occurrences of \r\n.
Maybe you have the same issue. I still haven't figured out when and why these chars are inserted.
Pay attention to URLENCODE:
- (NSString *)URLEncodedString
{
// CharactersToBeEscaped = #":/?&=;+!##$()~',*";
// CharactersToLeaveUnescaped = #"[].";
NSString *unencodedString = self;
NSString *encodedString = (NSString *)
CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)unencodedString,
NULL,
(CFStringRef)#"!*'();:#&=+$,/?%#[]",
kCFStringEncodingUTF8));
return encodedString;
}

iOS: why i need to use antire mixpanel sdk to just tracking the event?

I integrated the mixpanel iOS sdk with my ios app. It's working fine
but my question is this, for just track the events why i need the
storyboard and assets and all?. Does any one knows here what are all
the necessary files that is enough to track the events?. The thing is, why i need to keep unused files in my project?.
Any help that might be really appreciated.
I've created a cut down version of MixPanel for iOS that uses the MixPanel spec.
You can download it, and a demo app from my github page:
https://github.com/peterept/mixpanellite
The crux of the code is:
+(void)track:(NSString*)event properties:(NSDictionary*)properties {
NSMutableDictionary *allProperties = [NSMutableDictionary dictionaryWithObjectsAndKeys:[mixpanellite distinctIdentifier],#"distinct_id", MIXPANELLITE_TOKEN, #"token", nil];
if (properties != nil) {
[allProperties addEntriesFromDictionary:properties];
}
NSMutableDictionary *eventDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:event,#"event", allProperties, #"properties", nil];
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:eventDict options:0 error:&error];
if (error == nil)
{
NSLog(#"[MIXPANELLITE] %#", [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);
NSString *urlTemplate = #"http://api.mixpanel.com/track/?data=%#";
NSString *url = [NSString stringWithFormat:urlTemplate, [jsonData mp_base64EncodedString]];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:nil];
}
}
The only dependency is Matt Gallagher's excellent NSData Base64 category.

Google Contacts API version 3.0 to retrieve all contacts for objective C (iOS) sample request format?

In my application user logs in throgh google+ credentials for which I have implemented google+ framework for authentication/authorization. After Successful login I have to get the contacts list of the logged in user.
for which I am using Google Contacts API version 3.0 url https://www.google.com/m8/feeds/contacts/default/full
After GPP Sign In authentication request..
- (void)finishedWithAuth:(GTMOAuth2Authentication *)auth
error:(NSError *)error {
if (error) {
NSLog(#"%#",[NSString stringWithFormat:#"Status: Authentication error: %#", error]);
return;
}
auth.clientID =#"XXXX.apps.googleusercontent.com";
auth.clientSecret =#"xxxxxxxx-z7tmOqQCShgH3ax";
NSString *urlStr = #"https://www.google.com/m8/feeds/contacts/default/full";
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"GET"];
[request setValue:#"3.0" forHTTPHeaderField:#"GData-Version"];
auth.scope= #"https://www.googleapis.com/auth/contacts.readonly";
[auth authorizeRequest:request
completionHandler:^(NSError *error) {
NSString *output = nil;
if (error) {
output = [error description];
} else {
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
if (data) {
// API fetch succeeded :Here I am getti
output = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(#"%#", output);
} else {
// fetch failed
output = [error description];
}
}
}];
}
in the successful response I have written code like this.
But the output is not as desired.
The output string is huge junk value and at the end of it is showing
401. That's an error. There was an
error in your request. That's all we
know.
Could any one please help in resolving the issue.
401 means that you have not correctly authorized. I don't work with that api but it's possible your credentials are incorrect.
you might also be seeing certificate problems for the https? you should set up this call back and see if it provides more info.
How to use NSURLConnection to connect with SSL for an untrusted cert?

message:(#200) App does not have permission to make this call in ios application using facebook sdk

I want to like and comment in my iOS application.But the problem is whenever I call my API the response comes in the form of this `error:message:(#200)'. App does not have permission to make this call and sometimes it will give input parameter is required.The permissions i give are 'basic_info','email','user_likes' and 'read_stream'. I am passing object id and access token in my like and comment API.
Like API code:
NSString *theWholeUrl = [NSString stringWithFormat:#"https://graph.facebook.com/%#/likes?access_token=%#",self.friendId,[FBSession activeSession].accessTokenData.accessToken];
NSLog(#"Value of theWholeUrl%#",theWholeUrl);
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]];
You need the publish_actions permission to be able to like and comment on content programmatically via the API.

How to parse JSONP in Objective-C?

I am retrieving JSON information for an API and it says on the API that it is in JSON but I noticed it is in JSONP or "json with padding" as some call it. I tired to look everywhere to find how to parse this but no luck. The information I am trying to receive is this:
({"book":[{"book_name":"James","book_nr":"59","chapter_nr":"3","chapter":
{"16":{"verse_nr":"16","verse":"For where envying and strife is, there is confusion and
every evil work."}}}],"direction":"LTR","type":"verse"});
The link to the data is https://getbible.net/json?p=James3:16, so you can look at it directly.
This is the code I am using to try to retrieve the JSON Data and parse it into a NSMutableDictionary.
-(void)fetchJson {
NSString *currentURL = [NSString stringWithFormat:#"https://getbible.net/json?p=James"];
NSURL *url = [NSURL URLWithString:currentURL];
NSData *data = [[NSData alloc]initWithContentsOfURL:url];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
NSMutableData *receivedData = [[NSMutableData alloc] initWithLength:0];
NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
[receivedData setLength:0];
NSURLResponse *response = [[NSURLResponse alloc] initWithURL:url MIMEType:#".json" expectedContentLength:-1 textEncodingName:nil];
expectedTotalSize = [response expectedContentLength];
if ([data length] !=0) {
NSLog(#"appendingData");
[receivedData appendData:data];
if(connection){
NSLog(#"Succeeded! Received %lu bytes of data",(unsigned long)[receivedData length]);
}
NSError *error;
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
if(jsonResponse){
NSArray *responseArr = [jsonResponse mutableCopy];
NSLog(#"%lu",(unsigned long)[responseArr count]);
}else if (!jsonResponse){
//do internet connection error response
}
}
}
The results I am getting back from putting a breakpoint in the code is:
jsonResponse returns NULL
NSError NSCocoaErrorDomain code - 3840
but my NSData *data is returning 15640 bytes.
My console is displaying this from the NSLogs I used for debugging:
2014-04-20 01:27:31.877 appendingData
2014-04-20 01:27:31.879 Succeeded! Received 15640 bytes of data
I am receiving the data correctly but I am not parsing it correctly I know the error is because the JSON is in JSONP format. If anyone could please help with this I would appreciate it so much. I have tired to give as much detail on this question as I can but if you need more information just let me know so I can add it and make this as clear as possible.
Your code has at least two separate attempts to download the data. Neither is really correct. The code also only works with JSON, not JSONP.
Try this:
NSURL *url = [NSURL URLWithString:#"https://getbible.net/json?p=James"];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (data) {
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSRange range = [jsonString rangeOfString:#"("];
range.location++;
range.length = [jsonString length] - range.location - 2; // removes parens and trailing semicolon
jsonString = [jsonString substringWithRange:range];
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *jsonError = nil;
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&jsonError];
if (jsonResponse) {
// process jsonResponse as needed
} else {
NSLog(#"Unable to parse JSON data: %#", jsonError);
}
} else {
NSLog(#"Error loading data: %#", error);
}
}];
One problem is that the data you're downloading has extraneous information at the beginning and end. The JSON being delivered by your URL is:
({"book":[{"book_name":"James","book_nr":"59","chapter_nr":"3","chapter":{"16":{"verse_nr":"16","verse":"For where envying and strife is, there is confusion and every evil work."}}}],"direction":"LTR","type":"verse"});
As the error message you're seeing indicates: you need to remove the initial ( from the beginning of the string and the ); from the end so that your JSON will start with the dictionary that your code expects. You can do this by calling subdataWithRange: on your NSData object:
NSData* jsonData = [data subdataWithRange:NSMakeRange(1, data.length-3)];
NSDictionary* jsonResponse = [NSJSONSerialization JSONObjectWithData:jsonData
options:0
error:&error];
Just to update everyone, the NSURLRequest has been deprecated in iOS9. I tried the answer by #rmaddy, and I didn't receive anything either (just like what #lostAtSeaJoshua was encountering I guess). I have updated rmaddy's answer to reflect the NSURLSession implementation that has (I think) replaced NSURLRequest:
NSURLSession *session = [NSURLSession sharedSession];
NSURL *url = [NSURL URLWithString:#"http://somerandomwebsite.com/get.php?anotherRandomParameter=5"];
[[session dataTaskWithURL:url
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
// handle response
if (data) {
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"stringJSONed: %#",jsonString);
//Do something with the received jsonString, just like in # rmaddy's reply
} else {
NSLog(#"Error loading data: %#", error);
}
}] resume];
Just a heads up notice, when I first ran it, it gave me the security error. What you need to do (if you are using http) is to add this to your plist:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
I have to mention that after the NSAllowArbitraryLoads key, there are most probably other keys and values, such as NSExceptionDomain. But they're not really relevant to this answer I think. If you need to look further, let me know and I will dig deeper :)

Resources