【问题标题】:IOS Targets for each client每个客户端的 IOS 目标
【发布时间】:2018-07-24 06:06:23
【问题描述】:

一点背景:

在android中我们开发了同样的应用程序,基本上我们是先开发了Android应用程序,现在我们已经创建了它的IOS版本,所以这个应用程序有多个客户端。在 android 中,我们确实使用 Android 模块系统来处理这种情况。

现在在 IOS 中我们必须做同样的事情。那就是使相同的代码库用于多个客户端。

我做了研究,发现目标和框架是最好的选择。我创建了工作区,添加了框架并添加了项目。但这一切都无缘无故地陷入困境。

所以我决定转向目标。所以我读了它,它很容易理解,但它可以满足我的以下要求

  1. 为每个目标单独的 swift 文件。 (我有一个包含 URL 链接和其他内容的通用文件,每个客户端需要的内容不同,所以我可以为每个客户端制作相同的文件并更改 url 和其他内容
  2. 应用图标(每个目标必须有单独的图标)
  3. 颜色关闭应用程序(我想为每个客户提供不同的颜色。这里我真的不知道该怎么做,因为我已经使用 IB 给应用程序颜色)
  4. FCM 及其文件“GoogleService-Info.plist”(应如何管理多个目标)

更新:这就是我在 App Delegate 中编码的方式

    import Firebase
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    let gcmMessageIDKey = "gcm.message_id"

    func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        FirebaseApp.configure()

        // [START set_messaging_delegate]
        Messaging.messaging().delegate = self
        // [END set_messaging_delegate]
        // Register for remote notifications. This shows a permission dialog on first run, to
        // show the dialog at a more appropriate time move this registration accordingly.
        // [START register_for_notifications]
        if #available(iOS 10.0, *) {
            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.current().delegate = self

            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: {_, _ in })
        } else {
            let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }

        application.registerForRemoteNotifications()

        // [END register_for_notifications]
        return true
    }

    // [START receive_message]
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
        // If you are receiving a notification message while your app is in the background,
        // this callback will not be fired till the user taps on the notification launching the application.
        // TODO: Handle data of notification
        // With swizzling disabled you must let Messaging know about the message, for Analytics
        // Messaging.messaging().appDidReceiveMessage(userInfo)
        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }

        // Print full message.
        print(userInfo)
    }

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        // If you are receiving a notification message while your app is in the background,
        // this callback will not be fired till the user taps on the notification launching the application.
        // TODO: Handle data of notification
        // With swizzling disabled you must let Messaging know about the message, for Analytics
        // Messaging.messaging().appDidReceiveMessage(userInfo)
        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }

        // Print full message.
        print(userInfo)

        completionHandler(UIBackgroundFetchResult.newData)
    }

请说明使用“目标”是否可以实现以及如何实现?

【问题讨论】:

  • 它是 iOS 而不是 IOS。 IOS 是 Cisco 使用的术语。是的,目标是做到这一点的正确方法。
  • @adev 谢谢,下次我会小心的

标签: ios swift4 xcode9


【解决方案1】:

1. 创建目标时,在此之前添加的所有文件都将可用于新创建的目标。然后,对于您添加的每个新文件,您都可以选择该文件的目标。您可以在文件检查器的目标成员资格部分更改该首选项。

2.您可以为不同的目标设置不同的应用图标

  1. 在资产中创建不同的应用图标
  2. 然后在target -> App icon部分设置新创建的应用图标。

3.不同颜色:

您可以为每个目标设置编译标志

在 Swift Compiler 下的目标构建设置中找到 Swift 编译器标志 — Custom Flags → Other Swift Flags

在其他 swift 标志中创建标志将有助于区分目标。 然后根据值给出这样的颜色

#if TARGET1

let fillColor       = UIColor(red: 30/255.0, green: 100/255.0, blue: 100/255.0, alpha: 1.0)
let fillColorWithAlpha  = UIColor(red: 30/255.0, green: 100/255.0, blue: 100/255.0, alpha: 0.9)

let buttonColor     = UIColor(red: 15/255.0, green: 45/255.0, blue: 90/255.0, alpha: 1.0)
let buttonColorDeselected = UIColor(red: 100/255, green: 100/255, blue: 100/255, alpha: 1.0)



#else
let fillColor       = UIColor(red: 74/255.0, green: 144/255.0, blue: 226/255.0, alpha: 1.0)
let fillColorWithAlpha       = UIColor(red: 74/255.0, green: 144/255.0, blue: 226/255.0, alpha: 0.9)

let buttonColor     = UIColor(red: 36/255.0, green: 97/255.0, blue: 168/255.0, alpha: 1.0)
let buttonColorDeselected = UIColor(red: 116/255, green: 116/255, blue: 116/255, alpha: 1.0)



#endif 

其中“TARGET1”是您在 swift flag 部分中给出的值。

最后使用您配置的填充颜色来更改颜色。例如,将 UIButton 的背景颜色设置为该颜色 -> 创建自定义类并提供您为不同目标配置的颜色。通过这样做,您可以只更改配置中的颜色,而不是更新整个 UI

class CustomThemeButton: UIButton {


    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setBackGroundColor()
    }

    required override init(frame: CGRect) {
        super.init(frame: frame)
        setBackGroundColor()
    }

    private func setBackGroundColor(){

        self.backgroundColor = fillColor
    }

}

【讨论】:

  • Google 创建的 FCM plist 文件怎么样?如何为每个客户端虎钳添加多个文件,(根据客户端/目标,我将更改其包名称)
  • 您创建的每个目标都有自己的 info.plist 文件,您将在其中设置捆绑 ID。您可以添加不同的 google plist ,每个 google plist 都会关联到特定的 bundle id。
  • @Androidteem 我认为在添加 google plist 时,您可以指定目标。
  • 好的,但我仍然无法理解这个“首先在其他快速标志中创建标志以区分目标。然后根据值给出这样的颜色”和您作为示例显示的文件,你没有告诉我在哪里添加以及如何处理
  • 请把我当成 iOS 笨蛋
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多