Declare-iOS

From Xojo Documentation

Commonly used Declares

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

' Gets an instance of a class
Declare Function alloc Lib "Foundation" Selector "alloc" (classRef As Ptr) As Ptr
' Usage: Dim 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)

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
Dim 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
Dim 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:" (id As Ptr, nsurl As Ptr) As Boolean
Return openURL(sharedApp, nsURL)
End Function

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)

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

Dim colorPtr As ptr = colorWithRGBA(UIColorClassPtr, red, green, blue, alpha)
Dim 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