How to open mail panel in Swift? Mac OS
There are two ways to do that:
AppleScript
frameworkNSSharingService
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
Make a Uiwebview as Height as Its Content with Auto Layout
Pausing Spritekit Game Using Appdelegate in iOS8
Creating Tableview Sections from JSON Data Swift 4
Setting Uiimageview Image Affects Layout Constraints
Swiftui Multiple Navigationlinks in Form/Sheet - Entry Stays Highlighted
Locationmanager Didupdatelocations Fires Twice on Device, Only Once on Simulator
Read Uiapplicationlaunchoptionsurlkey in Swift
App Crash on Device But Works on Simulator iOS
How to Check If Device Orientation Is Landscape Left or Right in Swift
How to Give Cornerradius for Uibezierpath
Rotate a Imageview Around a Pivot Point in iOS
Swift: Load Custom Uiview from Xib Programmatically
How to Convert an Int into Nsdata in Swift
What am I Doing Wrong with Allowsfractionalunits on Nsdatecomponentsformatter
How to Delete an Image from Photolibrary After I Pick It Up Using Uiimagepickercontroller