使用UIActivityViewController

时间:2016-02-01 16:22:29

标签: ios objective-c uiactivityviewcontroller

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

我目前的代码如下所示 -

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的电话号码?

1 个答案:

答案 0 :(得分:1)

我创建了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];

您将需要“ message-app-icon”: 60x60 120x120 180x180