Opening a Datastore in iOS Dropbox Sync API 3.0 - ios

One of the new features of the 3.0 Datastore API is the ability to use local datastores that will later sync up with Dropbox when the user decides to link your app to Dropbox. I'm wondering how the process of opening datastores differs now.
For example, this is how I currently open a datastore:
DBAccount *account = [[DBAccountManager sharedManager] linkedAccount];
self.store = [DBDatastore openDefaultStoreForAccount:account error:nil];
How do I get a DBAccount without a linked account? Maybe I don't. :)
On that same note, what is the process for opening a datastore with openDefaultStoreForAccount if there is no account present?
I just noticed openDefaultLocalStoreForAccountManager:
Is this how it's used? And does this still work later when there is a linked account?
self.store = [DBDatastore openDefaultLocalStoreForAccountManager:[DBAccountManager sharedManager] error:nil];
I'd appreciate any help. Thanks!

From https://www.dropbox.com/developers/blog/99/using-the-new-local-datastores-feature:
// If the user has linked a Dropbox account for the first time...
if (_justLinked) {
if (_localDatastoreManager && self.account) {
// Perform a one-time migration to move from the local datastore to a remote one.
[_localDatastoreManager migrateToAccount:self.account error:nil];
_localDatastoreManager = nil;
}
}
if (!_store) {
// If there's a linked account, use that.
if ([[DBAccountManager sharedManager] linkedAccount]) {
_store = [DBDatastore openDefaultStoreForAccount:self.account error:nil];
// Otherwise, use a local datastore.
} else {
_store = [DBDatastore openDefaultLocalStoreForAccountManager:[DBAccountManager sharedManager]
error:nil];
}
}

Related

Download Multiple Images Sequentially using NSURLSession downloadTask in Objective C

