简体   繁体   中英

Cannot drag vCard from Apple's Address Book to app in Mac OS X Lion

I'm working on an Objective-C app that is Mac OS X Lion only. I'm trying to accept drag & drops from Address Book. It looks like in Lion, Address Book vCard representations are not returning any data. Unfortunately I cannot test the code under a previous OS, though I had found sample code suggesting that it was possible before.

I setup my table to receive kUTTypeVCard like the docs suggest for 10.6+ (though NSVCardPboardType works, too) and I do receive the drop event. So to debug I setup a loop to output the data types and their values from the drop:

- (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id <NSDraggingInfo>)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)operation
{
    NSPasteboard *pb = [info draggingPasteboard];

    NSArray *types = pb.types;
    for (NSString *type in types)
    {
        NSLog(@" ");
        NSLog(@"%@:", type);
        NSLog(@"    property list = %@", [pb propertyListForType:type]);
        NSLog(@"    data = %@", [pb dataForType:type]);
        NSLog(@"    string = %@", [pb stringForType:type]);
    }
    NSLog(@" ");

    return YES;
}

The results are as follows:

2012-02-01 19:46:18.907 MyAppName[586:60b]  
2012-02-01 19:46:18.907 MyAppName[586:60b] dyn.ah62d4rv4gu8ycuwftb2gc5xeqzwfg3pqqzv1k4ptr3m1k6xmr3xyc6xwqf6zk8puqy:
2012-02-01 19:46:18.909 MyAppName[586:60b]     property list = (null)
2012-02-01 19:46:18.911 MyAppName[586:60b]     data = (null)
2012-02-01 19:46:18.912 MyAppName[586:60b]     string = (null)
2012-02-01 19:46:18.913 MyAppName[586:60b]  
2012-02-01 19:46:18.913 MyAppName[586:60b] ABExpandedSelectionStringArrayType:
2012-02-01 19:46:18.914 MyAppName[586:60b]     property list = (null)
2012-02-01 19:46:18.914 MyAppName[586:60b]     data = (null)
2012-02-01 19:46:18.915 MyAppName[586:60b]     string = (null)
2012-02-01 19:46:18.915 MyAppName[586:60b]  
2012-02-01 19:46:18.915 MyAppName[586:60b] dyn.ah62d4rv4gu8ycuwxqz0gn25yrf106y5ysmy0634bsm3gc8nytf2gn:
2012-02-01 19:46:18.917 MyAppName[586:60b]     property list = (null)
2012-02-01 19:46:18.918 MyAppName[586:60b]     data = (null)
2012-02-01 19:46:18.918 MyAppName[586:60b]     string = (null)
2012-02-01 19:46:18.918 MyAppName[586:60b]  
2012-02-01 19:46:18.919 MyAppName[586:60b] ABSelectionStringArrayType:
2012-02-01 19:46:18.919 MyAppName[586:60b]     property list = (null)
2012-02-01 19:46:18.921 MyAppName[586:60b]     data = (null)
2012-02-01 19:46:18.922 MyAppName[586:60b]     string = (null)
2012-02-01 19:46:18.922 MyAppName[586:60b]  
2012-02-01 19:46:18.922 MyAppName[586:60b] dyn.ah62d4rv4gu8yc6durvwwa3xmrvw1gkdusm1044pxqyuha2pxsvw0e55bsmwca7d3sbwu:
2012-02-01 19:46:18.922 MyAppName[586:60b]     property list = (
    vcf
)
2012-02-01 19:46:18.923 MyAppName[586:60b]     data = <3c3f786d 6c207665 7273696f 6e3d2231 2e302220 656e636f 64696e67 3d225554 462d3822 3f3e0a3c 21444f43 54595045 20706c69 73742050 55424c49 4320222d 2f2f4170 706c652f 2f445444 20504c49 53542031 2e302f2f 454e2220 22687474 703a2f2f 7777772e 6170706c 652e636f 6d2f4454 44732f50 726f7065 7274794c 6973742d 312e302e 64746422 3e0a3c70 6c697374 20766572 73696f6e 3d22312e 30223e0a 3c617272 61793e0a 093c7374 72696e67 3e766366 3c2f7374 72696e67 3e0a3c2f 61727261 793e0a3c 2f706c69 73743e0a>
2012-02-01 19:46:18.923 MyAppName[586:60b]     string = <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <string>vcf</string>
</array>
</plist>
2012-02-01 19:46:18.923 MyAppName[586:60b]  
2012-02-01 19:46:18.923 MyAppName[586:60b] Apple files promise pasteboard type:
2012-02-01 19:46:18.923 MyAppName[586:60b]     property list = (
    vcf
)
2012-02-01 19:46:18.924 MyAppName[586:60b]     data = <3c3f786d 6c207665 7273696f 6e3d2231 2e302220 656e636f 64696e67 3d225554 462d3822 3f3e0a3c 21444f43 54595045 20706c69 73742050 55424c49 4320222d 2f2f4170 706c652f 2f445444 20504c49 53542031 2e302f2f 454e2220 22687474 703a2f2f 7777772e 6170706c 652e636f 6d2f4454 44732f50 726f7065 7274794c 6973742d 312e302e 64746422 3e0a3c70 6c697374 20766572 73696f6e 3d22312e 30223e0a 3c617272 61793e0a 093c7374 72696e67 3e766366 3c2f7374 72696e67 3e0a3c2f 61727261 793e0a3c 2f706c69 73743e0a>
2012-02-01 19:46:18.924 MyAppName[586:60b]     string = <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <string>vcf</string>
</array>
</plist>
2012-02-01 19:46:18.924 MyAppName[586:60b]  
2012-02-01 19:46:18.925 MyAppName[586:60b] dyn.ah62d4rv4gu8ycuwuqz11a5dfnzeyk64uqm10c6xenv61a3k:
2012-02-01 19:46:18.925 MyAppName[586:60b]     property list = (
    "C9786D5F-157A-4A65-A95E-C61752D2B4E3:ABPerson"
)
2012-02-01 19:46:18.925 MyAppName[586:60b]     data = <3c3f786d 6c207665 7273696f 6e3d2231 2e302220 656e636f 64696e67 3d225554 462d3822 3f3e0a3c 21444f43 54595045 20706c69 73742050 55424c49 4320222d 2f2f4170 706c652f 2f445444 20504c49 53542031 2e302f2f 454e2220 22687474 703a2f2f 7777772e 6170706c 652e636f 6d2f4454 44732f50 726f7065 7274794c 6973742d 312e302e 64746422 3e0a3c70 6c697374 20766572 73696f6e 3d22312e 30223e0a 3c617272 61793e0a 093c7374 72696e67 3e433937 38364435 462d3135 37412d34 4136352d 41393545 2d433631 37353244 32423445 333a4142 50657273 6f6e3c2f 73747269 6e673e0a 3c2f6172 7261793e 0a3c2f70 6c697374 3e0a>
2012-02-01 19:46:18.926 MyAppName[586:60b]     string = <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <string>C9786D5F-157A-4A65-A95E-C61752D2B4E3:ABPerson</string>
</array>
</plist>
2012-02-01 19:46:18.926 MyAppName[586:60b]  
2012-02-01 19:46:18.926 MyAppName[586:60b] ABPeopleUIDsPboardType:
2012-02-01 19:46:18.927 MyAppName[586:60b]     property list = (
    "C9786D5F-157A-4A65-A95E-C61752D2B4E3:ABPerson"
)
2012-02-01 19:46:18.927 MyAppName[586:60b]     data = <3c3f786d 6c207665 7273696f 6e3d2231 2e302220 656e636f 64696e67 3d225554 462d3822 3f3e0a3c 21444f43 54595045 20706c69 73742050 55424c49 4320222d 2f2f4170 706c652f 2f445444 20504c49 53542031 2e302f2f 454e2220 22687474 703a2f2f 7777772e 6170706c 652e636f 6d2f4454 44732f50 726f7065 7274794c 6973742d 312e302e 64746422 3e0a3c70 6c697374 20766572 73696f6e 3d22312e 30223e0a 3c617272 61793e0a 093c7374 72696e67 3e433937 38364435 462d3135 37412d34 4136352d 41393545 2d433631 37353244 32423445 333a4142 50657273 6f6e3c2f 73747269 6e673e0a 3c2f6172 7261793e 0a3c2f70 6c697374 3e0a>
2012-02-01 19:46:18.937 MyAppName[586:60b]     string = <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <string>C9786D5F-157A-4A65-A95E-C61752D2B4E3:ABPerson</string>
</array>
</plist>
2012-02-01 19:46:18.937 MyAppName[586:60b]  
2012-02-01 19:46:18.937 MyAppName[586:60b] public.vcard:
2012-02-01 19:46:18.942 MyAppName[586:60b]     property list = (null)
2012-02-01 19:46:18.944 MyAppName[586:60b]     data = (null)
2012-02-01 19:46:18.945 MyAppName[586:60b]     string = (null)
2012-02-01 19:46:18.945 MyAppName[586:60b]  
2012-02-01 19:46:18.945 MyAppName[586:60b] Apple VCard pasteboard type:
2012-02-01 19:46:18.945 MyAppName[586:60b]     property list = (null)
2012-02-01 19:46:18.946 MyAppName[586:60b]     data = (null)
2012-02-01 19:46:18.946 MyAppName[586:60b]     string = (null)
2012-02-01 19:46:18.948 MyAppName[586:60b]  
2012-02-01 19:46:18.949 MyAppName[586:60b] dyn.ah62d4rv4gu8z82xqqz1gk3penm11swpsqvw1u3px:
2012-02-01 19:46:18.949 MyAppName[586:60b]     property list = (null)
2012-02-01 19:46:18.949 MyAppName[586:60b]     data = <040b7374 7265616d 74797065 6481e803 84014084 8484114e 534d7574 61626c65 496e6465 78536574 0084840a 4e53496e 64657853 65740084 84084e53 4f626a65 63740085 84014901 96817c01 960186>
2012-02-01 19:46:18.949 MyAppName[586:60b]     string = (null)
2012-02-01 19:46:18.949 MyAppName[586:60b]  
2012-02-01 19:46:18.950 MyAppName[586:60b] _blendedRowIndexes:
2012-02-01 19:46:18.950 MyAppName[586:60b]     property list = (null)
2012-02-01 19:46:18.950 MyAppName[586:60b]     data = <040b7374 7265616d 74797065 6481e803 84014084 8484114e 534d7574 61626c65 496e6465 78536574 0084840a 4e53496e 64657853 65740084 84084e53 4f626a65 63740085 84014901 96817c01 960186>
2012-02-01 19:46:18.950 MyAppName[586:60b]     string = (null)
2012-02-01 19:46:18.950 MyAppName[586:60b]  
2012-02-01 19:46:18.950 MyAppName[586:60b] com.apple.pasteboard.promised-file-content-type:
2012-02-01 19:46:18.951 MyAppName[586:60b]     property list = public.vcard
2012-02-01 19:46:18.951 MyAppName[586:60b]     data = <7075626c 69632e76 63617264>
2012-02-01 19:46:18.951 MyAppName[586:60b]     string = public.vcard
2012-02-01 19:46:18.952 MyAppName[586:60b]  
2012-02-01 19:46:18.952 MyAppName[586:60b] com.apple.pasteboard.promised-file-url:
2012-02-01 19:46:18.954 MyAppName[586:60b]     property list = (null)
2012-02-01 19:46:18.954 MyAppName[586:60b]     data = <>
2012-02-01 19:46:18.954 MyAppName[586:60b]     string = 
2012-02-01 19:46:18.955 MyAppName[586:60b]  
2012-02-01 19:46:18.955 MyAppName[586:60b] dyn.ah62d4rv4gu8y6y4usm1044pxqzb085xyqz1hk64uqm10c6xenv61a3k:
2012-02-01 19:46:18.955 MyAppName[586:60b]     property list = (null)
2012-02-01 19:46:18.956 MyAppName[586:60b]     data = <>
2012-02-01 19:46:18.956 MyAppName[586:60b]     string = 
2012-02-01 19:46:18.956 MyAppName[586:60b]  
2012-02-01 19:46:18.956 MyAppName[586:60b] NSPromiseContentsPboardType:
2012-02-01 19:46:18.956 MyAppName[586:60b]     property list = (null)
2012-02-01 19:46:18.957 MyAppName[586:60b]     data = <>
2012-02-01 19:46:18.957 MyAppName[586:60b]     string = 
2012-02-01 19:46:18.957 MyAppName[586:60b] 

