gpt4 book ai didi

ios - 使用 UIActivityViewController 向特定电话号码发送短信

转载 作者:太空狗 更新时间:2023-10-30 03:45:27 29 4
gpt4 key购买 nike

我正在使用 UIActivityViewController 邀请联系人使用我的应用。我只需要分享一段特定的文字。它适用于所有共享选项,但是当我尝试发送短信时,我找不到以编程方式添加特定联系人的选项,因为我已经有了该联系人的电话号码。

我当前的代码是这样的——

if(NSClassFromString(@"UIActivityViewController")) {
NSString *message = @"Hi,\nCheckout My App, a new messenger.";

UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:@[message] applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];
}

我尝试子类化 UIActivityItemProvider,但它基本上允许我向不同的提供商发送不同的消息,但我不希望那样。

有没有办法选择特定联系人或向 UIActivityViewController 提供电话号码?

最佳答案

我创建了 UIActivityViewController 的子类,它可以在 Swift 和 Objective-C 中正常工作:

import UIKit
import MessageUI

@objcMembers public class PhoneNumberActivityItem: NSObject {
let phoneNumber: String

init(phoneNumber: String) {
self.phoneNumber = phoneNumber
}
}

@objcMembers public class ExtendedActivityViewController: UIActivityViewController, MFMessageComposeViewControllerDelegate {

public override init(activityItems: [Any], applicationActivities: [UIActivity]?) {
let phoneNumberActivityItems = activityItems.filter({ $0 is PhoneNumberActivityItem }) as! [PhoneNumberActivityItem]

guard MFMessageComposeViewController.canSendText(), !phoneNumberActivityItems.isEmpty else {
super.init(activityItems: activityItems, applicationActivities: applicationActivities)
return
}

var activities = [UIActivity]()
let phoneNumbers: [String] = phoneNumberActivityItems.map { $0.phoneNumber }

let extendedMessageActivity = ExtendedMessageActivity(phoneNumbers: phoneNumbers)
activities.append(extendedMessageActivity)

if let otherActivities = applicationActivities {
activities.append(contentsOf: otherActivities)
}

super.init(activityItems: activityItems, applicationActivities: activities)

extendedMessageActivity.messageComposeDelegate = self
self.excludedActivityTypes = [.message]
}

public func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
controller.dismiss(animated: false, completion: nil)
}

}

fileprivate extension UIActivity.ActivityType {
static let extendedMessage = UIActivity.ActivityType("ExtendedMessage")
}

fileprivate class ExtendedMessageActivity: UIActivity {

private let phoneNumbers: [String]
private var message: String?

weak var messageComposeDelegate: MFMessageComposeViewControllerDelegate?

init(phoneNumbers: [String]) {
self.phoneNumbers = phoneNumbers
super.init()
}

override static var activityCategory: UIActivity.Category {
return .share
}

override var activityType: UIActivity.ActivityType? {
return .extendedMessage
}

override var activityTitle: String? {
return NSLocalizedString("Message", comment: "")
}

override var activityImage: UIImage? {
return UIImage(named: "message-app-icon")
}

override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
for case is String in activityItems {
return true
}
return false
}

override func prepare(withActivityItems activityItems: [Any]) {
for case let text as String in activityItems {
print(text)
self.message = text
}
}

override func perform() {
let messageComposeVC = MFMessageComposeViewController()
messageComposeVC.body = message
messageComposeVC.recipients = phoneNumbers
messageComposeVC.messageComposeDelegate = messageComposeDelegate
UIApplication.getTopViewController()?.present(messageComposeVC, animated: true, completion: nil)
}

}

fileprivate extension UIApplication {

class func getTopViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return getTopViewController(base: nav.visibleViewController)
}
else if let tab = base as? UITabBarController, let selected = tab.selectedViewController {
return getTopViewController(base: selected)
}
else if let presented = base?.presentedViewController {
return getTopViewController(base: presented)
}
return base
}

}

快速用法:

let activityItems: [Any] = ["Message Text", PhoneNumberActivityItem(phoneNumber: "+48 536 754 038")]
let extendedActivityVC = ExtendedActivityViewController(activityItems: activityItems, applicationActivities: nil)
extendedActivityVC.popoverPresentationController?.sourceView = self.view // to prevent iPad app crashes
self.present(extendedActivityVC, animated: true, completion: nil)

Objective-C 用法:

NSArray *activityItems = [[NSArray alloc] initWithObjects:@"Some Message Text", [[PhoneNumberActivityItem alloc] initWithPhoneNumber:@"+48 123 456 789"], nil];
ExtendedActivityViewController *extendedActivityVC = [[ExtendedActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
extendedActivityVC.popoverPresentationController.sourceView = self.view; // to prevent iPad app crashes
[self presentViewController:extendedActivityVC animated:true completion:nil];

您将需要“消息应用程序图标”: 60x60 120x120 180x180

关于ios - 使用 UIActivityViewController 向特定电话号码发送短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35135327/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com