Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7181261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:31:16+00:00 2026-05-28T17:31:16+00:00

I have seen iCloud Document sample code for iOS and I used it to

  • 0

I have seen iCloud Document sample code for iOS and I used it to sync a uidocument to and from iCloud and now I am trying to sync iCloud with an nsdocument on a Mac OSX app which doesn’t have UIDocument.

I tried to change UIDocument to NSDocument but all the methods of syncing with icloud are different. I haven’t found any sample code or tutorials except for the documentation from apple which is very confusing and not well written.

For example, the method below, from UIDocument on iOS does not exist in NSDocument on OS X:

//doc is an instance of subclassed UIDocument
[doc openWithCompletionHandler:nil];

The Apple Documentation provides this code for OS X:

- (void)checkIfCloudAvaliable {
    NSURL *ubiquityContainerURL = [[[NSFileManager defaultManager]
                                    URLForUbiquityContainerIdentifier:nil]
                                   URLByAppendingPathComponent:@"Documents"];
    if (ubiquityContainerURL == nil) {
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                              NSLocalizedString(@"iCloud does not appear to be configured.", @""),
                              NSLocalizedFailureReasonErrorKey, nil];
        NSError *error = [NSError errorWithDomain:@"Application" code:404
                                         userInfo:dict];
        [self presentError:error modalForWindow:[self windowForSheet] delegate:nil
        didPresentSelector:NULL contextInfo:NULL];
        return;
    }
    dest = [ubiquityContainerURL URLByAppendingPathComponent:
            [[self fileURL] lastPathComponent]];
}

- (void)moveToOrFromCloud {
    dispatch_queue_t globalQueue =
    dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(globalQueue, ^(void) {
        NSFileManager *fileManager = [[NSFileManager alloc] init];
        NSError *error = nil;
        // Move the file.
        BOOL success = [fileManager setUbiquitous:YES itemAtURL:[self fileURL]
                                   destinationURL:dest error:&error];
        dispatch_async(dispatch_get_main_queue(), ^(void) {
            if (! success) {
                [self presentError:error modalForWindow:[self windowForSheet]
                          delegate:nil didPresentSelector:NULL contextInfo:NULL];
            }
        });
    });
    [self setFileURL:dest];
    [self setFileModificationDate:nil];
}

How can I sync between iOS and OS X (because NSDocument does not exists on iOS, and UIDocument does not exist on OS X)? Does anyone know where I can find a sample for Mac OSX (NSDocument syncing)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:31:17+00:00Added an answer on May 28, 2026 at 5:31 pm

    I managed to get it working! Here’s my code from the subclassed nsdocument file on OS X:

    Header file:

    #import <Cocoa/Cocoa.h>
    #import <Foundation/Foundation.h>
    
    @interface subclassedNSDocument : NSDocument
    
    @property (strong) NSData *myData;
    
    @end
    

    Implementation file:

    - (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError 
    {
        BOOL readSuccess = NO;
        if (data) 
        {
            readSuccess = YES;
            [self setMyData:data];
        }
    
        [[NSNotificationCenter defaultCenter] postNotificationName:@"dataModified" 
                                                            object:self];
    
        return readSuccess;
    }
    
    - (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError 
    {
        if (!myData && outError) {
            *outError = [NSError errorWithDomain:NSCocoaErrorDomain
                                            code:NSFileWriteUnknownError userInfo:nil];
        }
        return myData;
    }
    

    and in the AppDelegate.m file:

    #define kFILENAME @"mydocument.dox"
    
    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    {
        NSURL *ubiq = [[NSFileManager defaultManager] 
                       URLForUbiquityContainerIdentifier:nil];
        if (ubiq) {
            NSLog(@"iCloud access at %@", ubiq);
            // TODO: Load document... 
            [self loadDocument];
        }
        else 
        {
            NSLog(@"No iCloud access");
        }
    
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(dataReloaded:) 
                                                     name:@"dataModified" object:nil];
    }
    
    - (void)update_iCloud
    {
        NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
        NSURL *ubiquitousPackage = [[ubiq URLByAppendingPathComponent:@"Documents"] URLByAppendingPathComponent:kFILENAME];
        self.doc.myData = [NSKeyedArchiver archivedDataWithRootObject:[@"Your Data Array or any data", nil]];
        [self.doc saveToURL:ubiquitousPackage ofType:@"dox" forSaveOperation:NSSaveOperation error:nil];
    }
    
    - (void)loadData:(NSMetadataQuery *)query {
    
        if ([query resultCount] == 1) {
    
            NSMetadataItem *item = [query resultAtIndex:0];
            NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];
            NSLog(@"url = %@",url);
            subclassedNSDocument *doc = [[subclassedNSDocument alloc] initWithContentsOfURL:url ofType:@"dox" error:nil];
            [doc setFileURL:url];
            self.doc = doc;
        } 
        else {
    
            NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
            NSURL *ubiquitousPackage = [[ubiq URLByAppendingPathComponent:@"Documents"] URLByAppendingPathComponent:kFILENAME];
    
            dataUrls *doc = [[dataUrls alloc] init];
            [self.doc setFileURL:ubiquitousPackage];
            self.doc = doc;
            [self.doc saveToURL:ubiquitousPackage ofType:@"dox" forSaveOperation:NSSaveOperation error:nil];
        }
    
    }
    
    - (void)queryDidFinishGathering:(NSNotification *)notification {
    
        NSMetadataQuery *query = [notification object];
        [query disableUpdates];
        [query stopQuery];
    
        [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                        name:NSMetadataQueryDidFinishGatheringNotification
                                                      object:query];
    
        _query = nil;
    
        [self loadData:query];
    
    }
    
    - (void)loadDocument {
    
        NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
        _query = query;
        [query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];
        NSPredicate *pred = [NSPredicate predicateWithFormat: @"%K == %@", NSMetadataItemFSNameKey, kFILENAME];
        [query setPredicate:pred];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryDidFinishGathering:) name:NSMetadataQueryDidFinishGatheringNotification object:query];
    
        [query startQuery];
    
    }
    
    - (void)dataReloaded:(NSNotification *)notification 
    {
        self.doc = notification.object;
    
        NSArray *arrFromCloud = [NSKeyedUnarchiver unarchiveObjectWithData:self.doc.myData];
    
        //Update you UI with new data
    }
    

    The only thing that I haven’t got working is that if I change the data of the document on the iPad, the Mac app doesn’t call the readFromData method for to update from iCloud, does anyone know what I am missing?

    On iOS, the equivalent method, loadFromContents, is called automatically on every change of the UIDocument in iCloud. On OS X the readFromData is called once on load but never called again.

    Hope my code can help, for me it is working one way from Mac to iPad.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen the mentioned piece of code on several occasions now, what's the
I have seen examples of printing from a windows application but I have not
I have seen this example: http://jsfiddle.net/UwEe2/306/ that is implemented with scrollleft . Now I
I have seen this line of code in several tutorials for using Unity in
i have seen this code : var myNet = require (net); and in some
I have seen people use GO statement between batches of SQL code, but AFAICS
I have seen the following code in a website... what does this mean?.Can i
I have seen the following definition throughout legacy code: std::vector<boost::shared_ptr<ClassNameAAA>> vecClass; I am able
I have seen this code in web.xml file <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> I build small
I have seen this feature on McAfee WaveSecure. Is there a sample application demonstrates

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.