Examples for iOS
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 MobileControl)
  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 String) as Boolean
  ' NSString* launchUrl = @"http://www.xojo.com/";
  ' [[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 ObjCBlock(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 MobileTextField.
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 MobileTextField, 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 Opening event for an MobileTextField 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 MobileUIControl, 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 Opening event of an MobileTextField to change its background color to red:
Me.BackgroundColor = Color.RGB(255, 0, 0)
See also
Declare keyword