Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

MacOs sandbox, security scoped bookmarks



  • Hi,

    I struggle writing a code related to MacOs security scoped bookmarks (related to MacOs sandboxing, where to keep access to user selected files across App runs one have to facilitate Security Scoped Bookmarks). Anyone having an experience on that matter or point to some links with examples?

    Env: Qt 5.10, MacOs High Sierra

    Plamen


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Try something like:

    QUrl url("file:///Users/your_user/your_file.extension");
    NSURL *nsUrl = url.toNSURL();
    NSError *error = nil;
    NSData *bookmark = [nsUrl bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope
                                                      includingResourceValuesForKeys:nil
                                                      relativeToURL:nil
                                                      error:&error];
    qDebug() << "Error" << error;
    qDebug() << QByteArray::fromNSData(bookmark);
    

    Hope it helps



  • @SGaist
    Is this code can compile in *.cpp ?
    I want to add them into my code but occurs some error...

    #if defined(Q_OS_MACOS)
    QUrl url = QUrl::fromLocalFile(selectFolder);
    qInfo() << "Get url: " << url;

    NSURL *nsUrl = url.toNSURL();
    NSError *error = nil; // Unknown type name 'NSError'
    NSData *bookmark = [nsUrl bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope
                                                      includingResourceValuesForKeys:nil
                                                      relativeToURL:nil
                                                      error:&error];
    // Expected ',' or ']' in lambda capture list
    qDebug() << "Error" << error;
    qDebug() << QByteArray::fromNSData(bookmark);
    

    #endif



  • @YuChao-Hsu
    That's Objective C++.
    The code need to be in a *.mm file.