MGTwitterEngine + OAuth Load Users Tweets - ios

I am using MGTwitterEngine + OAuth in my project. I would like to load all tweets from the currently logged in user into a NSMutableArray, however I am having difficulty doing so.
Upon successful login I call the following method:
[_engine getUserTimelineFor:_engine.username sinceID:0 startingAtPage:0 count:20];
And then in the delegate method I have the following:
- (void) statusesReceived:(NSArray *)statuses
forRequest:(NSString *) connectionIdentifier
{
for ( NSDictionary *dict in statuses )
{
NSLog(#"%#", [dict objectForKey:#"id" ]);
// tweets is a NSMutableArray previously allocated and initialised
[tweets addObject:[dict objectForKey:#"id" ]];
}
[_engine getUserTimelineFor:_engine.username sinceID:0 startingAtPage:0 count:20];
}
As you can see I am attempting to batch load the current users tweets, 20 at a time.
I believe the issue lies in the fact, I am not incrementing the page parameter. My issue is, I don't understand what a page actually represents, or how to get the total number of pages for any given user.
How can I load all of a users tweets in a simple, and efficient manner?

If you are using tableView to display tweet to user, you can use "Facebook's way". You can show first 20 tweets in the table view and when iPhone user scrolls down and and try to view last tweet (if you are rendering last row of your table view) then send request to get next 20 tweets and so on. So that you don't need to show page numbers and older tweets will get loaded when user wants to view them.
I hope this helps you.

Related

Create Pin PDKResponseObject response not consistent with documentation

We are creating a pin in a specific Board by using the method
createPinWithImageđź”—onBaord:description:progress:withSuccess:andFailure:
We read in the documentation (here: https://developers.pinterest.com/docs/api/overview/ and here: https://github.com/pinterest/ios-pdk/blob/master/Pod/Classes/PDKClient.h#L417) that this method should return a PDKResponseObject *responseObject with the ID, URL, clickthrough URL and description of the created Pin.
We have been creative enough to try to access the ID of the Pin and its URL using any possible key (#"id", #"identifier", #"url", #"NSUrl") but the values returned are always nil. In fact the PDKResponseObject returns only 2 keys: Board ID and Pin Description.
What should we do to access the ID or, at the very least, the URL of the newly created Pin?
Does anybody have the same issue?
Despite multiple attempts and after having tried to discuss this issue with the Pinterest development Team, this still remains.
Testing a solution becomes also extremely difficult considering the new limitation Pinterest has imposed on not approved apps (which include all apps under development by definition).
For now, I only found a way around by calling a new request to get all pins in a specific board and get the first in the resulting array (which is the last posted):
//Create pin in Pinterest
[[PDKClient sharedInstance]createPinWithImage:image link:urlToShare
onBoard:reference description:message progress:nil
withSuccess:^(PDKResponseObject *responseObjectCreation) {
//Previous block does not return pin id so a new call is required
[[PDKClient sharedInstance]getBoardPins:reference fields:[NSSet
setWithArray:#[#"link"]] withSuccess:^(PDKResponseObject
*responseObject) {
//Get id of last pin
NSArray *pinIDs = [[NSArray arrayWithArray:[responseObject
pins]]valueForKey:#"identifier"];
NSString *postId = [pinIDs objectAtIndex:0];
}];
}];
By the way, the right key for the pin ID is "identifier" and not "id" or "ID" as said in the API documentation. Just found out by trying multiple times and checking the Pinterest Example app in GitHub.
Hope this helps other people who are fighting the same problem.

Getting all the user's friends that are using the app

I know that by the title this question has been asked a lot, but with the new Facebook 4.0 sdk this is a problem.
I know that if i call
[self FCMSendOpenGraphRequestWithPath:#"me/friends" andCompletionHandler:^(BOOL seccess, id result, NSError *error) {
if (seccess) {
}
}else{
}
}];
I will get all the user's friends that are using my app and that fit into one paging page (i have noticed that one page can hold 25 friends).
My problem is what happens when i need to get the page 2,3,4..etc'?
I know that if i call the graph call with the 'next' field i will get the next page. But because the Facebook sdk uses only completionHandlers using recursion won't work, and i don't want to hard code all the method call obviously.
All advices will help,
Thanks

SPPlaylist items as SPTracks not loading (consistently)

I've been struggling with this for a while now and was looking around for some advice from anyone who's worked with CocoaLibSpotify on iOS (iOS 7 to be exact).
I'm trying to load all the (SPTrack) items in a SPPlaylist. I've looked though the example code and the documentation so I know the best way to observe things is with KVO, however I can't see how that's the best way here.
Looking at the "Guess the Into" example and other sources on GitHub and the web in general, the precess I'm doing is as follows:
Get the SPPlaylist (this is either directly from a the users playlists or created from a saved URL),
Load the playlist with SPAsyncLoading,
Loop though the SPPlaylistItems returned in the SPPlaylist and get all the SPTrack objects,
Pass the array of SPTrack objects into another SPAsyncLoading call,
Display all items from the loadedItems array.
Step 5 is where things seem to go wrong. Sometimes it is fine and I get all the tracks in the playlist displaying. However most of the time it doesn't load them, or only load a few (normally very little).
I've noticed that restarting the phone can sometimes make it work. Though after running the app (or changing the playlist) a couple of time, it stops working again.
When I log each step I get something like the following:
-[ViewController sessionDidLoginSuccessfully:]
Load playlist with URL: spotify:user:XXXXX:playlist:XXXXXXXXXXXXXXXXXXXXXX
Created playlist: <SPPlaylist: 0x16775bc0>: (null) (0 items)
Loaded playlist: <SPPlaylist: 0x16775bc0>: Playlist Name (59 items)
Created SPTrack items: 59
Loaded SPTrack items: 1
Failed to load SPTrack items: 58
As you can see, the user is logged on, with a valid session. Each step works according to plan until the end. Sometimes even if it does load a track, it won't allow me to play it anyway as well returning SP_TRACK_AVAILABILITY_UNAVAILABLE.
I've also added a code snippet below if it helps. But any help would be grateful at this point with this.
Thanks.
- (void)loadPlaylist
{
__weak NSURL *spotifyURL = <URL from saved SPPlaylist>;
NSLog(#"Load playlist with URL: %#", spotifyURL);
[SPPlaylist playlistWithPlaylistURL:spotifyURL inSession:[SPSession sharedSession] callback:^(SPPlaylist *playlist) {
_spotifyPlaylist = playlist;
NSLog(#"Created playlist: %#", _spotifyPlaylist);
[SPAsyncLoading waitUntilLoaded: _spotifyPlaylist timeout:kSpotifyTimeout then:^(NSArray *loadedItems, NSArray *notLoadedItems) {
NSLog(#"Loaded playlist: %#", _spotifyPlaylist);
if ([loadedItems count] > 0)
[self loadSpotifyPlaylist];
}];
}];
}
- (void)loadSpotifyPlaylist
{
NSArray *tracksArray = [self tracksFromPlaylistItems:[_spotifyPlaylist items]];
NSLog(#"Created SPTrack items: %d", [tracksArray count]);
if (tracksArray && [tracksArray count] > 0)
{
[SPAsyncLoading waitUntilLoaded:tracksArray timeout:kSpotifyTimeout then:^(NSArray *loadedItems, NSArray *notLoadedItems) {
_playlist = loadedItems;
NSLog(#"Loaded SPTrack items: %d", [loadedItems count]);
NSLog(#"Failed to load SPTrack items: %d", [notLoadedItems count]);
[self finishReloadData];
}];
}
_spotifyPlaylist and _playlist are both strong instances and tracksFromPlaylistItems: is the same method used in the "Guess the Info" sample code.
Edit: The kSpotifyTimeout timeout is set to 10.0.
What's your value of kSpotifyTimeout? I think what you're seeing is simply the fact that metadata can take a long time to load (especially yesterday when the Spotify backend seemed to be having trouble, which can happen from time to time).
Generally, loading the entire contents of a playlist at once is bad practice, and you'll see that Guess the Intro doesn't actually care whether all the tracks loaded or not - it just grabs what happened to load and uses those tracks for the game.
A good approach to take is to paginate loading of playlist tracks to match your UI. That is, to only load the tracks your user can see, plus maybe a "screen" or two's worth up and down. As the user scrolls, you can start loading tracks - UIScrollViewDelegates -scrollViewWillEndDragging:withVelocity:targetContentOffset: is particularly good for this, as it lets you see where the user will "land" and start to load tracks for that point. There's some example code for doing this as well as a helper class called SPSparseArray for partially loading content like playlists on the dev branch of CococaLibSpotify (although be aware that dev branch = beta quality).

How to add "advertisements" in the UIScrollView like BBC News app?

I have created the news app which use the "UIScrollView + UIWebView" to let users to swipe through different news. However, I would like to ask whether it's possible for me to add the advertisements like the BBC News app or not, which users will see the advertisements while scrolling horizontally.
I was trying to use addSubView while scrolling (scrollViewDidScroll) but it replaces the existing content.
I realize BBC News app do the things like this,
Users swipe through the news --> After swiping a few times --> show the ads (in position 5, for instance) --> users swipe from start to the end again --> the ads appear again, but this time could be in position 3).
But I just don't know how to make it work for my app. Any clue? thanks in advance.
You probably already have a functionality, where You have stored news articles data in some array. Or You fetch data from database into some array.
Well - I think the easiest way would be just to add advert_data in this news array.
For example - You fetch from db in array:
NSArray *mArray = #[
"news1",
"news2",
"news3",
"news4",
"news5",
"news6"];
then You could simply - iterate it all through, and add advert data. For example
int counter = 0;
while (counter < [mArray count])
{
int mTmp = 3+(arc4random() % 2); // atleast 3, but up to 5?
counter +=mTmp;
[mArray insertObject:"advert" atIndex:counter];
counter +=1; //we added one object, so we need to adjust counter
}
Then simply - when You try to load corresponding data in webview, check if content is not "advert". If it is - load some random advert html from somewhere.
P.S. I guess, Your data arrays will have NSManagedObjects, instead of strings. Then You can check if [mArray objectAtIndex:i] (when You try to load it into webview)- is NSManagedObject or a string value.
I hope You understand this idea.

Implement my own a custom GKTurnBasedMatchmakerViewController

I'm sure there are questions similar to this, but I couldn't find an example. I'm trying to recreate the apple GKTurnBasedMatchmakerViewController for my turn based game with my own custom interface for my turn based iphone game. I'm having trouble getting all the options to display correctly so I was hoping someone had a working example. My current code is this:
-(void)getCurrentGamesFromServer {
[GKTurnBasedMatch loadMatchesWithCompletionHandler:^(NSArray *matches, NSError *error) {
if(matches){
for (GKTurnBasedMatch *myMatch in matches) {
//this is, I believe, also where we need to consider invitations. available instance methods provided by GC are: acceptInviteWithCompletionHandler and declineInviteWithCompletionHandler
if(myMatch.status == 1){ //ongoing game
if([myMatch.currentParticipant.playerID isEqualToString:[GKLocalPlayer localPlayer].playerID]){ //if it's my turn
//here we need to populate the table by adding our matches to mcurrentgamesarray. after all the matches have been added, reload the tableview
NSLog(#"active game that is my turn");
[self.mCurrentGamesArray addObject:myMatch];
}
else{
NSLog(#"other turn or an invite from another player waiting to hear from you");
}
}
}
}
[_mCurrentGamesTableView reloadData];
}];
}
As you can see, the only games I'm grabbing right now are games where the status is 1 (ongoing game) and the current participant ID is my ID.
to get games that were not my turn, I tried doing games where it was not my ID, but it included invites with it and I couldn't figure out how to separate them out.
Basically, I'd like to have a section of games where it is my turn, a section where it is not my turn but games are still active, a section of completed old games, and a section for invites. does anyone have a working example, or a page they can send me to that explains the best practices for what I'm trying to do? Thanks for your help.

Resources