How to Fetch the Inbox from Email Agent in Swift/Objective C Using Mailcore Framework or Any Framework

How to open mail panel in Swift? Mac OS

There are two ways to do that:

  1. AppleScript framework

  2. NSSharingService

NSSharingService to compose mail:

let service = NSSharingService(named: NSSharingServiceNameComposeEmail)

service?.recipients = ["test@gmail.com"]
service?.subject = "Test Mail"
service?.performWithItems(["Test Mail body"])

Launch Mail App

If you just want to open Mail app, try below code:

NSWorkspace.sharedWorkspace().launchApplication("Mail")

Improper Setting:

  • If account is not configured on the system, this code will show configuration window for mail
  • When user will try to send mail it'll be shown improper setting related error by Mail app so you need not to bother about it
  • If you want to use other then Mail app, just try your own mail system, native mail client or third party mail framework.

Try from terminal:

You can use shell script to send mail using native client

mail -s "Hello" "test@gmail.com" <<EOF
Hello, Test!
EOF

Detect current device with UI_USER_INTERFACE_IDIOM() in Swift

When working with Swift, you can use the enum UIUserInterfaceIdiom, defined as:

enum UIUserInterfaceIdiom : Int {
case unspecified

case phone // iPhone and iPod touch style UI
case pad // iPad style UI (also includes macOS Catalyst)
}

So you can use it as:

UIDevice.current.userInterfaceIdiom == .pad
UIDevice.current.userInterfaceIdiom == .phone
UIDevice.current.userInterfaceIdiom == .unspecified

Or with a Switch statement:

    switch UIDevice.current.userInterfaceIdiom {
case .phone:
// It's an iPhone
case .pad:
// It's an iPad (or macOS Catalyst)

@unknown default:
// Uh, oh! What could it be?
}

UI_USER_INTERFACE_IDIOM() is an Objective-C macro, which is defined as:

#define UI_USER_INTERFACE_IDIOM() \ ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? \ [[UIDevice currentDevice] userInterfaceIdiom] : \ UIUserInterfaceIdiomPhone)

Also, note that even when working with Objective-C, the UI_USER_INTERFACE_IDIOM() macro is only required when targeting iOS 3.2 and below. When deploying to iOS 3.2 and up, you can use [UIDevice userInterfaceIdiom] directly.



Related Topics



Leave a reply



Submit