Category: iOS

iOS (formerly iPhone OS) is a mobile operating system created and developed by Apple Inc. exclusively for its hardware. It is the operating system that powers many of the company’s mobile devices, including the iPhone and iPod Touch; the term also included the versions running on iPads until the name iPadOS was introduced with version 13 in 2019. It is the world’s second-most widely installed mobile operating system, after Android. It is the basis for three other operating systems made by Apple: iPadOS, tvOS, and watchOS. It is proprietary software, although some parts of it are open source under the Apple Public Source License and other licenses.

  • SwiftUI Know When Entering Landscape Mode

    SwiftUI Know When Entering Landscape Mode

    Create a ObservableObject Object like this

    final class UserAndDeviceSettings: ObservableObject {
        @Published var userInterfaceOrientationChanges:Bool = false
        @Published var userInterfaceOrientationLandscape:Bool = false
    }

    Now go to your SceneDelegate and add a var on top and pass your settings var in UIHostingController in willConnectTo method.

    var settings = UserAndDeviceSettings()
    
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
            // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
            // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
            // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    
            // Get the managed object context from the shared persistent container
            let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    
            // Create the SwiftUI view and set the context as the value for the managedObjectContext environment keyPath
            let contentView = ContentView().environment(\.managedObjectContext, context)
    
            // Use a UIHostingController as window root view controller.
            if let windowScene = scene as? UIWindowScene {
                let window = UIWindow(windowScene: windowScene)
                
                // read the initial device orientation here
                self.settings.userInterfaceOrientationLandscape = (windowScene.interfaceOrientation.isLandscape == true)
                
                window.rootViewController = UIHostingController(rootView: contentView.environmentObject(self.settings))
                self.window = window
                window.makeKeyAndVisible()
            }
        }

    and in didUpdate delegate method, add this code.

    
    func windowScene(_ windowScene: UIWindowScene, didUpdate previousCoordinateSpace: UICoordinateSpace, interfaceOrientation previousInterfaceOrientation: UIInterfaceOrientation, traitCollection previousTraitCollection: UITraitCollection) {
            print(">> previous tratis  \(previousTraitCollection)")
            print(">> previous coordinateSpace \(previousCoordinateSpace)")
            print(">> previous orientation \(previousInterfaceOrientation)")
            
            self.settings.userInterfaceOrientationChanges = true
            
            if previousInterfaceOrientation == .portrait || previousInterfaceOrientation == .portraitUpsideDown {
                self.settings.userInterfaceOrientationLandscape = true
            } else {
                self.settings.userInterfaceOrientationLandscape = false
            }
        }

    Now you can use this @EnvironmentObject in any View

    @EnvironmentObject var settings:UserAndDeviceSettings
    
    if self.settings.userInterfaceOrientationLandscape {
        
    }
    Spread the love
  • How top open Facebook group page in Facebook app on iPhone using Swift and Xcode.

    Opening a Facebook group page in the Facebook app on the iPhone using Swift is really easy. I’m using Swift 5 and Xcode 11.6.

    You must need your Facebook Page integer id which looks like this 8794208798015.

    let fbPage = "fb://profile/" + FB_GROUP_ID // Your Facebook group id e.g 8794808798015
    let fbURL = URL(string:fbPage)
    let canOpenURL = UIApplication.shared.canOpenURL(fbURL!)
    if (canOpenURL) {
        UIApplication.shared.open(fbURL!, completionHandler: nil)
    }

    Also update your Info.plist file otherwise you will get error like this

    This app is not allowed to query for scheme
    Spread the love
  • FSCalendar Dark Mode issue for title color

    FSCalendar is a very good calendar library in Swift but it does not support dark mode, I’m using version 2.8.1.

    We can easily change all date color by one of its delegate’s FSCalendarDelegateAppearance func.

    func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, titleDefaultColorFor date: Date) -> UIColor? {
            
            let defaultColor = appearance.titleDefaultColor
            
            if #available(iOS 12.0, *) {
                if self.traitCollection.userInterfaceStyle == .dark {
                    return .orange
                } else {
                    return defaultColor
                }
            } else {
                return defaultColor
            }
            
        }

    Also, don’t forget to reload the calendar data

    override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
            self.calendar?.reloadData()
        }
    Spread the love