iPhone SDK: Loading resources from custom URL scheme - iphone

I have HTML string which is created using an xml file by a third party library. The HTML string contains custom URLs for images and videos (Ex:image://). Is there is way by I can handle these resource load request and load them correctly in UIWebView?

You should be able to change them to a file:// url that points to a file inside the application bundle.
To get the path you can use:
NSString *path = [[NSBundle mainBundle] pathForResource:#"MyFileInResources" ofType:#".png"];
Note that you will need to escape this using:
NSString *escapedPath = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

We need to create a subclass of NSURLProtocol and re-implement the following methods that can handle the custom URL scheme
+(BOOL)canInitWithRequest:(NSURLRequest*)request
+(NSURLRequest*)canonicalRequestForRequest:(NSURLRequest*)request
+(BOOL)requestIsCacheEquivalent:(NSURLRequest*)a toRequest:(NSURLRequest*)b
-(void)startLoading
-(void)stopLoading
We also have to register this custom URL protocol class when the app launches in the following way
[NSURLProtocol registerClass:[CustomURLProtocol class]];

Related

Phonegap: How can I add a hash value to index.html inside webview via Objective-C?

I created an iPhone app with PhoneGap.
On application start, PhoneGap starts the web view with the index.html file from the www directory.
That works good so far.
However, I have the required that I need to do a hashchange to index.html via Objective-C when a certain event occurs (in this case a push message).
Example:
index.html needs to be changed to index.html#foo during runtime
So, how can I change the hash value via Objective-C?
Untested:
UIWebView *webView = ...
NSString *hash = ...
[webView stringByEvaluatingJavascriptFromString:
[NSString stringWithFormat:#"window.location.hash = '%#'", hash]];

Link to pdf within app

I've got an iOS app that at one point opens a link to a website in a webview. These links are kept in a plist file (so it is easy to maintain them as the app evolves). What I want to do next is to also link to PDF's (or any picture of text file format, of even a html format, this is flexible) that are kept within the app. And I would like to do this as much as possible from within the existing app structure. So, is it possible to create a link that can be put in the plist as a web-link, but instead opens a file on the device itself (possibly in the webview)? And how would I go about that? Any ideas?
Thanx in advance for your help!
You will need to create the links at runtime. I would suggest having a certain prefix to a local url, such as mylocalfile:filename. Then, in the code that loads the plist, check for the prefix and create the link when necessary. You could also just create these links once and store them in a separate file, then load that instead of the original.
NSArray *links = nil; //I assumed your plist is an array. Change to dictionary if required
NSString *pathToStoredFile; //Get the path for the file you create with the updated links
if([[NSFileManager defaultManager] fileExistsAtPath:pathToStoredFile]) {
links = [NSArray arrayWithContentsOfFile:pathToStoredFile];
} else {
NSArray *tmp = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"ListOfLinks" ofType:#"plist"]];
if(!tmp) {
//handle error
}
NSMutableArray *tmpLinks = [[NSMutableArray alloc] initWithCapacity:[tmp count]];
for(NSString *link in tmp) {
if([link hasPrefix:#"mylocalfile:"]) {
link = [link substringFromIndex:12]; //12 is the length of mylocalfile:
NSURL *url = [[NSBundle mainBundle] urlForResource:[link stringByDeletingPathExtension] withExtension:[link pathExtension]];
[tmpLinks addObject:[url absoluteString]];
} else [tmpLinks addObject:link];
}
links = [tmpLinks copy];
[tmpLinks release];
[links writeToFile:pathToStoredFile atomically:NO];
}
Yes, I would go with a UIWebView. iOS should be able automatically handle certain URL handlers and your app can register to handle the rest, as necessary.
iOS knows how to handle certain file types already. For example, if Safari (or a UIWebView) encounters http://somesite.com/afile.pdf, it know which apps can handle the file type. Another example is a phone number: skype://8005555555. iOS knows to open Skype and pass the number to it. iOS also knows that iBooks can handle PDf files.
Register your app for the appropriate file handlers and types. Then, users can tap and hold on the link to see a menu of available apps to handle the link. If it's a link that's only used by one app, the user doesn't even need to hold, a tap will suffice.
As far as making a link pointing to a local file, you can, and you would use the C function NSDocumentsDirectory() and append that to a url handler. (Example: http://NSDocumentsDirectory()/filename.pdf)

Loading a local HTML file in Navigation based application

I am new to iOS development.
I have been trying to find an answer to my question but I could not, I have found some related answers but I could not fix the problem.
My problem is, I have made an application which has a table, and for every row there should be a URL which loads a UIWebView, and should be loaded when you click on the row, but it loads an online URL such as #"http://www.google.com". But I want it to load a local HTML file.
I have used this for the URL:
azkarDetailViewController.detailURL=
[[NSURL alloc] initWithString:
[[[azkarData objectAtIndex:indexPath.section] objectAtIndex:
indexPath.row] objectForKey:#"url"]];
And for every row in the table I use:
[AZKAR addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:#"THE NAME OF THE ROW",#"name",#"DayandNight.png",#"picture",#"http://www.google.com",#"url",nil]];
So I want the application to load a local file from the Resources (say for example the name of the HTML file is "index.html").
Can you please help me solving this problem ?
Thank you very much..
When you compile your app, the output is a “bundle” of files and directories. You can actually access the bundle through the NSBundle class; the static method +mainBundle will return a pointer to an instance of NSBundle that represents your app's main bundle.
You can then use the -URLForResource:withExtension: and -URLForResource:withExtension:subdirectory methods of NSBundle (these require iOS 4.0 or higher—there are older equivalents as well). If the HTML file is stored in your main bundle directory (that will be the case unless you created a an actual directory—different from a group—in your bundle), you can find its URL this way:
NSString *myDocumentName = #"index.html";
NSURL *documentURL = [[NSBundle mainBundle] URLForResource:myDocumentName extension:Nil];
Note that you do not have to specify the extension separately if it's already in the filename.
Note: my explanation is a little oversimplified (I'm assuming you don't need to deal with localizations, otherwise there are other issues you should be aware of explained in the docs).
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 40000
// code for iOS below 4.0
NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"WindowsTest" ofType:#"html"]];
#else
// code for iOS 4.0 ++
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:#"WindowsTest" withExtension:#"html"];
#endif

To test working of xml in iphone?

HI can i place both xml files and images that the xml file transports in xcode to test the working since i don't have the internet ? If it can be done , what should be the path name for the xml to be coded in program and images to be typed in xml.?
NSString *stringToXML = [[NSBundle mainBundle] pathForResource:#"theXMLDoc" ofType:#"xml"];
NSURL *XMLURL = [NSURL fileURLWithPath:stringToXML];
Using that you can get XML documents from the resource folder and use them.
Just substitute your url in your code for the variable XMLURL and you should be fine. Its the same for images, you just need to make an NSURL out of the path resource.

UIWebView locally save images and just load html

i want to load just the html from the web and show the images from the local resource.
This helped me a lot, but is the path to the resource on every device the same? Or does it depend on an installation path or similar?
Load Content in the UIWebView on the Iphone
the path to resources is different on every device, that's why you use the function
[[NSBundle mainBundle] pathForResource:nameOfImage ofType:#"jpg"]
the html loading from the web you can do all kinds of ways. The easiest is just:
NSString *myHTML = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://web.site.com/file.html"]];