Most of the types looks like private Address Book data types. But you can see that the vCard representations are null. And I'm thinking that even if I used ABPeopleUIDsPboardType , it would get rejected from the App Store because it's not available in any header file.

Any suggestion in how to handle a drag & drop from Address Book under Lion?

Thanks.

* Edit 2/5/12 *

Tried researching promised drags and promised contents, but still coming up empty:

- (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id <NSDraggingInfo>)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)operation
{
    NSPasteboard *pb = [info draggingPasteboard];

    // Supposed to call this before readFileContentsType:toFile: for some reason
    NSArray *types = pb.types;

    NSString *destPath = NSTemporaryDirectory();
    NSURL *destURL = [NSURL fileURLWithPath:destPath isDirectory:YES];
    NSArray *fileNames = [info namesOfPromisedFilesDroppedAtDestination:destURL];
    NSLog(@"fileNames = %@", fileNames);

    NSString *afile = [pb readFileContentsType:nil toFile:@"random_filename"];
    // Tried with (NSString *)kUTTypeVCard as well, same results
    NSLog(@"afile = %@", afile);
    // What do I do with this result?
    NSString *card = [NSString stringWithContentsOfFile:afile];
    NSLog(@"card = %@", card);

    NSFileWrapper *wrapper = [pb readFileWrapper];
    NSLog(@"wrapper = %@", wrapper);

    return YES;
}

