iOS Tutorial Share data from your app with ActivityViewController

upload_2016-11-17_9-57-25.png


ActivityViewController allows the user to share data from your app using the standard share dialog.
It was added in iPhone library v2.00.

Using it is simple. You need to initialize AVC with one or more items that you want to share and call show:
B4X:
Sub Page1_Click
   Dim avc As ActivityViewController
   avc.Initialize("avc", Array("Some text to share together with an image", LoadBitmap(File.DirAssets, "smiley.png")))
   avc.Show(Page1, Page1.RootPanel) 'Second parameter is relevant for iPad only. The arrow will point to the view.
   'avc.Show(B4XPages.GetNativeParent(Me), Root) '<---- B4XPages equivalent code
   Wait For avc_Complete (Success As Boolean, ActivityType As String)
   Log($"Success: ${Success}, ActivityType: ${ActivityType}"$)
End Sub

You can also share files with ActivityViewController.

B4X:
Sub Page1_Click
   Dim avc As ActivityViewController
   avc.Initialize("avc", Array(CreateFileUrl(File.DirLibrary, "1.pdf")))
   avc.Show(Page1, Page1.RootPanel)
End Sub

'Doesn't work with assets files. You must first copy them.
Sub CreateFileUrl (Dir As String, FileName As String) As Object
   Dim no As NativeObject
   no = no.Initialize("NSURL").RunMethod("fileURLWithPath:", Array(File.Combine(Dir, FileName)))
   Return no
End Sub

Sub avc_Complete (Success As Boolean, ActivityType As String)
   Log($"Success: ${Success}, ActivityType: ${ActivityType}"$)
End Sub
 
Last edited:

sorex

Expert
Licensed User
Longtime User
nice, Erel.

I assume that this is from now on the official method and replaces that modShare.bas module someone posted here once?
 

Descartex

Well-Known Member
Licensed User
Longtime User
Absolutely amazing and easy to work with!
Thank you so much!!!
 

b4xscripter

Member
Licensed User
Longtime User
Hi!
Many thanks for this library!
For my project I would like to have an easy action: when is performed a Sub, for example, Activity_Create, it should just open a dialog listing all the possible "social networks" and email to share the text. Nothing more. On Android is very easy:

B4X:
Sub Globals
'Android code
Private
intent As INTENTID
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("layout1")
intent.Initialize
intent.ShareText("HOLA!!!!")
End Sub

I suppose that it should be something like:

B4X:
Sub Activity_Create(FirstTime As Boolean)
Dim avc As ActivityViewController
 avc.Initialize("avc", Array("Some text to share together with an image", LoadBitmap(File.DirAssets, "smiley.png"))) ' I only want to share the text, nothing more!
 avc.Show(Page1, Page1.RootPanel) 'Second parameter is relevant for iPad only. The arrow will point to the view.
End Sub

Could someone clarify that this library is right for my this purpose?

Thank you!
 
Top