我希望你今天过得好!
我正在尝试创建一个非常简单的应用程序,它具有较小的社交扩展。 当用户将音频消息分享到我的应用程序时,我希望我的应用程序检测它是否是音频。它只适用于音频附件,但我无法实现。 例如,如果我将'kUTTypeAudio'更改为'kUTTypeImage'并且我正在尝试将图像共享到我的应用程序 - 它可以正常工作。它会检测图像类型,但是使用音频消息它不起作用。 在我的应用程序中,我不需要带有发布按钮和所有内容的ShareView UI。我这就是我取消View的原因,只是向用户显示一条消息并提示消息。
我的问题是: 1.如何检测来自Whatsapp的音频消息? 2.接下来,如何使用此音频消息?我需要将它变为变量然后满足我的需求或者什么? 谢谢你们!
这是我的ShareViewController.swift:
//
// ShareViewController.swift
// Get Message
//
// Created by Tom Luz on 12/11/2017.
// Copyright © 2017 Tom Luz. All rights reserved.
//
import UIKit
import Social
import MobileCoreServices
class ShareViewController: SLComposeServiceViewController {
override func isContentValid() - > Bool {
// Do validation of contentText and/or NSExtensionContext attachments here
return false
}
override func didSelectPost() {
// This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.
// Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context.
self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
}
override func configurationItems() - > [Any] !{
// To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
return []
}
override func viewDidAppear(_ animated: Bool) {
let contentType = kUTTypeAudio as String
if let content = extensionContext ? .inputItems.first as ? NSExtensionItem,
let contentProvider = content.attachments ? .first as ? NSItemProvider,
contentProvider.hasItemConformingToTypeIdentifier(contentType) {
contentProvider.loadItem(forTypeIdentifier: contentType, options: nil) {
(data, error) in
let data = data as ? String
let alertController = UIAlertController(title: "Words Are Better", message: data, preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: self.cancelShareExtension))
self.present(alertController, animated: true, completion: nil)
}
}
else {
let alertController = UIAlertController(title: "Words Are Better", message: "Not OK", preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: self.cancelShareExtension))
self.present(alertController, animated: true, completion: nil)
}
}
func cancelShareExtension(alert: UIAlertAction) {
cancel()
}
}