Results:

2012-02-05 12:36:58.419 MyAppName[3650:60b] fileNames = (null)
2012-02-05 12:36:58.419 MyAppName[3650:60b] afile = random_filename
2012-02-05 12:36:58.419 MyAppName[3650:60b] card = (null)
2012-02-05 12:36:58.420 MyAppName[3650:60b] wrapper = (null)

I run also into this issue. Summary for readers.

On 10.7 register for NSFilesPromisePboardType and kUTTypeVCard. Process NSFilesPromisePboardType.

On 10.8+ register for kUTTypeVCard and process kUTTypeVCard

You are right, the AddressBook changed its Pasteboard behavior from former OS versions to Lion. Formerly the vCard data were directly stored in the pasteboard with type NSVCardPboardType. But also in Lion you can find all informations to read a dropped vCard.

There are two ways to deal with a pasteboard. The 'direct' way and a 'dialog' way. (My words, not Apple's). In the direct way all data are put to the pasteboard, where the receiver finds them and can read them. Sometimes it is very time and space consuming, when the data are big as is very often with images or long text (RTFD with images). Therefore another way exists, which needs a small dialog between sender and receiver. The sender (of the drag operation) puts (nearly) no data to the pasteboard but promises to store the data somewhere in the filesystem. The receiver (of the drag operation) has to tell where this place (a folder) should be. The answer from the sender is then the name of the newly created file(s) so the receiver can read the data from this file. All this should be done in the method -(BOOL)performDragOperation:(id <NSDraggingInfo>)sender . But if the drop is done over a tableView, outLineView, browser etc. you need to implement another method. Search for acceptDrop: in the documentation. Essentially they do all the same.

In your case (drop over a tableView) the following (pseudo-)code will (I hope so) do the job:

- (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id < NSDraggingInfo >)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)operation
{
    NSPasteBoard *pb = [info draggingPasteboard];

    NSArray *types = pb.types;

    if( ([types containsObject: NSVCardPboardType]) && *data_exist for this type* ){
       // read the data directly from here
       . . . . 
       return YES;
    }

    if( [types containsObject:NSFilesPromisePboardType] ){
      // create a place for the promised data
      NSString *destPath = NSTemporaryDirectory();
      NSURL *destURL = [NSURL fileURLWithPath:destPath isDirectory:YES];
      // let the sender know it
      // the sender answers with filenames (without the path)
      NSArray *fileNames = [sender namesOfPromisedFilesDroppedAtDestination:destURL];
      if( fileNames == nil ) return NO;  // Firefox likes to return nothing
      NSString *resultStr = [destPath stringByAppendingPathComponent:[fileNames objectAtIndex:0]];
      // read the vCard data from here
         . . . . . 
      return YES;
    }
    // search for other pbTypes or just return  without success
    return NO;
}

