Hey !
I am trying to port the excellent B4A library called "RSAsyncDownloader" to B4i.
Could someone please have a look at the Objective C code ? I would very much appreciate it
If someone is wondering why I am asking all these Objective C related questions over the last week or so, I am A) trying to learn a bit about Objective C and B) trying to "level" the playing field between B4A and B4i so that functionality on both platforms can be approximately the same (for the functionality/features that my app uses anyway) even if many libraries are simply not available to B4i at the moment. Also I am trying to ensure that interfaces/API are as identical as can be between the two platforms so that the most amount of code may be reused (or at least that's for I am aiming for )
I hope that the code can be of use to others. Feel free to grab it.
I am trying to port the excellent B4A library called "RSAsyncDownloader" to B4i.
Could someone please have a look at the Objective C code ? I would very much appreciate it
If someone is wondering why I am asking all these Objective C related questions over the last week or so, I am A) trying to learn a bit about Objective C and B) trying to "level" the playing field between B4A and B4i so that functionality on both platforms can be approximately the same (for the functionality/features that my app uses anyway) even if many libraries are simply not available to B4i at the moment. Also I am trying to ensure that interfaces/API are as identical as can be between the two platforms so that the most amount of code may be reused (or at least that's for I am aiming for )
I hope that the code can be of use to others. Feel free to grab it.
B4X:
'Class module
Sub Class_Globals
Private FileName As String = ""
Private Directory As String = ""
Private ev As String = ""
Private t As Object
End Sub
#If OBJC
- (void)downloadURL:(NSURL *)url (NSString *)dir (NSString *)file completionBlock:(void (^)(BOOL succeeded, NSData *image))completionBlock
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if ( !error )
{
completionBlock(YES,data);
//NSLog(@"downloaded FULL size %lu",(unsigned long)data.length);
if (file.length == 0) {
NSArray *parts = [url componentsSeparatedByString:@"/"];
NSString *filename = [parts lastObject];
} else {
NSString *filename = file;
}
// Construct fullpath
NSString *fullpath = [dir stringByAppendingFormat:@"%@/%@", filename];
// Save
[data writeToFile:fullpath atomically:YES];
// Raise event
[self.bi raiseEvent:nil event:@"downloadDone:" params:@[url]];
} else{
completionBlock(NO,nil);
// Raise event
[self.bi raiseEvent:nil event:@"downloadFailed:" params:@[url]];
}
}];
}
#End if
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(eventName As String, Target As Object)
ev = eventName
t = Target
End Sub
Public Sub setFileName(name As String)
FileName = name
End Sub
Public Sub setDirectory(dir As String)
Directory = dir
End Sub
Public Sub getFileName
Return FileName
End Sub
Public Sub getDirectory
Return Directory
End Sub
Sub Download(theURL As String)
If Directory = "" Then
' We ought to throw an exception here but that is not possible ??
Dim h As HUD
h.ToastMessageShow("No Directory given ???")
Return
End If
Dim n As NativeObject = Me
If FileName = "" Then
If SubExists(t, ev & "_Started") = True Then
CallSub2(t, ev & "_Started")
End If
n.RunMethod("downloadURL:", Array As Object(theURL, Directory, ""))
Else
If SubExists(t, ev & "_Started") = True Then
CallSub2(t, ev & "_Started")
End If
n.RunMethod("downloadURL:", Array As Object(theURL, Directory, FileName))
End If
End Sub
' PRIVATE EVENTS
Private Sub downloadDone( URL As String )
CallSub3(t, ev & "_Finished", URL, True)
End Sub
Private Sub downloadFailed( URL As String )
CallSub3(t, ev & "_Finished", URL, False )
End Sub
Last edited: