Declare-iOS

From Xojo Documentation

Revision as of 21:49, 31 January 2022 by Gperlman (talk | contribs) (Change Text Field Border)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!

Commonly used Declares

' Gets a reference to a class
Declare Function NSClassFromString Lib "Foundation" (classname As CFStringRef) As Ptr
' Usage: Var NSClass As Ptr = NSClassFromString("NSClassName")

' Gets an instance of a class
Declare Function alloc Lib "Foundation" Selector "alloc" (classRef As Ptr) As Ptr
' Usage: Var instance As Ptr = alloc(NSClass)

Set focus on an iOS control

Declare Sub becomeFirstResponder Lib "Foundation" Selector "becomeFirstResponder" (ref As Ptr)
becomeFirstResponder(TextField1.Handle)

Clears focus on an iOS control

Sub ClearFocus(Extends c As iOSControl)
Declare Sub resignFirstResponder Lib "Foundation" Selector "resignFirstResponder" (ref As Ptr)
resignFirstResponder(c.Handle)
End Sub

Open a URL in the default app that can handle it (ShowURL)

The openURL command can be called with a series of Declares, the last of which requires a Block. Here are the Declares:

Public Function ShowURL(url As Text) as Boolean
// NSString* launchUrl = @"http://www.xojo.com/%22;
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString: launchUrl]];

Declare Function NSClassFromString Lib "Foundation" (name As CFStringRef) As Ptr
Declare Function sharedApplication Lib "UIKit" Selector "sharedApplication" (obj As Ptr) As Ptr
Var sharedApp As Ptr = sharedApplication(NSClassFromString("UIApplication"))

// https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/#//apple_ref/occ/clm/NSURL/URLWithString:
Declare Function URLWithString Lib "Foundation" Selector "URLWithString:" ( id As Ptr, URLString As CFStringRef ) As Ptr
Var nsURL As Ptr = URLWithString(NSClassFromString("NSURL"), url)

// https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html#//apple_ref/occ/instm/UIApplication/openURL:
Declare Function openURL Lib "UIKit" Selector "openURL:options:completionHandler:" (id As Ptr, nsurl As Ptr, options As Ptr, handler As Ptr) As Boolean

Var b As New iOSBlock(AddressOf URLResult)

Return openURL(sharedApp, nsURL, Nil, b.Handle)
End Function

URLResult is a global method with this Declaration:

Public Sub URLResult(success As Boolean)
// Your code here, although nothing is required
End Sub

Check the scale for the main iOS screen

Function MainScreenScale() As Double
Declare Function NSClassFromString Lib "Foundation" (aClassName As CFStringRef) As Ptr
Declare Function scale Lib "Foundation" Selector "scale" (classRef As Ptr) As CGFloat
Declare Function mainScreen Lib "Foundation" Selector "mainScreen" (classRef As Ptr) As Ptr

Return scale(mainScreen(NSClassFromString("UIScreen")))
End Function

Disable the Idle Timer to prevent the device from sleeping

Function SetIdleTimerDisabled(disabled As Boolean)
Declare Sub setIdleTimerDisabled Lib "UIKit" Selector "setIdleTimerDisabled:" (obj_id As Ptr, disabled As Boolean)
Declare function sharedApplication Lib "UIKit" Selector "sharedApplication" (clsRef As Ptr) As Ptr
Declare function NSClassFromString Lib "Foundation" (clsName As CFStringRef) As Ptr
setIdleTimerDisabled(sharedApplication(NSClassFromString("UIApplication")), disabled)
End Function

Vibrate Device

Public Sub Vibrate()
Const kSystemSoundID_Vibrate = 4095
Declare Sub AudioServicesPlaySystemSound Lib "AudioToolbox.framework" (snd As Integer)
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
End Sub

Change Text Field Border

This is how you can create an extension method to change the border style of an iOSTextField.

Create a module and add this Enumeration to it:

Public Enum BorderStyles
None = 0
Line = 1
Bezel = 2
RoundedRect = 3
End Enum

Now add this method:

Public Sub BorderStyle(Extends tf As iOSTextField, Assigns style As BorderStyles)
Declare Sub setBorderStyle Lib "UIKit.Framework" _
Selector "setBorderStyle:" (obj As Ptr, value As Integer)
setBorderStyle(tf.Handle, Integer(style))
End Sub

You can call this in the Open event for an iOSTextField like this:

Me.BorderStyle = BorderStyles.None

Change Control Background Color

This is how you can create an extension method to change the background color of most iOS controls.

Create a module and add this method to it:

Public Sub BackgroundColor(Extends c As iOSControl, Assigns col As Color)
Declare Function NSClassFromString Lib "Foundation" (className As CFStringRef) As Ptr
Declare Function colorWithRGBA Lib "UIKit" Selector "colorWithRed:green:blue:alpha:" (UIColorClassRef As Ptr, red As CGFloat, green As CGFloat, blue As CGFloat, alpha As CGFloat) As Ptr
Declare Function view Lib "UIKit" Selector "view" (UIViewController As Ptr) As Ptr
Declare Sub setBackgroundColor Lib "UIKit" Selector "setBackgroundColor:" (UIView As Ptr, UIColor As Ptr)

Var UIColorClassPtr As Ptr = NSClassFromString("UIColor")
Var red As CGFloat = col.Red / 255
Var green As CGFloat = col.Green / 255
Var blue As CGFloat = col.Blue / 255
Var alpha As CGFloat = 1.0 - col.Alpha / 255

Var colorPtr As ptr = colorWithRGBA(UIColorClassPtr, red, green, blue, alpha)
Var viewPtr As Ptr = c.Handle
setBackgroundColor(viewPtr, colorPtr)
End Sub

You can call this method in the open event of an iOSTextField to change its background color to red:

Me.BackgroundColor = Color.RGB(255, 0, 0)

Example Projects

All iOS Declare examples are located here: Examples/iOS/Declares

  • AlertSheet: Demonstrates declares to show an iOS Alert Sheet.
  • Base64Encoding: Demonstrates how to Base64Encode Text or a MemoryBlock and to decode back to Text.
  • IconBadgeNumber: Demonstrates how to add a badge number to your app icon.
  • iOSAlerts: Demonstrates how to show all the different types of iOS alerts, including text, password, login and more.
  • ModalView: Display a view modally.
  • SetFocus: Demonstrates how to set focus in a specific control.
  • ShowURL: Demonstrates how to use the iOS URL handling to open URLs in Safari, Maps, Mail and Phone.
  • Speak: Demonstrates how to use Text to Speech.
  • TableSelectAndScroll: Demonstrates how to select a row in an iOSTable and scroll to it if it is not visible.
  • TextFieldBorder: Change the border of an iOSTextField.
  • UIButtonDeclares: Demonstrates how to access additional settings for Buttons.
  • UIDevice: Displays device information.

See Also

Declare keyword