Safari toolbar's popover not showing off

First, many thanks for your time.


I'm developing a Safari extension with a toolbar icon. By clicking on the toolbar icon a small popover is shown and the user can set Safari's app extension preferences. I just used the projects defaults, so I have, in the extension:


  • SafariExtensionHandler.swift
  • SFSafariExtensionViewController.swift
  • SafariExtensionViewController.xib --> contains the popover layout


So far so good. My machine runs macOS High Sierra. In SafariExtensionHandler.swift I have:


    override func popoverViewController() -> SFSafariExtensionViewController {
        return SafariExtensionViewController.shared
    }


also in SFSafariToolbarItem, Action is Popover.


So when I click on the toolbar item, I get the SafariExtensionViewController.xib layout,

but not when I distribute to my clients, they get a functional extension, but they only see an empty popover (macOS High Sierra).


I'm using a Cocoa project with a Safari Extension attached to it and swift programming language.


Inside the SafariExtensionHandler I am overriding the viewWillAppear function, to populate correctly the settings


    override func viewWillAppear() {
        super.viewWillAppear()
        // settings are here
    }

Any clue? Is any additional call required in popoverViewController to ensure the xib controller is shown?

Replies

Update: after latest MacOS update I'm getting the same issue as well. When I run the extension I have this error in the error log:


2017-12-22 15:37:07.320567+0100 Extension[4524:479851] <NSViewServiceMarshal: 0x7ffc3801c1c0> implicit request for resize of <NSWindow: 0x6000001f4d00> to {0, 0} was not granted as-is; set breakpoint on -[NSViewServiceMarshal implicitResize:ofWindow:] to catch this


I suspect some problem with the resize of the Popover. Investigating further.


Nailed it! You have to add:


    override func viewDidLoad() {
      
        self.preferredContentSize = NSMakeSize(self.view.frame.size.width, self.view.frame.size.height);
    }


in SafariExtensionViewController.swift!

If I'm reading this correctly, the popover is showing, but none of the content is visible, is that correct?


One thing you could try is setting a preferred content size of your SafariExtensionViewController when it is created.


https://developer.apple.com/documentation/appkit/nsviewcontroller/1434409-preferredcontentsize

any news on the subject?

Did setting self.preferredContentSize (as frnkundrwdsuggested) help?


This should be automatically handled for you if you create a new Safari App Extension using Xcode 10 Betas.

Does anybody have a solution? I can't get the popover working. I get this in the debugger: The extension's toolbar item was clicked but that's it. I have set permission in the plist file to "All" and i run on xcode 10 non beta.


- (void)messageReceivedWithName:(NSString *)messageName fromPage:(SFSafariPage *)page userInfo:(NSDictionary *)userInfo {
    // This method will be called when a content script provided by your extension calls safari.extension.dispatchMessage("message").
    [page getPagePropertiesWithCompletionHandler:^(SFSafariPageProperties *properties) {
        NSLog(@"The extension received a message (%@) from a script injected into (%@) with userInfo (%@)", messageName, properties.url, userInfo);
    }];
}

- (void)toolbarItemClickedInWindow:(SFSafariWindow *)window {
    // This method will be called when your toolbar item is clicked.
    NSLog(@"The extension's toolbar item was clicked");
}

- (void)validateToolbarItemInWindow:(SFSafariWindow *)window validationHandler:(void (^)(BOOL enabled, NSString *badgeText))validationHandler {
    // This method will be called whenever some state changes in the passed in window. You should use this as a chance to enable or disable your toolbar item and set badge text.
    validationHandler(YES, nil);
}

- (SFSafariExtensionViewController *)popoverViewController {
    return [SafariExtensionViewController sharedController];
}

If toolbarItemClickedInWindow is being called in your extension, it sounds like you have your toolbar item set to perform a command instead of showing a popover.


In your extension's Info.plist - in the SFSafariToolbarItem section, you should see Action => Command. If you change that to Action => Popover, then your extension will start to display the popover.