Show Progressbar while downloading file from Dropbox - iphone

In my application I want to show Progressbar while downloading file from Dropbox.
Which method I use to download file ?
1) ASIHTTPRequest (but it requires URL to download)
2) DBRequest (but it requires URLRequest)
3) [[self restClient] loadFile:file.path intoPath:localPath (but how to show progress bar?)
Thanks,

Because you mention loadFile, I assume you're using the Core SDK. In that case, you'll want to implement the loadProgress delegate method.
From DBRestClient.h:
- (void)restClient:(DBRestClient*)client loadProgress:(CGFloat)progress
forFile:(NSString*)destPath;

Related

Use of unresolved identifier 'PHPhotoLibrary'

I am working on a Photos extension for Mac OS X (10.11).
I have the Photos and PhotosUI frameworks imported.
Everything is ok apart from when I try to make a call to PHPhotoLibrary.sharedPhotoLibrary().
This results in an error in Xcode 7.3.1 (7D1014):
Use of unresolved identifier 'PHPhotoLibrary'
Has anyone else experienced this or know what could be causing the issue?
You don't need the PHPhotoLibrary class to make a photo editing extension in either iOS or OS X, and in OS X you can't use it.
The entire workflow for a photo editing extension goes through the PHContentEditingController protocol. On both platforms, the main view controller of a photo editing extension implements this protocol. It provides the asset to be edited in the startContentEditingWithInput:placeholderImage: method, and when the user is done editing it calls your finishContentEditingWithCompletionHandler: method so you can apply your final-quality edits and provide output.
Notice I say "when the user is done editing". Saving to the photo library isn't something you do in your extension code — instead Photos asks you for final rendered output, and Photos handles saving it to the library.
In your finishContentEditingWithCompletionHandler: method, you should:
Create a PHContentEditingOutput from the PHContentEditingInput (which you received in startContentEditingWithInput:placeholderImage:).
Save your output as a JPEG file (or QuickTime movie file, for video assets) to the renderedContentURL that the PHContentEditingOutput specifies
Call the completionHandler block that was passed to you, passing it your PHContentEditingOutput.
You calling that completion handler is what triggers Photos saving the edited asset to the library.

SDWebImage clearing cache

I'm displaying a list of icons downloaded from the web with text in a table view. The icons can be changed on server side and I need to replace them as soon as new icons are getting available. I try using the following code:
[imgView setImageWithURL:url placeholderImage:[UIImage imageNamed:#"table_avatar_icon"] options:SDWebImageCacheMemoryOnly];
And call [[SDImageCache sharedImageCache] clearMemory]; In my refresh callback, but it does not purge the contents of the cache. More to it, even if I close the application and open it again the image is still there.
I found only one way to clear the cache and it is by calling [[SDImageCache sharedImageCache] clearDisk];. Which only works after I close and reopen the app.
How can I force SDWebImage to not to use disk caching?
SDImageCache *imageCache = [SDImageCache sharedImageCache];
[imageCache clearMemory];
[imageCache clearDisk];
Don't forget to put these lines of code in your didReceiveMemoryWarning, too.
Located the source if an issue. It seems that I was deceived by the Singleton pattern used in SDImageCache. The cache for extension that is used over UIImageView is being controlled by SDWebImageManager which has an instance variable of SDImageCache. If you want to clear the cache for extension you have to call its imageCache's methods like clearDisk and clearMemory.
Only following code worked for me : Swift 5.0, Xcode 11, iOS 13, SDWebImage pod 5.0
SDWebImageManager.shared.imageCache.clear(with: .all) {
print("deleted all")
}
where you choose options like SDImageCacheType.disk, SDImageCacheType.memory, SDImageCacheType.disk
Also if you want to remove specific image from cache use following:
SDWebImageManager.shared.imageCache.removeImage(forKey: "url of image", cacheType: .all)
Except SDImageCache methods, I strongly advise you to check your image urls. In my situation I tried every method for imageCache and memory issue was still continue. Crashes were occur mainly on iPhone 4s because of hardware it couldn't handle it.
Main issue was url ampersand encoding!
In example, check out these urls: first url is using "&amp" and second one is not. Because of ampersand my JSON library can't read the width and width value get much higher then it should be. That is why I had a memory issue.
1) /select.php?imageid=101961221 "&amp" ;width=100 "&amp" ;isWatermarked=true
2) /select.phpimageid=101961221&width=100&isWatermarked=true
Also the latest versions of SDWebImage library has include UIImageView+WebCache.h class and it really nicely handle cache problems.
The icons can be changed on server side,
so you need to load with refresh cached every time.
if you're using latest SDWebImage framework (5.12.x)
you can call it like this,
[imgView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:#"xxx" options:SDWebImageRefreshCached];
edit at 220112.

Manage the UIWebView loading process

I use UIWebView to present data, and a spinner to show the loading process. The data is an .mp3 file from my server.
I start the spinner when I start loading the webView. Then there is a delay until the audio file starts playing. I need to Stop the spinner quite at that moment.
Notta big deal, but just in case - the loading code:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://blablabla.mp3"]]];
Question: how could I catch the event when the webView is ready to play the audio (when there is enough data to start playing) ? I need it to stop the spinner.
WebViewDidFinishLoad is the only delegate method I could use and it's not good for me, because it notifies when ALL data is loaded. Even if I use it, it is not getting called when the audio file finishes loading (maybe it's not finishing, I don't know, I just see the loading progress gets to the end while the mp3 is playing). Just in case - the error:
Error Domain=NSURLErrorDomain Code=-999
"The operation couldn’t be completed. (NSURLErrorDomain error -999.)"
UserInfo=0x1d39d0 {NSErrorFailingURLKey=http://blablabla.mp3,
NSErrorFailingURLStringKey=http://blablabla.mp3}
Any help/tut/link is appreciated. Thanks in advance!
For that kind of control I would not recommend doing it via a webView - you can't get that level of interaction.
You may try da very popular ASIHTTPRequest class.
There is a [didReceiveData:] delegate, maybe suitable for your testing.
Don't worry it's easy !
the -999 for WebView is a normal error that apply when you are loading another content without letting the page really gets fully loaded.
so must of the time do that :
if([error code] != NSURLErrorCancelled) return;
To your javascript event detection :
Execute Objective-C methods from JS
This is unfortunately slightly more complex, because there isn't the same windowScriptObject property (and class) that exists on Mac OSX allowing complete communication between the two.
However, you can easily call from javascript custom-made URLs, like:
window.location = yourscheme://callfunction/parameter1/parameter2?parameter3=value
load a jquery library and use the document.ready event :
$(document).ready(function() {
window.location = yourscheme://callfunction/parameter1/parameter2?parameter3=value
});
And intercept it from Objective-C with this:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *URL = [request URL];
if ([[URL scheme] isEqualToString:#"yourscheme"]) {
// parse the rest of the URL object and execute functions
}
}
This is not as clean as it should be (or by using windowScriptObject) but it works.
Last solution :
A Javascript Bridge
https://github.com/marcuswestin/WebViewJavascriptBridge
Enjoy
Instead of using the iOS activity indicator, implement the activity indicator inside the html. It is much simpler end also elegant solution. Just show/hide html element with animated gif image using the javascript.
Use a separate class to manage the NSUrlConnection for the retrieval and storage of the .mp3 file. You could use a NSNotificationCenter and the postNotification message or a delegate for when the file is ready to be played

Display encrypted file using QuickLook framework or UiDocumentInteractionController

I have an encrypted word/excel/pdf file locally stored which I need to preview in my iPad app. I understand that QLPreviewController or UiDocumentInteractionController could be used to preview these files. I can very well use this
- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index {
return [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[documents objectAtIndex:index] ofType:nil]];
}
But the file is encrypted and when I decrypt it I would get hold of NSData object. How do I go about loading NSData in either of these.
Also I understand that I can very well store the NSData back as a local file and load it in Preview. But there is a constraint of not storing the unencrypted file locally.
If someone has already accomplished this and can help me out here it will be greatly appreciated.
Thanks
AJ
Since you are using Quick Look, your options are limited. You must give Quick Look an NSURL, which means it must be on the file system (or the Internet). Fortunately, this shouldn't be much of a problem. iOS devices use hardware-level encryption. When your file is encrypted, only your app has the key to decrypt it. So, your file will still be encrypted, but it will also be readable by your app and only your app.
Here's what you do:
Decrypt your file into an NSData object, which you've already done.
Write the file to a location that will not get uploaded to iCloud nor backed up by iTunes. The tmp directory is probably the best choice. The code looks something like this:
NSData * data = // Your decrypted file data.
NSString * fileName = // Whatever you want to name your file.
NSString * path = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
NSURL * url = [NSURL URLWithString:path];
NSError * error = nil;
BOOL success = [data writeToURL:url
options:NSDataWritingFileProtectionComplete
error:&error];
if (success) {
// Give the URL to Quick Look.
}
else {
// An error happened. See the 'error' object for the details.
}
At this point you have an NSURL which you can use with Quick Look. Don't forget to delete the decrypted file when you are done with it.
There are a few things to note about on-disk encryption:
It is only supported on iOS 4.0+.
It may not work on "older" devices.
The user must have an active passcode.
If you use NSDataWritingFileProtectionComplete, the file is not accessible while the device is locked. If you need to access the file while the app is locked, then you should use NSDataWritingFileProtectionCompleteUnlessOpen or NSFileProtectionCompleteUntilFirstUserAuthentication instead. This will still give you great protection, even if the device is stolen and jailbroken. Be aware, though, that these encryption options are only available on iOS 5.0+
For more details for on-disk encryption, check out the iOS App Programming Guide
After doing some digging, I found out that QLPreviewController is using UIWebView underneath, and calls the loadRequest: to load the requested file.
Another way to accomplish what you desire is to make a private Category on UIWebView,
and use method swizzling to override the loadRequest: method, and call instead the loadData:MIMEType:textEncodingName:baseURL: method.
Beware that:
1) In low-memory scenarios (i.e. large files) a black screen with
"Error to load the document" appears, if that concerns you. (The
unhacked QLPreviewController knows how to handle these scenarios
very well and present the document).
2) I'm not sure Apple are going
to approve this kind of hack, although no private APIs are used
here.
code:
#implementation UIWebView (QLHack)
- (void)MyloadRequest:(NSURLRequest *)request
{
// Check somehow that it's the call of your QLPreviewController
// If not, just call the original method.
if (!insideQLPreviewController)
{
// Call original implementation
[self MyloadRequest:request];
}
else
{
// Load the real data you want
[self loadData:data MIMEType:mimeType textEncodingName:nil baseURL:someURL];
}
}
+ (void)load
{
method_exchangeImplementations(class_getInstanceMethod(self, #selector(loadRequest:)), class_getInstanceMethod(self, #selector(MyloadRequest:)));
}
#end
Actually, writing a file to a tmp directory is still insecure. The other alternative is to use UIWebView with NSURLProtocol and allow decrypting this data on the fly.
One way could be.
use Temp Dir , Save File in Temp , Make NSURL From that Temp File and Display and then Delete that temp Dir after that.
Thanks.

Using a UIWebView with loadHTMLString/loadData breaks the back and forward buttons, workaround?

There's a known problem with embedded UIWebViews that if you load data into them using loadHTMLString or loadData, the canGoBack/canGoForward properties and goBack/goForward methods don't work. These only work when using loadRequest.
Since Safari's normal app cache doesn't work in embedded UIWebViews, creating a native app that effectively caches otherwise live content becomes impossible/unusable. That is, I can cache the contents of the HTML, Javascript, images, etc. and load them via loadHTMLString or loadData, but then the back and forward buttons don't work.
I could also use loadRequest and specify a file URL, but that breaks when it comes to communicating with the live site -- even if I specify a tag (because of cookie domain issues).
I have a work around that involves basically re-implementing the app cache using local store (and not having the native app do any caching itself), which is OK, but not really ideal. Are there any other work arounds/something I missed?
I am using the UIWebView's canGoBack to check to see if I'm at the first page of the history. If I am then I just call the little method I used to load the first page (displayLocalResource sets up the HTMLString and loads it into the webView). Here is a snippet:
//Implementing a back button
- (void)backOne:(id)sender{
if ([webView canGoBack]) {
// There's a valid webpage to go back to, so go there
[webView goBack];
} else {
// You've reached the end of the line, so reload your own data
[self displayLocalResource];
}
}
So do you download the HTML yourself, then pass it to UIWebView as a string? Why so? Do you modify it on the fly or something?
Maybe a custom URL schema would help? You use loadRequest with a schema of your own, which in turn works with HTTP and then feeds the webview whatever data you want?
I had a same problem. I tried manage the history, but it is error prone. Now I have discovered a better solution of this.
What you want to do is simply add a loadRequest to about:blank and make that as a placeholder for you before you call loadHTMLString/loadData. Then you are totally free from monitoring the history. The webview.canGoBack and canGoForward will just work. Of course, you will need a hack to handle go back to the placeholder about:blank. You can do that in webViewDidFinishLoad. Here is the code highlight:
In the function when you call loadHTMLString:
[weakSelf.fbWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"about:blank"]]];
[weakSelf.fbWebView loadHTMLString:stringResponse baseURL:url];
Code to handle goBack:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
if ([webView.request.URL.absoluteString isEqualToString:#"about:blank"]
&& ![webView canGoBack] && [webView canGoForward]) {
[weakSelf.fbWebView loadHTMLString:stringResponse baseURL:url];
}
}
I think it is also possible expand this solution to handle those loadHTMLString that is not the first load. Just by having a Stack to record all the string response and insert an about:blank on each loadHTMLString. And pop the stack when each time go back to about:blank.
Could you fetch the content, save it to the local filesystem, point the webview to the local filesystem using file:// URLs, then intercept the link follows with shouldStartLoadWithRequest to fetch more to local fs, point webview at new local content, etc?
I've had good luck with UIWebView and file:/// URLs. Basically you'd be intercepting load requests, fetching stuff yourself, writing it to the local filesystem with rewritten URLs, then loading that into the browser.
There seems to be no way to load/save the browser history.
Loading the string into a temp file and using that as a URL request seems to cure this. It's something about loading the string directly that causes UIWebView not to see it as the home page you can navigate back to. This code worked for me:
//If you load the string like this, then "webView.canGoBack" never returns YES. It's documented frequently on the web.
//Loading the string as a URL instead seems to work better.
//[self.myWebView loadHTMLString:str baseURL:nil];
//write the string to a temp file
NSString *fileName = #"homepage.html";
NSURL *fileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:fileName]];
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
[data writeToURL:fileURL atomically:NO];
//open that temp file in the UIWebView
[self.myWebView loadRequest:[NSURLRequest requestWithURL:fileURL]];
Use this to enable/disable the back button:
- (void)webViewDidFinishLoad:(UIWebView *)webView{
//this is to check if we're back at the root page.
if (webView.canGoBack == YES) {
self.backButton.enabled=YES;
}
else {
self.backButton.enabled=NO;
}
}