I am using the below code to load a url into safari (iphone):
NSString *gotoURL = [self getPostID:indexPath.row];
//NSLog(gotoURL );
NSURL *url = [ [ NSURL alloc ] initWithString:gotoURL];
[[UIApplication sharedApplication] openURL:url];
the code works if I replace gotoURL with : #"http://www.example.com" but it wont if I replace it.
The gotoURL gets a url from getPostID (which also returns a NSString) and I did an nslog as you can see and the url format seems fine.
Any idea why its not working?
From what you've added, it is likely that you're missing the protocol in the url. The string passed must be http://domain.tld/4Mi.
Related
I am creating a Cocoa web browser, and I noticed that if the webview loads a nil location, it just loads about:home. Since I have not set it, the page just appears white. Is there a way I can change what about:home looks like. Even if it is a simple .rtf file or something.
I looked around, but don't see any way to do this. Am I suppose to create a NSURL and set it to whatever file?
Thanks. Oh, and if code is ever needed, I would be glad to add it.
Try something like this:
// Inside your App Delegate
-(void)applicationDidFinishLaunching:(NSNotification *)notification {
// Assuming WebView is called myWebView
NSString *currentURL = [myWebView mainFrameURL];
if(!currentURL) {
NSString *homeResource = [[NSBundle mainBundle] pathForResource:#"home" ofType:#"html" inDirectory:#"default"];
NSURL *homeURL = [NSURL fileURLWithPath:homeResource];
NSURLRequest *request = [NSURLRequest requestWithURL:homeURL];
[myWebView loadRequest:request];
}
}
You'll need to have a pre-made file called home.html within a folder called default located in the Resources section of your project.
I suppose this isn't exactly replacing about:home, but you can always check for about:home and handle that appropriately as well.
In iOS 6 the QLPreviewController no longer loads a PDF from a URL. It works fine in iOS 5. I have implemented the QLPreviewControllerDataSource methods as documented here.
#pragma mark - QLPreviewControllerDataSource
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
return 1;
}
- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index;
{
NSURL *fileURL = [NSURL URLWithString:#"http://www.bliley.net/XTAL/PDF_Instructions/Test_File.pdf"];
return fileURL;
}
This works perfectly in iOS 5, however in iOS 6 the console outputs:
Couldn't issue file extension for path: /XTAL/PDF_Instructions/Test_File.pdf
Have you tried using fileURLWithPath instead of URLWithString? I had other issues that were fixed by doing so.
Also not sure if QLPreviewController will handle remote URLs. If not, you could download the file and then display it.
I downloaded the file from remote url and saved locally, then I display the PDF using the QLPreviewController .In iOS 6 its working.
First i saved the file from remote url using the following code :
NSString *local_location;
NSString *path = [[NSBundle mainBundle] pathForResource:#"sampleData" ofType:#"plist"];
path = NSTemporaryDirectory();
local_location= [path stringByAppendingPathComponent:[NSString stringWithFormat:#"My_Invoice.pdf"]];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString: remoteurl]];
[request setDownloadDestinationPath:local_location];
[request startSynchronous];
For showing the Pdf :
QLPreviewController* preview = [[QLPreviewController alloc] init];
preview.dataSource = self;
[self presentModalViewController:preview animated:YES];
QLPreviewController delegate methods are :
- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller
{
return 1;
}
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
return [NSURL fileURLWithPath:local_location];
}
I am having a similar issue and seems like it might stem from a stricter enforcement of the file-type URL of QLPreviewItem
#property (readonly) NSURL *previewItemURL;
Discussion
This property is used by a Quick Look preview controller to get an item’s URL. In typical use, you would implement a getter method in your preview item class to provide this value.
The value of this property must be a file-type URL.
If the item is not available for preview, this property’s getter method should return nil. In this case, the Quick Look preview controller displays a “loading” view.
Availability
Available in iOS 4.0 and later.
Declared In
QLPreviewItem.h
UPDATE: I have opened a bug with Apple dealing with this issue for iOS 6 and it seems they have aced it as a bug so may offer a fix in the near future. The bug I opened had to do with using custom NSURLProtocols for the preview, but may apply to other aspects as well.
Link to class
But note that QLPreviewController expects a URL to a local resource
You would need to download and save the PDF file locally first and then create a proper file URL to the local file.
I am working with Sencha Touch and PhoneGap. The code is for iOS and it's waiting for url with suffix #phonegap-external ..
- (BOOL) webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
if ( ([url fragment] != NULL) && ([[url fragment] rangeOfString:#"phonegap=external"].location != NSNotFound))
{
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
return NO;
}
}
return [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
}
So because I haven't written any line of code in Obj-C, I need your help. Can someone edit code, so that it would open url without suffix.
EDIT:
If user opens url in app, it would open it inside webview but on occasion I would prefer that url is opened in Safari. So this code checks if url has suffix like this - http://google.com#phonegap-external and than it opens it in Safari. Only thing what bugs me, is url is not changed into http://google.com and it opens given url http://google.com/#phonegap-external. Could someone please fix this.
If you're sure that the part of the URL that indicates whether it's to be opened inline or externally (i. e. the #phonegap-external string) is always the last one in the URL, then you can try removing this suffix by writing something like as follows:
NSString *orig = [url absoluteString];
size_t frLen = [#"phonegap-external" length];
NSString *stripped = [orig substringToIndex:orig.length - frLen];
NSURL *newURL = [NSURL URLWithString:stripped];
[[UIApplication sharedApplication] openURL:newURL];
I
have read everywhere so many different things about opening the facebook app from another app, and some says its not official and can be changed ,some say its ok,
but anyway it doesnt work for me (and for many others-with broken pages)
i use :
NSURL *url = [NSURL URLWithString:#"fb://pages/MY-APP-PAGE"];
[[UIApplication sharedApplication] openURL:url];
its open the facebook app, but with a white page. on my mac- this link is working.
so, is this thing is real ?
since i KNOW many people know how to do that , and are not answering,
well , thats how its done :
NSURL *fanPageURL = [NSURL URLWithString:#"fb://profile/PAGE ID"];
[[UIApplication sharedApplication] openURL:fanPageURL];
Where PAGE ID is the long id number at the end of your facebook page .
Thanks for this answer.
To support more users, I used this one:
NSURL *nsurl = [ [ NSURL alloc ] initWithString: #"fb://profile/463560897057026" ];
if (![[UIApplication sharedApplication] canOpenURL:nsurl])
nsurl = [ [ NSURL alloc ] initWithString: #"http://www.facebook.com/SmileyGames" ];
[[UIApplication sharedApplication] openURL:nsurl];
-(void)loadWebAdress:(NSString*)textAdress {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
adressurl=[NSString stringWithFormat:#"http://%#", textAdress];
NSURL *url=[NSURL URLWithString:adressurl];
NSURLRequest *requestObj=[NSURLRequest requestWithURL:url];
[webview loadRequest:requestObj];
}
although url takes it's value from adressurl, adressurl is all the time out of scope when checked in debugger. what is going on? i would like to use it in some other places too. not only in this method. because is out of scope, the app crashes. But, I repeat, it is the one who gives its value to url.
It depends on where the adressurl variable is declared. Since it is generated from the method parameter, it seems odd that you'd want to use it elsewhere in the code. If you have it as a static variable, it could be getting stomped by other code. (For example, if you set it to one value in this method, and another elsewhere, it's not unusual to get crashes, especially if you don't coordinate or synchronize between the two. One reason to avoid globals/statics.) You're free to use the same local variable name in different methods if you like.
Here's what I'd suggest doing instead: (Note: I've fixed some typos.)
- (void) loadWebAddress:(NSString*)textAddress {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://%#", textAddress]];
[webview loadRequest:[NSURLRequest requestWithURL:url]];
}
This is shorter and avoids unnecessary variables. Since the "http://" prefix is fairly common, it doesn't seem like reusing that will provide that much benefit. Is there something else I'm missing?
Edit: To clarify a typo in my comment, you can get the URL as a string from the UIWebView as follows:
[[[webview request] URL] absoluteString]
This uses the following methods chained together:
-[UIWebView request]
-[NSURLRequest URL]
-[NSURL absoluteString]