I want my app to recognize if the current contents of the Clipboard are a URL, and if it is, I want it to load that URL into a web-view.
I'm using the following statement to do this checking:
if ([pasteboard containsPasteboardTypes: [NSArray arrayWithObject:#"public.url-name"]])
...code to load the URL into the webView
but its not working - the IF statement always returns as FALSE, even when the clipboard's contents are clearly a URL.
Strangely enough though, when I remove this IF statement and just go ahead and load the URL I read from the Clipboard into the webView it works perfectly well. So its definitely just the IF statement that's not working for some reason.
Here's the full code:
// executed on a Button-click:
-(IBAction) showClipBoard {
pasteboard = nil; // resetting the pasteBoard each time
pasteboard = [UIPasteboard generalPasteboard];
NSURL *tempURL = pasteboard.URL;
//Check the pasteboard's value-type:
if ([pasteboard containsPasteboardTypes: [NSArray arrayWithObject:#"public.url-name"]]) {
NSLog(#"URL is: %#", pasteboard.string);
NSURL *url = [NSURL URLWithString: pasteboard.string];
NSURLRequest *req = [NSURLRequest requestWithURL: url];
[webView loadRequest:req];
}
else
NSLog(#"=NOT a valid web-address");
NSString *tempString = [pasteboard valueForPasteboardType:#"public.utf8-plain-text"];
// Show the URL in a text-view box called "clipText":
clipText.text = tempString;
}
Anyone see what's wrong here?
You've probably figured this out by now, but, I ran into the same issue & the following worked for me :-
if ([pasteboard containsPasteboardTypes: [NSArray arrayWithObject:#"public.url"]])
...code to load the URL into the webView
Related
When I bring up a "New Message" manually I will see a camera icon to the left of the text edit area. When I use the MFMessageComposeViewController it will not display this icon which means you cannot insert images. I know this can be done because the guys that made txtAgif can do it. One subtle difference is the Caps is turned on. This might be a clue as to how they are getting this to work.
I know that MFMessageComposeViewController does not allow you to insert images programmatically and that is why I'm doing the copy to UIPasteboard trick. This part works perfectly.
This same question has been asked here and here the question has not been answered except for the "It can't be done."
This is my first post so I did not have a high enough ranking to contribute to the other question posts.
How are they doing this? Is there a trick to MFMessageComposeViewController or are they using something completely different?
I have fixed this by following code:
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.persistent = YES;
NSString *imagefile =app.strimagepath;
///
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:imagefile];
if (fileExists)
{
NSData *data = UIImagePNGRepresentation([UIImage imageWithContentsOfFile:imagefile]);
pasteboard.image = [UIImage imageWithData:data];
}
NSString *phoneToCall = #"sms: 123-456-7890";
NSString *phoneToCallEncoded = [phoneToCall stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [[NSURL alloc] initWithString:phoneToCallEncoded];
[[UIApplication sharedApplication] openURL:url];
Here app.strimgPath is the path of image stored in document directory. when the MessageView is opened. Longpress and click on Paste and message will be pasted.
I found the answer! Using UIApplication sharedApplication to launch a blank message works while MFMessageComposeViewController does not. Because I'm using the UIPasteboard I do not need to insert items into the body.
NSString *phoneToCall = #"sms: 123-456-7890";
NSString *phoneToCallEncoded = [phoneToCall stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [[NSURL alloc] initWithString:phoneToCallEncoded];
[[UIApplication sharedApplication] openURL:url];
This is a bug in MFMessageComposeViewController because why would they allow images to be inserted into one and not the other. I would insert an image but I'm not allowed to because I do not have enough reputation.
I want to open an iTunes link in my webView, but when the webView launches the page, it redirects to the Safari browser. There, the url is getting opened, but I want it to open in my webView.
- (void)viewDidLoad {
NSString *urlAddress = #"http://itunes.apple.com/us/app/foodcheck-traffic-light-nutrition/id386368933?mt=8";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
[super viewDidLoad];
}
Please suggest a way to sort out this problem.
You may want to try logging the load requests when you run the app. It may be that apple is automatically changing http:// to itms-apps or http://phobos or something along these lines. If so, then you can block the load when it's call using something like this:
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType;
{
NSURL *loadURL = [[request URL] retain];
NSLog(#"%#",loadURL);
if([[loadURL absoluteString] hasPrefix:#"http://"])
{
[loadURL release];
return TRUE;
}
[loadURL release];
return FALSE;
}
Good luck. I'm curious to know what finally works.
A note from the Apple reference documents- Q: How do I launch the App Store from my iPhone application? Also, how do I link to my application on the store?
Note: If you have iTunes links inside
a UIWebView, you can use this
technique after intercepting the links
with the -[UIWebViewDelegate webView:shouldStartLoadWithRequest:navigationType:]
delegate method.
Not sure if you ever got it working, but this works well for me:
NSString *responseString = [NSString stringWithContentsOfURL:myURL];
[self.webView loadHTMLString:responseString baseURL: nil)];
I am trying to send a query as part a the URL to obtain an XML file, and then trying to parse the XML file using NSXMLParser and initWithContentsOfURL. However the parser is not able to parse the file. I tested the parser with the same file, but this time the file was saved on the server (it was not being generated) and it worked just fine, so I know it is not a problem with the parser.
I have come to think that it does not parse it because I need to load the file before I try to parse it, or give the initWithContentsOfURL time to load the contents. So I tried to put those contents in a NSString and a NSData and using a sleep function as well as using a block but that did not work either.
What would be the best way to go about this problem?
Here is some of the code:
NSString *surl = [NSString stringWithFormat:#"http://lxsrv7.oru.edu/~maria_hernandez/query.xml"];
url = [NSURL URLWithString:surl];
NSString *curl = [[NSString alloc] initWithContentsOfURL:url];
NSLog(#"URL: %#", surl);
NSLog(#"URL Content: %#", curl);
SXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:receivedData];
//Other stuff we have tried:
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:surl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLResponse = nil;
NSError = nil;
receivedData = [NSURLConnection sendSynchronousRequest: theRequest returningResponse: &theResponse error: &error];
Let me know if you have more questions or if you wish to see more code.
Thanks!
have you tried setting a delegate for the NSXMLParse that implements the NSXMLParserDelegate which has events for parsing the document
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html
There are a lot of threads about using UIWebView with caches and/or cookies, but they all seem to relate to remote URLs.
I cannot get cookies to work when "displaying local content" (as the iPhone docs call it).
For example, if I load a plain old HTML file from my bundle resource:
- (void) viewDidLoad {
[super viewDidLoad];
NSString* path = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL* url = [NSURL fileURLWithPath:path];
NSData* data = [NSData dataWithContentsOfFile:path];
[web loadData:data MIMEType:#"text/html" textEncodingName:#"us-ascii" baseURL:url];
}
then:
- (void) webViewDidFinishLoad:(UIWebView*)webView {
NSString* result = [web stringByEvaluatingJavaScriptFromString:
#"try{document.cookie='name=value';''+document.cookie;}catch(e){''+e}"];
NSLog(#"Result = '%#'", result);
}
results in:
Result = ''
Setting the URL to be the actual filename rather than the directory prevents getting: Result = 'Error: SECURITY_ERR: DOM Exception 18', but the cookies do not seem to persist.
I have found a satisfactory work-around. By specifying a real URL, such as http://localhost/..., and then intercepting the loading, by subclassing the NSURLCache class, in order to fetch actual local content.
- (NSCachedURLResponse*) cachedResponseForRequest:(NSURLRequest *)request {
NSString* path = [[request URL] path];
NSData* data = [... get content of local file ...];
NSURLResponse *response = [[NSURLResponse alloc]
initWithURL:[request URL]
MIMEType:[self mimeTypeForPath:path]
expectedContentLength:[data length]
textEncodingName:nil];
NSCachedURLResponse* cachedResponse = [[NSCachedURLResponse alloc]
initWithResponse:response
data:data];
[response release];
return [cachedResponse autorelease];
}
Well you could check out NSHTTPCookieStorage class reference. But If you're using the webView for local content, what is the purpose of using cookies? Why not just save that info some other way on your app?
If your aim is to store data in the UIWebView you can also use window.localStorage. It is a hashtable in which you can store max. 5MB of string data.
e.g.
window.localStorage['highscore_level_1']='12000';
alert(window.localStorage['highscore_level_1']);
I've used this succesfully to implement a highscore table in an UIWebView based iPhone App.
I am trying to build an RSS reader for the Tom's Hardware website.
I have an error when I try to load an URL into an UIWebview from an RSS link.
Here'is my code:
- (void)viewDidLoad {
[super viewDidLoad];
if (self.url != nil) {
NSURL *requestUrl = [NSURL URLWithString:self.url];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:requestUrl];
[webView loadRequest:requestObj];
}
}
The url is setted by the parent controller.
When the URL comes directly from the RSS, I have an error in log:
[2234:207] Error Domain=WebKitErrorDomain Code=101 UserInfo=0x3a6a240 "Operation could not be completed. (WebKitErrorDomain error 101.)"
When I set manually the URL with the same URL like below, it work !
self.url = #"http://www.presence-pc.com/tests/fraude-financiere-paget-macafee-23198/#xtor=RSS-12";
Here is an URL exemple: http://www.presence-pc.com/tests/fraude-financiere-paget-macafee-23198/#xtor=RSS-12. I have no idea about that problem.
I hope you can help me.
Best regards.
Thanks guy, that'it, I have split the URL like this:
NSArray *split = [url componentsSeparatedByString:#"#"];
NSURL *requestUrl = [NSURL URLWithString:[[split objectAtIndex:0] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
and now it work , thanks for your help !
I have this code which also triggers error code 101
_request = [NSURLRequest requestWithURL:[NSURL URLWithString:redirectUrl]];
[editorWebView loadRequest:_request];
RedirectURL is an NSString which has this format http%3A%2F%2Fwww.linkedin.com%2Fnhome%2F
I tried removing percent escapes using the method of NSString and it now works.
[redirectUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
I think the problem can be the anchor in your URL, a friend got the same problem