5

我有一个用 Swift 编写的 iOS 共享扩展。

这是我第一次使用 Swift,我遇到了在操作完成之前关闭共享扩展的问题。此共享扩展将音频文件上传到 API。音频文件在发送到服务器之前必须是 base64。似乎它适用于小于 30 秒左右的音频文件,但一旦它们变大,共享扩展似乎会关闭代码并且它永远不会运行。

下面是代码:

    override func didSelectPost() {
    let defaults = NSUserDefaults(suiteName: suiteName)
    var authorization: String? = defaults!.stringForKey("Authorization")
    authorization = authorization!.stringByReplacingOccurrencesOfString("\"", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)

    if let content = extensionContext!.inputItems[0] as? NSExtensionItem {
        let contentType = kUTTypeAudio as String

        if let contents = content.attachments as? [NSItemProvider] {
            for attachment in contents {
                attachment.loadItemForTypeIdentifier(contentType, options: nil) { data, error in
                    let url = data as! NSURL

                    do {

                        let audioData = try NSData(contentsOfURL: url, options: NSDataReadingOptions())
                        let base64Audio = audioData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
                        print(base64Audio)


                        let sessionConfig = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(self.suiteNameSessionConfig)
                        // Extensions aren't allowed their own cache disk space. Need to share with application
                        sessionConfig.sharedContainerIdentifier = self.suiteName
                        let session = NSURLSession(configuration: sessionConfig)

                        let url = NSURL(string: "https://api.example.com/upload")
                        let request = NSMutableURLRequest(URL: url!)
                        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
                        request.setValue("application/json", forHTTPHeaderField: "Accept")
                        request.setValue(authorization!, forHTTPHeaderField: "Authorization")

                        request.HTTPMethod = "POST"

                        let jsonObject:[String: AnyObject] = [ "recording": [ "file": "data:audio/x-m4a;base64," + base64Audio] ]


                        var jsonError: NSError?
                        do {
                            let jsonData = try NSJSONSerialization.dataWithJSONObject(jsonObject, options:[])
                            request.HTTPBody = jsonData
                        } catch {
                            request.HTTPBody = nil
                        }


                        let task = session.dataTaskWithRequest(request)
                        task.resume()


                        self.extensionContext!.completeRequestReturningItems([], completionHandler: nil)
                    } catch {
                        print(error)
                        self.extensionContext!.completeRequestReturningItems([], completionHandler: nil)
                    }
                }
            }

        } else {
            self.extensionContext!.completeRequestReturningItems([], completionHandler: nil)
        }
    } else {
        self.extensionContext!.completeRequestReturningItems([], completionHandler: nil)
    }
}
4

0 回答 0