iOS Action Extension Sample Code Not Working

前端 未结 2 405
难免孤独
难免孤独 2021-01-25 05:52

The iOS Action Extension sample code does not seem to work.

  1. In xCode, I create a new Swift project and choose Single View Application.

  2. Then I se

2条回答
  •  太阳男子
    2021-01-25 06:45

    There's a bug in the Swift version of Xcode's project template for this kind of extension, apparently because of an error when someone was converting from Objective-C to Swift.

    To make a long story short: the line that reads "found = true" is in the wrong place. This causes the method doneWithResults to be called twice when it should only be called once. On the first call it sets self.extensionContext = nil. On the second call it tries to use self.extensionContext and throws an exception due to unwrapping a nil optional. But the exception message gets swallowed by the extension system, so there are no clues.

    If you change this code in the project template:

    itemProvider.loadItemForTypeIdentifier(String(kUTTypePropertyList), options: nil, completionHandler: { (item, error) in
        let dictionary = item as! [String: AnyObject]
        NSOperationQueue.mainQueue().addOperationWithBlock {
            self.itemLoadCompletedWithPreprocessingResults(dictionary[NSExtensionJavaScriptPreprocessingResultsKey] as! [NSObject: AnyObject])
        }
        found = true
    })
    

    to look like this:

    itemProvider.loadItemForTypeIdentifier(String(kUTTypePropertyList), options: nil, completionHandler: { (item, error) in
        let dictionary = item as! [String: AnyObject]
        NSOperationQueue.mainQueue().addOperationWithBlock {
            self.itemLoadCompletedWithPreprocessingResults(dictionary[NSExtensionJavaScriptPreprocessingResultsKey] as! [NSObject: AnyObject])
        }
    })
    found = true
    

    ...then it works as expected.

    I filed the bug rdar://22482042 about this and I encourage you to file one as well.

提交回复
热议问题