I have a RMS Protected pdf file something like 'sample.ppdf'.
Its being loaded from an URL.
Now When I implement the following :
UIApplication.SharedApplication.OpenUrl(new NSUrl(_documentUrl));
iPhone Safari App opens and asks me for an option to open with a particular App 'AIP Viewer'
Now when i try implementing the same with the following code :
SFSafariViewController *svc = [[SFSafariViewController alloc] initWithURL:url];
svc.delegate = self;
[self presentViewController:svc animated:YES completion:nil];
SKSafariViewController opens but is Blank.
/*! #abstract Returns a view controller that loads a URL.
#param URL the initial URL to navigate to. Only supports initial URLs with http:// or https:// schemes.
*/
- (instancetype)initWithURL:(NSURL *)URL;
Make sure you url is http://* or https://*
SFSafariViewController is compatible with version iOS 9.0+. Put the version check if the iOS version is lesser than iOS 9.0 open it directly in safari browser.
Try this,
NSURL *urlAsString = [NSURL URLWithString:[NSString stringWithFormat:#"Your_URL"]];
if ([SFSafariViewController class] != nil) {
SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:urlAsString];
[self presentViewController:sfvc animated:YES completion:nil];
}
else {
[[UIApplication sharedApplication] openURL:urlAsString];
}
Related
I have a SFSafariViewController opening at the click of a button inside a UIActionSheet. It has been working fine and is still working fine on all the versions of iOS except iOS 11. Is there something they have changed regarding the SFSafariViewController in iOS 11 or in Xcode 9.0 that might have caused this issue?
UPDATE - So it seems like its Xcode 9.0 that is causing this issue. I have tried running it on different iOS versions and all of them seem to be giving this issue. It used to work fine when I ran it using Xcode 8.3.3, something I don't have anymore :(
Here's the code -
- (void)presentWebView:(NSString *)url {
url = [url stringByReplacingOccurrencesOfString:#" " withString:#"+"];
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *URL = [NSURL URLWithString:url];
if (URL) {
if ([SFSafariViewController class] != nil) {
SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:URL];
sfvc.delegate = self;
[self.tabBarController presentViewController:sfvc animated:YES completion:nil];
} else {
if (![[UIApplication sharedApplication] openURL:URL]) {
NSLog(#"%#%#",#"Failed to open url:",[url description]);
}
}
} else {
// will have a nice alert displaying soon.
}
}
I've managed to fix this in my code. I hope this helps anyone else with a similar problem.
I had the exact same problem as described here. I tried everything above and unfortunately nothing worked.
In my app there were different windows. The fix was to ensure the window that would show SFSafariViewController was 'key' before presenting it. For example:
class MyViewController: UIViewcontroller {
func showSafariViewController() {
// imagine we have 2 windows, one for 'normal' content (key window) and one for 'other' content. lets say we're showing the 'other' content window, and have hidden the 'normal' window. you can see the 'other' content window in the app, but it won't be the key window!
let window = // get the 'other' content window
// make sure we make it the key window before presenting safari
window.makeKey()
// now present safari and celebrate victory by triumphantly slurping on your hot black coffee
let mySafariViewController = SFSafariViewController(...)
self.present(mySafariViewController ...)
}
}
I suspect Apple are searching for a SFSafariViewController instance in the window UIApplication.shared.keyWindow. Perhaps they're adding a child view from somewhere else. In the documentation it states The user's activity and interaction with SFSafariViewController are not visible to your app, so perhaps it's the bug is something related to an added level of security https://developer.apple.com/documentation/safariservices/sfsafariviewcontroller
I have tried to do it using delay and make view of controller loading.Both are working for me.
Method 1. Using delay.
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
let controller = SFSafariViewController(url: url)
controller.modalPresentationStyle = .overFullScreen
self.present(controller, animated: true, completion: nil)
controller.delegate = self
}
Method 2. Loading view.
let controller = SFSafariViewController(url: url)
let _ = controller.view
controller.modalPresentationStyle = .overFullScreen
self.present(controller, animated: true, completion: nil)
controller.delegate = self
Very similar to https://openradar.appspot.com/29108332
To fix it, you can disable the lazy loading of the view:
SFSafariViewController *viewController = [[SFSafariViewController alloc] init...];
(void)viewController.view;
...
[controller presentViewController:viewController animated:YES completion:nil];
Update
Turns out the old answer didn't work, it just worked since I put breakpoints. If I added a thread sleep seems it worked in XCode9, but that's not the best solution. Anyone have another better solution?
SFSafariViewController *sfcontroller = [[SFSafariViewController alloc] initWithURL:url];
if (#available(iOS 11.0, *)) {
[NSThread sleepForTimeInterval:0.5f];
}
sfcontroller.delegate = self;
[controller presentViewController:sfcontroller animated:NO completion:nil];
Old Answer
I have the same issue as genaks and tinkered around with SFViewController.
Seems like this code works for me
SFSafariViewController *sfcontroller = [[SFSafariViewController alloc] initWithURL:url];
if (#available(iOS 11.0, *)) {
SFSafariViewControllerConfiguration *config = [[SFSafariViewControllerConfiguration alloc] init];
config.barCollapsingEnabled = NO;
sfcontroller = [[SFSafariViewController alloc] initWithURL:url configuration: config];
} else {
// Fallback on earlier versions
}
sfcontroller.delegate = self;
[controller presentViewController:sfcontroller animated:YES completion:nil];
In ios 11, they introduce SFSafariViewControllerConfiguration, and by default the barCollapsingEnabled is true and it seems the one that causing my blank SafariView. Hope this solves yours too
We had this issue: our URL was https://our-side.com/index.html (an Angular site that would redirect to the /account route). When we removed the index.html, the SFSafariViewController loaded correctly!
The only thing that has worked so far for me is to make the SafariViewController the rootviewcontroller in the following manner -
((XYZAppDelegate *)[UIApplication sharedApplication].delegate).window.rootViewController = self.svc;
[((XYZAppDelegate *)[UIApplication sharedApplication].delegate).window makeKeyAndVisible];
I have this strange problem that only occurs in iPhone 4 with iOS 7. When I try to present a UIDocumentInteractionController on to the screen, application is stuck on presentPreviewAnimated method. The same thing happens in a different place when I try to use MPMoviePlayerViewController, only this time it is stuck on initWithContentURL. No error is thrown. I know that it is not much info I provided, but I don't have a clue how these things can be related and thus I don't know what information would be helpful. In my project I am using the following structure of Views.
HomeViewController * homeViewController = [[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil];
MenuTableViewController * menuViewController = [[MenuTableViewController alloc] initWithStyle:UITableViewStylePlain];
ECSlidingViewController * slidingViewController = [[ECSlidingViewController alloc] init];
slidingViewController.topViewController = homeViewController;
slidingViewController.underLeftViewController = menuViewController;
UINavigationController * navigationController = [[UINavigationController alloc] initWithRootViewController:slidingViewController];
self.window.rootViewController = navigationController;
For example when I debug the code:
_docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
_docController.delegate = self;
[_docController presentPreviewAnimated:YES];
NSLog(#"Document showed");
The #"Document showed" message is never logged.
Application works great on the iPad and iPhone 5.
In my project I am using CocoaPods.
Please ask any questions that can help with finding the solution.
Try below code, hope it will be helpful.
set UIDocumentInteractionControllerDelegate in .h file
#interface NextViewController : UIViewController <UIDocumentInteractionControllerDelegate>
Create object of UIDocumentInteractionController and initialise it.
- (IBAction)openDocument:(id)sender
{
NSString* path = [[NSBundle mainBundle] pathForResource:#"fileName" ofType:#"fileType"]; // File types - .pdf, .txt, .jpg, .png, or any other.
if (path)
{
NSURL* url = [NSURL fileURLWithPath:path];
UIDocumentInteractionController* docController = [UIDocumentInteractionController interactionControllerWithURL:url];
docController.delegate = self;
[docController presentPreviewAnimated:YES];
}
}
Implement its delegate method.
#pragma mark - UIDocumentInteractionControllerDelegate
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
return self;
}
Turned out that iPhone 4 manages threads differently than other newer devices. I had an error in not directly related place resulting in an endless loop.
while (self.delegate && ![self.delegate dataLoaderOperationCanStop:self])
{
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
All other devices showed those ViewControllers with no problem, only iPhone 4 was stuck. I hope that my problem will help someone with similar strange application behavior.
I load a webpage in a uiwebviewcontroller,after click one link, it will pop a popovercontroller, is it possible to get this controller in my code?
Actually, this popovercontroller is not created by my code. The html detect the webpage is loaded by iDevice and pop this. That means, if I use safari to open webpage, it also will display this popovercontroller.
Thanks.
implement the UIWebView delegate method.
-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
if([urlString isEqualToString :#"abc.com") // link on which want to open popoverview
{
UIPopoverController itemPopover = [[UIPopoverController alloc] initWithContentViewController:viewController];
[itemPopover presentPopoverFromRect:customCell.addImage.bounds inView:webViewpermittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
return NO; //if wanted to load the link on UIWebview return YES else return NO
}
return YES;
}
I have used a native post dialog and I am passing an URL to the method that actually displays the dialog.The URL actually gets posted to the Facebook as needed.But I don't want this URL to be shown in post dialog, because if the user modifies it my mistake then some wrong text gets posted.Is there anyway hiding the URL in dialog. I am using the method presentShareDialogModallyFrom:initialText:image:url:handler: to present the native post dialog.
Unfortunately, you don't show any code so we don't know how you pass the URL. You probably use the setInitialText: method, whereas you need the addURL: method.
SLComposeViewController *facebookController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[facebookController setInitialText:self.titel];
[facebookController addURL:[NSURL URLWithString:self.guid]];
[self presentViewController:facebookController animated:YES completion:nil];
Facebook native share, you have to implements Accounts Framework in to your app
In your framwork you have to Add: Social.framework
In your File .h add:
#import "Social/Social.h"
#interface UIViewController {
SLComposeViewController *FBCompose;
}
In your file .m under a IBAction add:
- (IBAction)facebook:(id)sender {
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"imageURL"]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
//----here you can get pragmaticaly a part of yout text
NSString *mutableArry = (self.myMutableDictionay)[#"string"];
FBCompose = [[SLComposeViewController alloc]init];
FBCompose = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[FBCompose setInitialText:[NSString stringWithFormat:#"Ishare on FB %# from MyApp App", mutableArry]];
[FBCompose addImage:image];
[FBCompose addURL:[NSURL URLWithString:#"http://Link of my app or get the link by myMutableDictionay"]];
[self presentViewController:FBCompose animated:YES completion:NULL];
}
Edit my post because is incomplete sorry.
Hope this help you ;)
HI I want to share Image on twitter from my APP. I have tweet text succesfully by following code.How can I tweet image with text.
-(IBAction)updateTwitter{
if(!_engine){
_engine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate:self];
_engine.consumerKey = kOAuthConsumerKey;
_engine.consumerSecret = kOAuthConsumerSecret;
}
if(![_engine isAuthorized]){
UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine:_engine delegate:self];
if (controller){
[self presentModalViewController: controller animated: YES];
}
}
if([_engine isAuthorized]){
[_engine sendUpdate:#"This is my first test"];
}
}
Have you considered using iOS 5's Twitter Framework? In particular, check out TWTweetComposeViewController, which lets you add images easily.