美文网首页iOS开发基础应用
Xcode同时构建iOS/ipadOS/macOS项目

Xcode同时构建iOS/ipadOS/macOS项目

作者: Felix_ | 来源:发表于2019-10-09 10:50 被阅读0次
    苹果于2019年10月8日发布了macOS Catalina,在此系统中,使得同时开发适合于iOS,ipadOS,macOS的应用程序APP成为可能(UWP骂骂咧咧的退出群聊),昨天,辛辛苦苦了一整天,将环境由macOS Mojve更新到了masOS CatalinaXcode 10更新到了Xcode 11,也把ipad更新到了新的ipadOS。接下来,就开始尝试构建一个适用于三端的App运行一下,当然,有兴趣的也可以把现有的项目做些修改,打造成一个macOS的App。

    环境介绍:

    系统版本: macOS Catalina
    Xcode版本: 11.1
    iOS模拟器: 13.1

    话不多说,打开Xcode直接开始像以前一样创建项目:


    接下来,选择Single View App

    创建一个名为CSApp的项目,语言选择SwiftUser Interface选择Storyboard,显然SwiftUI也很有诱惑力,但是暂时不在本节的讨论范围内

    创建完项目后,这里勾上Mac

    现在看下目录结构,和以前的目录结构稍有不同,除了AppDelegateViewController,多出了一个SceneDelegate,这是iPadOS带来的新的多窗口支持的结果,并且有效地将应用程序委托的工作分成两部分

    这里我们不再使用原本的Main.storyboard来做UI,所以删除``Main.storyboard文件,并且在设置中移除Main![](https://img.haomeiwen.com/i2761682/d9415f273b518b40.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 在AppDelagate```中可以看到如下代码
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            
            return true
        }
        func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
            return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
        }
        func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
            
        }
    }
    

    UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)我们可以看到,加载了一个名为Default Configuration的场景设置,转到Info.plist下查看,多出了一个程序的场景清单


    展开后,可以看到这里配置了名为Default Configuration的场景,默认使用了Main.storyboard,这里我们删除Storyboard Name这一项,改为使用自己的逻辑创建

    删除后如图

    然后,在SceneDelegate里的func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions)方法中写入以下代码
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
            let ws = scene as? UIWindowScene
            if ws != nil{
                window = UIWindow(windowScene: ws!)
                window?.rootViewController = ViewController()
                window?.makeKeyAndVisible()
            }else{
                return
            }
        }
    

    在这里去设置程序的根控制器,然后,改变以下ViewController的背景颜色

    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.view.backgroundColor = UIColor.cyan
        }
    }
    

    在运行平台上选择My Mac,然后运行项目,结果如图


    我们尝试以开发移动端App的逻辑代码来观察一下,首先创建一个BViewController,并设置背景颜色为橘色
    class BViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.view.backgroundColor = UIColor.orange
        }
    }
    

    现在,各给ViewControllerBViewController增加一个按钮,点击逻辑如下

    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.view.backgroundColor = UIColor.cyan
            
            let btn:UIButton = UIButton(frame: CGRect(x: 50, y: 50, width: 100, height: 40))
            btn.setTitle("GoTo B", for: .normal)
            btn.backgroundColor = UIColor.lightGray
            btn.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
            self.view.addSubview(btn)
        }
    
        @objc func btnClick(){
            let bvc:BViewController = BViewController()
            bvc.modalPresentationStyle = .fullScreen
            self.present(bvc, animated: true, completion: nil)
        }
    
    }
    
    class BViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.view.backgroundColor = UIColor.orange
            
            let btn:UIButton = UIButton(frame: CGRect(x: 50, y: 50, width: 100, height: 40))
            btn.setTitle("GoTo A", for: .normal)
            btn.backgroundColor = UIColor.lightGray
            btn.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
            self.view.addSubview(btn)
        }
    
        @objc func btnClick(){
            self.dismiss(animated: true, completion: nil)
        }
    }
    

    逻辑很简单,在每个页面上增加一个按钮,ViewController点击按钮跳转到BViewControllerBViewController点击按钮退出当前控制器回到ViewController,先在Mac上运行查看下结果


    然后在iphone11模拟器上看下效果

    ipad上就不测试了,和iphone上一样的效果。先在,就可以动手开发可以同时运行在三端设备上的原生App了,也可以稍加修改,把以前的项目迁移到Mac上了。

    以上内容转载请注明出处,同时也请大家不吝你的关注和下面的赞赏
    ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

    相关文章

      网友评论

        本文标题:Xcode同时构建iOS/ipadOS/macOS项目

        本文链接:https://www.haomeiwen.com/subject/fkywpctx.html