My app offers the option to download 3430 high resolution images from our server, each image of size 50k - 600k bytes.
The original approach was to just download all of them - but we realized that gave a lot of NSURLErrorTimedOut errors and crashed our program. We then implemented it such that we download all of the images, but in batches of 100 images at a time. Someone on SO suggested we actually implement our download like this:
Create a list of all file URLs that need to be downloaded.
Write your code so that it downloads these URLs sequentially. I.e. do
not let it start downloading a file until the previous one has
finished (or failed and you decided to skip it for now).
Use NSURLSession's support for downloading an individual file to a
folder, don't use the code to get an NSData and save the file
yourself. That way, your application doesn't need to be running while
the download finishes.
Ensure that you can tell whether a file has already been downloaded or
not, in case your download gets interrupted, or the phone is restarted
in mid-download. You can e.g. do this by comparing their names (if
they are unique enough), or saving a note to a plist that lets you
match a downloaded file to the URL where it came from, or whatever
constitutes an identifying characteristic in your case.
At startup, check whether all files are there. If not, put the missing
ones in above download list and download them sequentially, as in #2.
Before you start downloading anything (and that includes downloading
the next file after the previous download has finished or failed), do
a reachability check using the Reachability API from Apple's
SystemConfiguration.framework. That will tell you whether the user has
a connection at all, and whether you're on WiFi or cellular (in
general, you do not want to download a large number of files via
cellular, most cellular connections are metered).
We create a list of all images to download here:
- (void)generateImageURLList:(BOOL)batchDownloadImagesFromServer
{
NSError* error;
NSFetchRequest* leafletURLRequest = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription* leafletURLDescription = [NSEntityDescription entityForName:#"LeafletURL" inManagedObjectContext:managedObjectContext];
[leafletURLRequest setEntity:leafletURLDescription];
numberOfImages = [managedObjectContext countForFetchRequest:leafletURLRequest error:&error];
NSPredicate* thumbnailPredicate = [NSPredicate predicateWithFormat:#"thumbnailLocation like %#", kLocationServer];
[leafletURLRequest setPredicate:thumbnailPredicate];
self.uncachedThumbnailArray = [managedObjectContext executeFetchRequest:leafletURLRequest error:&error];
NSPredicate* hiResPredicate = [NSPredicate predicateWithFormat:#"hiResImageLocation != %#", kLocationCache];
[leafletURLRequest setPredicate:hiResPredicate];
self.uncachedHiResImageArray = [managedObjectContext executeFetchRequest:leafletURLRequest error:&error];
}
We use NSURLSession to download an individual image to a folder by calling hitServerForUrl and implementing didFinishDownloadingToURL:
- (void)hitServerForUrl:(NSURL*)requestUrl {
NSURLSessionConfiguration *defaultConfigurationObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:defaultConfigurationObject delegate:self delegateQueue: nil];
NSURLSessionDownloadTask *fileDownloadTask = [defaultSession downloadTaskWithURL:requestUrl];
[fileDownloadTask resume];
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
if (isThumbnail)
{
leafletURL.thumbnailLocation = kLocationCache;
}
else
{
leafletURL.hiResImageLocation = kLocationCache;
}
// Filename to write to
NSString* filePath = [leafletURL pathForImageAtLocation:kLocationCache isThumbnail:isThumbnail isRetina:NO];
// If it's a retina image, append the "#2x"
if (isRetina_) {
filePath = [filePath stringByReplacingOccurrencesOfString:#".jpg" withString:#"#2x.jpg"];
}
NSString* dir = [filePath stringByDeletingLastPathComponent];
[managedObjectContext save:nil];
NSError* error;
[[NSFileManager defaultManager] createDirectoryAtPath:dir withIntermediateDirectories:YES attributes:nil error:&error];
NSURL *documentURL = [NSURL fileURLWithPath:filePath];
NSLog(#"file path : %#", filePath);
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
//Remove the old file from directory
}
[[NSFileManager defaultManager] moveItemAtURL:location
toURL:documentURL
error:&error];
if (error){
//Handle error here
}
}
This code calls loadImage, which calls `hitServer:
-(void)downloadImagesFromServer{
[self generateImageURLList:NO];
[leafletImageLoaderQueue removeAllObjects];
numberOfHiResImageLeft = [uncachedHiResImageArray count];
for ( LeafletURL* aLeafletURL in uncachedHiResImageArray)
{
//// Do the same thing again, except set isThumb = NO. ////
LeafletImageLoader* hiResImageLoader = [[LeafletImageLoader alloc] initWithDelegate:self];
[leafletImageLoaderQueue addObject:hiResImageLoader]; // do this before making connection!! //
[hiResImageLoader loadImage:aLeafletURL isThumbnail:NO isBatchDownload:YES];
//// Adding object to array already retains it, so it's safe to release it here. ////
[hiResImageLoader release];
uncachedHiResIndex++;
NSLog(#"uncached hi res index: %ld, un cached hi res image array size: %lu", (long)uncachedHiResIndex, (unsigned long)[uncachedHiResImageArray count]);
}
}
- (void)loadImage:(LeafletURL*)leafletURLInput isThumbnail:(BOOL)isThumbnailInput isBatchDownload:(BOOL)isBatchDownload isRetina:(BOOL)isRetina
{
isRetina_ = isRetina;
if (mConnection)
{
[mConnection cancel];
[mConnection release];
mConnection = nil;
}
if (mImageData)
{
[mImageData release];
mImageData = nil;
}
self.leafletURL = leafletURLInput;
self.isThumbnail = isThumbnailInput;
NSString* location = (self.isThumbnail) ?leafletURL.thumbnailLocation :leafletURL.hiResImageLocation;
//// Check if the image needs to be downloaded from server. If it is a batch download, then override the local resources////
if ( ([location isEqualToString:kLocationServer] || (isBatchDownload && [location isEqualToString:kLocationResource])) && self.leafletURL.rawURL != nil )
{
//NSLog(#"final loadimage called server");
//// tell the delegate to get ride of the old image while waiting. ////
if([delegate respondsToSelector:#selector(leafletImageLoaderWillBeginLoadingImage:)])
{
[delegate leafletImageLoaderWillBeginLoadingImage:self];
}
mImageData = [[NSMutableData alloc] init];
NSURL* url = [NSURL URLWithString:[leafletURL pathForImageOnServerUsingThumbnail:self.isThumbnail isRetina:isRetina]];
[self hitServerForUrl:url];
}
//// if not, tell the delegate that the image is already cached. ////
else
{
if([delegate respondsToSelector:#selector(leafletImageLoaderDidFinishLoadingImage:)])
{
[delegate leafletImageLoaderDidFinishLoadingImage:self];
}
}
}
Currently, I'm trying to figure out how to download the images sequentially, such that we don't call hitServer until the last image is finished downloading. Do I need to be downloading in the background? Thank you for suggestions!
My app offers the option to download 3430 high resolution images from our server, each image of size 50k - 600k bytes.
This seems like a job for on-demand resources. Just turn these files into on-demand resources obtained from your own server, and let the system take care of downloading them in its own sweet time.
This sounds very much like an architectural issue. If you fire off downloads without limiting them of course you're going to start getting timeouts and other things. Think about other apps and what they do. Apps that give the user the ability to do multiple downloads often limit how may can occur at once. iTunes for example can queue up thousands of downloads, but only runs 3 at a time. Limiting to just one at a time will only slow things down for your users. You need a balance that consider your user's available bandwidth.
The other part of this is to again consider what your users want. Does every one of your uses want every single image? I don't know what you are offering them, but in most apps which access resources like images or music, it's up to the user what and when they download. Thus they only download what they are interested in. So I'd recommend only downloading what the users are viewing or have somehow requested they want to download.

Office 365 iOS: Not able to fetch List Items from Office 365 SDK

I have added the announcement app and added few items to it, now i want to fetch the items from my announcement list so I have used the below code
token_Obtained_During_first_time_Login: This is the token that i get when i login for the first time using the acquireTokenWithResource method of ADAuthenticationContext class
- (void)getClientList {
NSString *appToken = [[NSUserDefaults standardUserDefaults]
valueForKey:#"token_Obtained_During_first_time_Login"];
NSString* hostName = #"https://myTenant.sharepoint.com/sites/myApp";
OAuthentication *credentials = [[OAuthentication alloc] initWith:appToken];
ListClient *client = [[ListClient alloc]
initWithUrl:hostName
credentials:credentials];
NSURLSessionTask* task = [client getListItems:#"MyAnnouncements"
callback:^(NSMutableArray *listItems, NSError *error) {
if (error==nil) {
NSLog(#"%#",listItems);
}
}];
[task resume];
}
I have even debugged the 365 code and it provides me the below URL for getListItems: callback method
https://myTenant.sharepoint.com/sites/myApp/_api/lists/GetByTitle('MyAnnouncements')/Items
I have even tried the same using getTokenWith method which comes with the sample code
- (void)getAnnouncementList:(void (^)(ListClient *))callback{
NSString* hostName = #"https://myTenant.sharepoint.com";
[self getTokenWith:hostName :true completionHandler:^(NSString *token) {
OAuthentication *credentials = [[OAuthentication alloc] initWith:token];
callback([[ListClient alloc]initWithUrl:hostName credentials:credentials]);
}];
}
But still no luck i get the list as nil
Please guide on how this can be resolved, I have even verified the rights in the Azure Directory everything seems fine am able to fetch data of one drive, mails and calendar but list is a place where i am stuck.
Every time i call the above code i get the response nil not sure what am passing wrong, my guess is the token.
I resolved this issue by making a change in the apiUrl present in the ListClient.m file of Office 365.
All i did was changed it to
const NSString *apiUrl = #"/sites/mobileApp/_api/web/Lists";
Making the above change did the trick and now i can access all the list data.

Get GDataEntryBase instead of GDataEntryContact then try to fetch google contacts

I integrate gmail into my app by using GData Obcective-C Client for authentication and obtaining therefrom contacts. For authentication I use gtm-oauth2 and this part work pretty good.
My scope for GTMOAuth2ViewControllerTouch init:
NSString *scope = [NSString stringWithFormat:#"https://www.googleapis.com/auth/plus.me %#", [GDataServiceGoogleContact authorizationScope]];
Auth init:
__keychainItemName = [infoPlist objectForKey:#"GoogleKeyChainItem"];
__auth = [GTMOAuth2ViewControllerTouch
authForGoogleFromKeychainForName:__keychainItemName
clientID:[infoPlist objectForKey:#"GoogleClientID"]
clientSecret:[infoPlist objectForKey:#"GoogleClientSecret"]];
For GData building i use this blog (with pics and stuff)
http://hoishing.wordpress.com/2011/08/23/gdata-objective-c-client-setup-in-xcode-4/
GData I get from google repository, just by running this in console
# Non-members may check out a read-only working copy anonymously over HTTP.
svn checkout http://gdata-objectivec-client.googlecode.com/svn/trunk/ gdata-objectivec-client-read-only
Problems begin when I try to get contacts:
- (GDataServiceGoogleContact *)contactService {
static GDataServiceGoogleContact* service = nil;
if (!service) {
service = [[GDataServiceGoogleContact alloc] init];
[service setShouldCacheResponseData:YES];
[service setServiceShouldFollowNextLinks:YES];
[service setAuthorizer:__auth];
}
return service;
}
- (void) methodExecute {
GDataServiceGoogleContact *service = [self contactService];
GDataServiceTicket *ticket;
const int kBuncha = 2000;
NSURL *feedURL = [GDataServiceGoogleContact contactFeedURLForUserID:kGDataServiceDefaultUser];
GDataQueryContact *query = [GDataQueryContact contactQueryWithFeedURL:feedURL];
[query setShouldShowDeleted:NO];
[query setMaxResults:kBuncha];
[ticket setAuthorizer:__auth];
ticket = [service fetchFeedWithQuery:query
delegate:self
didFinishSelector:#selector(contactsFetchTicket:finishedWithFeed:error:)];
}
- (void)contactsFetchTicket:(GDataServiceTicket *)ticket
finishedWithFeed:(GDataFeedContact *)feed
error:(NSError *)error {
if(error != nil){
NSLog(#"%#\n\n\n%#", error, feed);
}
else{
NSLog(#"%#\n\n\n%#", error, feed.entries);
}
}
And here is the point - instead of GDataEntryContact which have to be in feed, I get array of GDataEntryBase objects. There is object description example:
GDataEntryBase 0xb3b2300: {v:3.1 title:John Jackson etag:"Rn4_fjVSLit***."
categories:1 links:photo,self,edit edited:2013-03-14T17:55:57Z
id:http://www.google.com/m8/feeds/contacts/myemail%40gmail.com/base/kindofid
unparsed:<gContact:groupMembershipInfo>,<gd:name>,<gd:phoneNumber>}
I try to replace svn GData to This GData version, but everything is useless. I'm on the edge.
BTW I also turned "on" the option Contacts API at google console and added -DGDATA_INCLUDE_CONTACTS_SERVICE=1 in Other C Flags for GData.
Am I missed something or just stupid?
Great thanks for your reply!
I entered other linker flags only for project and they are, for some reason, do not applied for the whole target.

How do you migrate an existing core data iOS 7 app's data into iCloud?

I have an iOS 7 app that already uses Core Data. I have used the new iOS 7 method of integrating iCloud into my app to sync items stored in core data by using the following code as an example:
https://github.com/mluisbrown/iCloudCoreDataStack/blob/master/README.md
This works great, except that all of the original data on the device doesn't show up in the iCloud store. I keep hearing that I need to migrate the data - but I can't find any examples on how to do this properly. Does anyone know how to do this?
I keep getting pointed to using migratePersistentStore:toURL:options:withType:error:, but I don't see how I can use this...
Here is a sample app with a iCloud control panel to move the store to or from iCloud. To move your existing store you need to open it with the existing options but make sure you use iOS7 options for the target store. Take a look at the sample apps code in OSCDStackManager and if you have specific questions then post them. http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/
- (bool)moveStoreFileToICloud:(NSURL*)fileURL delete:(bool)shouldDelete backup:(bool)shouldBackup {
FLOG(#" called");
// Always make a backup of the local store before migrating to iCloud
if (shouldBackup)
[self backupLocalStore];
NSPersistentStoreCoordinator *migrationPSC = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];
// Open the existing local store using the original options
id sourceStore = [migrationPSC addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:fileURL options:[self localStoreOptions] error:nil];
if (!sourceStore) {
FLOG(#" failed to add old store");
return FALSE;
} else {
FLOG(#" Successfully added store to migrate");
bool moveSuccess = NO;
NSError *error;
FLOG(#" About to migrate the store...");
// Now migrate the store using the iCloud options
id migrationSuccess = [migrationPSC migratePersistentStore:sourceStore toURL:[self icloudStoreURL] options:[self icloudStoreOptions] withType:NSSQLiteStoreType error:&error];
if (migrationSuccess) {
moveSuccess = YES;
FLOG(#"store successfully migrated");
[self deregisterForStoreChanges];
_persistentStoreCoordinator = nil;
_managedObjectContext = nil;
self.storeURL = [self icloudStoreURL];
// Now delete the local file
if (shouldDelete) {
FLOG(#" deleting local store");
[self deleteLocalStore];
} else {
FLOG(#" not deleting local store");
}
return TRUE;
}
else {
FLOG(#"Failed to migrate store: %#, %#", error, error.userInfo);
return FALSE;
}
}
return FALSE;
}
You move your existing store to a different path, and then you call the migrate method with the toURL set to the path where you want your store to end up.
You need to pass the options in that include the ubiquity settings that an iCloud store needs to have set.
When the migration is finished, you should have two copies of the store: the non-iCloud one which you moved aside, and the new one with iCloud options set. You can now remove the old store if you like, and just setup your Core Data stack to use the iCloud store.
Take a look at some of the methods in this example. In particular, look at the ones beginning with 'migrate'. You should be able to work out what steps to take to migrate data to a new cloud store.
Core Data sync is hard to get right, especially when you start to get into migrating in data. It is worth looking at other Core Data sync options like Wasabi Sync and Ensembles. They handle migration and merging of data automatically. (Disclosure: I develop Ensembles)

Dropbox Error Opens Another App on device, not on simulator

I have three apps that upload files to Dropbox. Same code for all three. They all share the same folder so I've used same secret key etc
Heres where it gets weird
1. All was fine for few months
2. Now on apps 2 and 3 when the user tries to log in it opens the first app?
3. Logging out and in, no help, just says Theres a an error connecting to Dropbox and to try later
What ive tried
Creating seprate secret keys etc for all three apps rathe rthan sharing the same, still get the same behaviour?
Some research on this suggested that Dropbox has changed the way it links to users accounts through applications and remins linked even if you delete the app? Has anyone else got any experience with this?
Appdelegate
NSString* appKey = #"00000000000";
NSString* appSecret = #"0000000000";
NSString *root = kDBRootAppFolder;
DBSession* session =
[[DBSession alloc] initWithAppKey:appKey appSecret:appSecret root:root];
session.delegate = self; // DBSessionDelegate methods allow you to handle re-authenticating
[DBSession setSharedSession:session];
[DBRequest setNetworkRequestDelegate:self];
Button Handler in viewController
LogCmd();
self.publishButtonPressed = YES;
if (![[DBSession sharedSession] isLinked]) {
[self loginLogoutButtonPressed:nil];
} else {
DBRestClient *restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
restClient.delegate = self;
NSError *error = nil;
NSString *filePath = [ICUtils pathForDocument:self.fileName];
[self.pdfData writeToFile:filePath options:0 error:&error];
if (nil == error) {
[restClient uploadFile:self.fileName
toPath:#"/"
withParentRev:nil
fromPath:filePath];
} else {
[ICUtils raiseAlertWithTitle:#"An error occurred" message:[error localizedDescription]];
}
}
}
Note works ok on the simulator, problem is only present on device
That is because you are using the same key for multiple applications. The Dropbox app is communicating with your app through a custom URL scheme - to launch your app (because there is no other way to launch apps programmatically on iOS).
In other words, the Dropbox app tells the system to open "db-yoursecretkey://somemessage" which opens the registered app for that custom URL scheme. Unfortunately, all of your apps use the same custom scheme as they are all using the same key, so the system just picks one: most likely the first one.
However, you can grant your apps access to all folders in dropbox, thus effectively sharing folders. So it's not really necessary to have all three apps using the same key.

Resources