As you can see in your log of all types on the pasteBoard, the type NSFilesPromisePboardType (the internal name is Apple files promise pasteboard type ) has a nonempty propertyList which simply says that data are of type vcf (file suffix). To use this pbType is the new way AddressBook works. This is btw the preferred way to send data to the finder.

I do not know why you had no success, but I have a question: Did you register your tableView for NSFilesPromisePboardType ? Otherwise it doesn't work! I will now show a code snippet from one of my apps which is tested and which works:

- (BOOL)  tableView:(NSTableView *)aTableView
         acceptDrop:(id <NSDraggingInfo>)info
                row:(NSInteger)row
      dropOperation:(NSTableViewDropOperation)operation
{
    NSPasteboard *pboard = [info draggingPasteboard];
    NSArray *types = pboard.types;

    if( [types containsObject:NSFilesPromisePboardType] ){
        NSString *destPath = NSTemporaryDirectory();  // or whatever you want
        NSURL *destURL = [NSURL fileURLWithPath:destPath isDirectory:YES];
        NSLog(@"destURL %@", destURL);
        NSArray *fileNames = [info namesOfPromisedFilesDroppedAtDestination:destURL];
        NSLog(@"fileNames %@", fileNames);
        if( fileNames == nil ) return NO;
        NSString *resultPath = [destPath stringByAppendingPathComponent:[fileNames objectAtIndex:0]];
        NSLog(@"resultPath %@", resultPath);
        // read the vCard data from here
        NSString *vCard = [NSString stringWithContentsOfFile:resultPath
                                                    encoding:NSASCIIStringEncoding
                                                       error:NULL];
        NSLog(@"vCard =\n%@", vCard);
        return YES;
    }
    return NO;
}

The test output is;

destURL file://localhost/var/folders/j2/78r9kp_d08gc52z04w2dwkv80000gn/T/
fileNames (
    "Heinrich Giesen 2.vcf"
)
resultPath /var/folders/j2/78r9kp_d08gc52z04w2dwkv80000gn/T/Heinrich Giesen 2.vcf
vCard =
BEGIN:VCAR
VERSION:3.0

. . . and so on. ending with

END:VCARD

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM