How can I check if a WebView URL is a local file? - iphone

I have this question that is unsolved for me.... I need to check if the current URL of my webview is a local file in the Documents directory of my app....how can I do this??
Thanks!

You can check:
[request.URL isFileURL];
More information here.

Check the URL's scheme. Assuming you have an NSURL* instance called myURL:
if ([[myURL scheme] isEqualToString:#"file"]) {
NSLog(#"URL %# is local", myURL);
}
Read Apple's NSURL specification to learn more about accessing different parts of an URL.
Assuming you have a UIWebView* called myWebView, you might call something like:
[[myWebView request] URL]
to get the NSURL* of what the web view currently has loaded. Then you can call that object's scheme as described above.

Check for the file:// protocol in the URL.

Related

Loading web view taking more time with ASIHTTPRequest in ios

In my browser based application, I need to set a proxy for each url and doing this with the help of ASIHTTPRequest.
The problem I am facing is that the web view is taking double the time to load the page, probably because I am loading the page twice in my code.
First I check the status with ASIHTTPRequest to determine if the page is allowed to load by ASIHTTPRequest and if so, then I load that url on web-view.
This is where I think the problem is as I think I am loading the url two times which is consuming time.
Can you make a suggestion on other ways to load page once, but in a way that supports checking for authenticated page with usage of proxy settings, or provide me with a link to guide relevant to this question?
NSString *response = [NSString stringWithContentsOfFile:
[theRequest downloadDestinationPath] encoding:[theRequest responseEncoding] error:nil];
int statusCode = [requestH responseStatusCode];
if (statusCode == 200) {
[webV loadRequest:[NSURLRequest requestWithURL:[requestH url]]];
}
else {
[webV loadHTMLString:response baseURL:[theRequest url]];
}
Implement the delegate methods of NSURLConnection (apple docu) and in the connectionDidFinishLoading save the content of the url to a local file and then in load this local file with loadHTMLString.

iphone sdk how to parse a https url?

I am using an url in HTTPS mode. Now I need to parse this url link using NSXMLParser. I tried to parse it but am getting this error "Unable to download story feed from web site(Error code 5)". Is it possible to parse a https url? Why am getting this error? Is there any sample code or links? Kindly help me to get out of this. Thanks in advance.
Not sure to understand, but if you've got an URL as a string, you can create a NSURL object with the urlWithString: method.
NSURL * url = [ NSURL urlWithString: #"https://www.example.org/foo/bar.html" ];
Then, you'll have access to all URL parts, using the NSURL methods, like host, path, scheme, port, and so on...

how to open url need username and password with uiwebview?

I want to open an URL which need password and username in a UIWebview. Such as open my local Wifi Router(192.168.1.1). But when I try following code, there is no popup as Safari to require password and username.
NSURL *url = [NSURL URLWithString:#"http://192.168.1.1"];
NSURLRequest *httpReq = [NSURLRequest requestWithURL:url];
[self._webView loadRequest:httpReq];
Since someone told me to use NSURLConnectionDelegate, I know this, but I donot know how to show the authorized page to the UIWebView.
This will help
NSURL *url = [NSURL URLWithString:#"http://username:password#192.168.1.1"];
NSURLRequest *httpReq = [NSURLRequest requestWithURL:url];
[self._webView loadRequest:httpReq];
You need to implement connection:didReceiveAuthenticationChallenge: in NSURLConnectionDelegate. For details, read Authentication Challenges chapter from Apple "URL Loading System Programming Guide".
UIWebView doesn't provide any mechanism to identify the response. So one solution is explained in UIWebViewHttpStatusCodeHandling github project which identifies the status code of the response (should be 404 in your case).
However, the main drawback is for each request you need to use NSURLConnection and load the request again. But in this case, you can cancel the NSURLConnection too.
The other solution should (might) be the use of Java-Script. Search for the Java-script, which provides the status code of the response.

Open Google web search in Safari in iphone app

I want a Google web search to open in a Safari for a certain search term, that is set by the app.
I am aware of the openURL method and have used it but I can't get a custom search opened in Safari.
This is the code I've got so far
NSString *searchTerm = [NSString stringWithFormat:#"Soccer World Cup"];
searchTerm = [searchTerm stringByReplacingOccurrencesOfString:#" " withString:#"+"];
NSString *url = [NSString stringWithFormat:#"http://www.google.com/#sclient=psy&hl=en&site=&source=hp&q=%#&aq=f&aqi=&aql=&oq=&gs_rfai=&pbx=1&fp=a20cfd04ba3c5cf9",searchTerm];
NSURL *googleURL = [NSURL URLWithString:url];
if(![[UIApplication sharedApplication] openURL:googleURL]){
NSLog(#"Failed to open URL");
}
This code ends up opening google in Safari but with no search term
Any help will be appreciated! Thanks
# defines a URL fragment, and is typically used to jump to a specific named anchor on the page. Some servers will process that info, but it is rare.
You want to use a ? instead to start your URL query parameters
http://www.google.com/?sclient=....
Looks to me like your url is incorrect. Open up Safari on your Iphone and do a search in the google search field. Copy the url from that. To be more specific, your url should probably begin like this: http://www.google.com/search? (note the question mark, that signals the beginning of the parameters in your url). Each parameter is then a name-value pair like for instance source=hp in your own code example. These pairs are separated with the & character. Most of the parameters in your code example can probably be skipped.
Second, you are going to need to replace more than just spaces in your search term (you need to urlencode it).

How to use iPhone Custom URL schemes

I've got an app that I want to be able to use Custom URL schemes for. I want users to be able to open Tweetie using the Custom URL protocol however I need to populate the tweet with dynamic website link which I get using currentItem.link.
I found this code which launches Tweetie and populates a message with static information:
NSString *shortened_url = #"http://your.url.com";
NSString *stringURL = [NSString stringWithFormat:#"tweetie://%#", shortened_url];
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
So using the above code how would I populate the message with currentItem.link information?
Thanks.
It depends entirely on the application on the receiving end. You have to find out how their protocol works, then you can use their protocol as it is designed.
Adding a http:// protocol URL to the end of a tweetie:// protocol URL is not the correct method, and searching for how the Tweetie URL protocol works would be suggested.
The Tweetie protocol is documented, but it's not clear how much of this still applies since the client was converted to the official Twitter one. I believe that the format you want is:
NSString *stringURL = [NSString stringWithFormat:#"tweetie://post?message=%#", shortened_url];
I have already tried this to get the account selection parameter to work. The basic method works, but account selection does not for me.