Now that 5.0 is launched and we can discuss it without breaching Apple's NDA, I have an issue with the new version of NSURLConnection. This has a new delegate, NSURLConnectionDownloadDelegate with two key methods.
connection:didWriteData:totalBytesWritten:expectedTotalBytes: is invoked repeatedly while the file download is progressing
connectionDidFinishDownloading:destinationURL: is called once when the download is complete.
The downloaded file should then be at destinationURL (at least for the life of this method - the intent is that you get it and move it somewhere permanent). The issue is : it's not there. That directory is empty. I have reported this as a bug to Apple and they tell me it is a duplicate of an issue that they are already aware of.
If anyone has a workaround for this, or finds they can use this delegate successfully, please let me know.
UPDATE 10/17/2011 : I've given up on this and gone back to the old delegate which still works fine in 5.0 even though the documentation says the delegate methods are only available thru 4.3.
Apparently only for use with Newsstand apps. This guy might have found a work around:
http://adamernst.com/post/18948400652/a-replacement-for-nsurlconnectiondownloaddelegate
Alternatively, just use NSURLConnection. But heads up if you implement the NSURLDownloadDelegate methods, they appear to override the standard NSURLConnection delegate methods. If it's the handy didWriteData: method of NSURLConnectionDownloadDelegate that you want, e.g. to update a UIProgressView, you can achieve the same by grabbing the total file size from the http response, and by using the didReceiveData: method of NSURLConnection.
Documentation tells the file is only guaranteed to exist while the delegate method is called so you will need to copy it somewhere else the moment the delegate method is called.
Google tells me people are having problems when the Server sends a cryptic filename and/or mime-type.
Folks,
I've recently discovered this new NSURLConnectionDataDelegate protocol. However, the iOS 5 documentation is incomplete.
The New NSURLConnectionDataDelegate protocol is in the documentation, but the new version of NSURLConnection that uses it is not. That documentation still tells us to use the now deprecated NSURLConnectionDelegate protocol methods.
I gather that NSURLConnection will now download data from a remote URL directly to a file on the local disk, much like the NSURLDownload class in Mac OS. How do I figure out how this works?
The header for NSURLConnection hints that the connection object deduces what you want based on which version of the NSURLConnectionDelegate protocol the delegate conforms to. That seems really screwy. I've never heard of using the protocol conformity of a delegate as a way of controlling the behavior of a class before.
Even now NSURLConnectionDownloadDelegate doens't work.
Here is a good replacement:
http://github.com/jbrennan/JBContainedURLConnection
Related
I want to migrate my iOS hybrid app from UIWebView to WKWebView as the former has been deprecated. There are a number of similar questions to this on Stack Overflow that have already been answered but these questions focused on the rather simpler topic of just displaying a web view with out addressing the loading of local files nor the two way interaction required between the Objective C wrapper and the Javascript code for a hybrid app to deliver any functionality.
So far I have established that I need to do the following
Replace the import statement for UIKit with WebKit.
Before creating the wkwebview it is necessary to create a configuration object and set its key allowFileAccessFromFileURLs to TRUE.
After creating the wkwebview, setting its navigationDelegate and its UIDelegate to self.
When loading the url of the html/ccc/js file location, specifying allowingReadAccessToURL to delete the last path component (which I think is the file://)
Set the wkwebview as a sub view of the main view (I think this was not required in UIWebView)
Replacing the existing communications channel from the javascrtipt code to the Objective C code which made use of "shouldStartLoadWithRequest" by creating a script message handler in the wkwebview configuration object mentioned in 2 above and then using this message handler to invoke the processing that used to be done by "shouldStartLoadWithRequest".
Replacing all existing communications channels to the javascript code from the Objective C code which made use of "stringByEvaluatingJavaScriptFromString" with "evaluateJavaScript" which now requires a completion handler which can be set to nil as I am not using any callback values.
Adding a solution to allow the keyboard to be displayed without user selecting an input text field. The best I can see so far is Programmatically focus on a form in a webview (WKWebView). I am somewhat concerned that it appears to need changing every IOS release.
Addressing CORS issues. I understand that WKWebView is much stricter in its implementation of loading remote files from different URLs than UIWebView was, but I am not clear whether there is also a need to whitelist the local files to be loaded as well.
If anyone knows of a check list of things that need to be changed with tips/examples with exact details, or could provide such as an answer that would be superb.
In addition I would like to continue to support pre IOS 11 users by retaining UIWebView for these users as I believe WKWebView had issues in those earlier versions. Does anyone know if this going to cause any additional problems to resolve, and if so how?
Yes
allowFileAccessFromFileURLs is undocumented, so it may or may not work in iOS 13.
Yes
Yes
Yes, and it is required with UIWebViews as well. What might be different is that adding a WKWebView to a storyboard or nib was impossible or buggy, at least until very recent versions of Xcode, which may be why you have that impression.
No, the -webview:shouldStartLoadWithRequest:navigationType: method is a UIWebViewDelegate method. You want the corresponding WKNavigationDelegate method, which is -webView:decidePolicyForNavigationAction:decisionHandler:.
Yes
I can't answer to that, as I've never needed to do it.
See #8
WKWebView has been around since iOS 8, so unless you're targeting iOS 7, Apple still may reject your app once they start rejecting for use of UIWebView. I don't think there's any way to know if that is the case other than submitting the app to find out.
If your javascript makes use of custom schemes that rely on the webview delegate to handle them correctly (i.e., myscheme://some/callback), it can be flaky with Webkit. This is where the script message handler that you alluded to comes in. But you have to update your javascript to use window.webkit.messageHandlers.someCallback.postMessage(someParams) instead of using a custom URL scheme.
I was looking at WWDC Video Advanced Techniques with UIKit Dynamics. I tried to go through the explanation how to create a customized transition using UIDynamics. When the UIGravityBehavior will be created, they also use a method I can't access (second line):
[self.gravityBehavior = [UIGravityBehavior alloc] initWithItems:#[myView]];
[self.gravityBehavior setXComponent:0.0 yComponent:3.0];
I also found a Github project where you can see the runtime Headers of UIGravityBehavior. There the method is available. I don't understand why the method is called inside the video. Is this just an explanation what happens when the app is running or do I have to implement a specific delegate to get access to it?
There's a method called didLoadFromCCB that you can implement for any class that has been created in SpriteBuilder. This method is called when Cocos2d loads the class from a CCB file.
What I want to know is: where does this method come from?
Xcode doesn't seem to know it exists, as it doesn't autocomplete.
I'm not having any problems with the method, it's getting called and everything is working perfectly, I'd just like to know where it comes from.
Also, I'd like to know if there is any documentation that lists methods like this that can be called on classes loaded from CCB files. I've had a look around www.spritebuilder.com, but there doesn't seem to be anything of that nature.
CCBReader sends this message. You can search the project in xcode if you need to find its exact origin.
The mistake is that this method wasn't declared in any of the CCBReader headers and isn't in a protocol either, therefore Xcode won't autocomplete it because it's considered a private method. This will be fixed eventually.
There is currently no CCNReader documentation besides the code itself. But didLoadFromCCB is the only method CCBReader will send to nodes.
EDIT: I opened an issue regarding autocomplete of didLoadFromCCB, it's been bothering me too.
I have been using a storyboard, and I have not gotten into the code much, and now it seems that it is time.
I have a form and when the submit button is pressed, do I need to put code to handle that into the .h or the .m file? Also, are there examples of handling a button and making a remote call that I can reference somewhere?
Thanks!!
You might find Apple's Your First iOS App document helpful; it covers connecting buttons to actions in code.
As for making a remote call; there are many ways. I would suggest you look into NSURLConnection and friends as a stating place, but there are many, many libraries to help with this, such as RESTKit.
I'm having difficulty uploading files to S3 via AFNetworking. When I attempt to upload, I get callbacks indicating a small percentage of the file is uploaded, then the callbacks cease, and one or two minutes later I get a timeout message.
I'm using AFAmazonS3Client but it seems like a pretty straight forward subclass of AFHTTPClient so it seems unlikely the problem lies there. Since I'm new to both AFNetworking and S3, I'm pretty sure the problem is typing this message :)
I've downloaded the AWS-IOS SDK and was able to post a picture from their sample code without a problem. So I don't think its a misconfiguration of my S3 account.
After struggling a bit with the same problem and a bit of research, I found a solution that seems to work well.
Download the Amazon AWS SDK for iOS and add it to your project:
http://aws.amazon.com/sdkforios/
Then, implement the upload test with a custom NSOperation class as explained here:
Using delegates, operations, and queues
The AmazonServiceRequestDelegate protocol is the key to get progress updates, in a similar fashion to what AFNetworking does.
In particular, this method is what I needed:
-(void)request:(AmazonServiceRequest *)request didSendData:(NSInteger)bytesWritten
totalBytesWritten:(NSInteger)totalBytesWritten
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite;