WKWebView Open _Bank Target Links in Safari - objective-c

I am very new so bear with me. I had an youtube video embed in a WKWebView playing fine on macOS in Objective-C. At first, none of the standard youtube links on the video (channel, recommended videos, etc) would load. I think this was because youtube uses _blank target links. The following code fixed it so any video links will now open in the WKWebView.
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
NSLog(#"createWebViewWithConfiguration %# %#", navigationAction, windowFeatures);
if (!navigationAction.targetFrame.isMainFrame) {
[(WKWebView *)_webView loadRequest:navigationAction.request];
}
return nil;
}
However, I would like these links to open in the macOS browser, and not WKWebview. Lot's of swift examples on iOS, but can't seem to get links to open from WKWebView in safari.
I have tried:
if (!navigationAction.targetFrame.isMainFrame) {
[[NSApplication sharedApplication] openURL:[navigationAction.request URL]];
}
But doesn't work on macOS

This is what eventually worked for me. Seems to open any url (including javascript type popovers used in YouTube) in Safari instead of opening them in WKWebView (or not opening them at all).
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
NSLog(#"createWebViewWithConfiguration %# %#", navigationAction, windowFeatures);
if (!navigationAction.targetFrame.isMainFrame) {
[[NSWorkspace sharedWorkspace] openURL:[navigationAction.request URL]];
}
return nil;
}

Not 100% sure but I've seen this circulated online for opening URLs on Mac. Give it a try.
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:#"http://stackoverflow.com"]];

Related

Open iTunes Store Review on iOS10.1

I was using in my app this link to user review:
- (IBAction)rateMyApp:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"itms-apps://itunes.apple.com/app/881623474"]];
the openURL is deprecated so now it is using according to the developer website this one:
- (IBAction)rateMyApp:(id)sender {
[[UIApplication sharedApplication] openURL:<#(nonnull NSURL *)#> options:<#(nonnull NSDictionary<NSString *,id> *)#> completionHandler:<#^(BOOL success)completion#>]];
But I can not make it work any help is more than welcome.
thanks in advance
Did you check in real iPhone/iPad? In simulator this not works. In device it works. If not works then make sure you return YES in openURL function in AppDelegate class.
- (BOOL)application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation
{
return YES;
}

Open the Bluetooth Settings Menu in Ios 10

I need to open the bluetooth settings menu in IOS10 and above.But [[UIApplication sharedApplication] openURL:
[NSURL URLWithString:#"prefs:root=Bluetooth"]]; is not working in ios 10.
After exploring multiple document I got below link which provide code which will work properly. https://gist.github.com/johnny77221/bcaa5384a242b64bfd0b8a715f48e69f
But, Now I have question will app store accept this patch code or they will reject application.
Please help me to solve this issue.
Thanks in advance
Swift 3.0:- Working in all iOS version upto iOS 10.2
let url = URL(string: "App-Prefs:root") //for system setting app
#IBAction func blutootheButtonTapped(_ sender: AnyObject) {
let url = URL(string: "App-Prefs:root=Bluetooth") //for bluetooth setting
let app = UIApplication.shared
app.openURL(url!)
}
As of iOS 10 "App-Prefs:root" should be used rather than "prefs:root". See below Objective C code. Tested this , code works fine but Apple may reject the app because of this.
NSString *settingsUrl= #"App-Prefs:root=Bluetooth";
if ([[UIApplication sharedApplication] respondsToSelector:#selector(openURL:options:completionHandler:)]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:settingsUrl] options:#{} completionHandler:^(BOOL success) {
NSLog(#"URL opened");
}];
}

iOS9: Cannot open http link with Safari from 'openurl' method

I cannot open the http link with Safari using 'openurl' method, if Safari is not opening in the background. But if Safari is running in background process, the app can open the http link with Safari successfully. I am not sure whether this is one of iOS9 changes.
Here is my code
BOOL isAppInstalled = [[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:[DELEGATE advertiseUrl]]];
if(isAppInstalled) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://stackoverflow.com"]];
}

UIwebview open document (pdf) in other app

Hello everyone,
I am trying to open pdf file in uiwebview to other app. I can display popup like this.
I write the code like this. I put delegate also. UIDocumentInteractionControllerDelegate
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
//other code
if([fileType isEqualToString:#"pdf"])
{
NSLog(#"It is pdf file.");
//other code to save to nsdocumentdirectory and then get this path
// file://localhost/var/mobile/Applications/024D40FF-4518-4526-BEAB- 2A0679FD6308/Documents/iphone_user_guide.pdf
NSURL *pdfURL = [NSURL fileURLWithPath:pathToDownloadTo];
UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:pdfURL];
[docController setDelegate:self];
[docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
}
- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller {
return self;
}
I got this error.
void SendDelegateMessage(NSInvocation *): delegate (webView:didFinishLoadForFrame:) failed to return after waiting 10 seconds. main run loop mode: kCFRunLoopDefaultMode
In some link, it say due to Javascript. I also need to use this. stringByEvaluatingJavaScriptFromString
But I have disabled that function and tested. Then, there is no error but the app crashes.
I would like to know if there is something left to transfer document.
Also, I would like to know how to add Uibarbutton like "Open in ibook or sth" in the uiwebview programmatically with some code as in safari when we open pdf file.
With Regards,

iOS: Playing video that needs authentication works in QuickLook but not in MPMoviePlayerViewController

I login to my server using a SOAP web service. Once logged in, many of the files that I am viewing are only available to the logged in user, so iOS must create a session in NSURL or something.
When trying to preview a video file using MPMoviePlayerViewController it will not work, it just loads up the viewController, then dismisses it.
If I use QuickLook it does work, probably because I download the video locally first, then view it.
But, I don't want to do it this way, I want to stream the video using MPMoviePlayerViewController because I don't want the user to have to download an entire video file. I have seen posts about using NSURLCredential but that doesn't seem to work for me. I used (added my own personal info obviously):
/**
* Play media session
*
* #version $Revision: 0.1
*/
- (void)playMediaWithURL:(NSString *)mediaURL {
// Authenticate
NSURLCredential *credential = [NSURLCredential credentialWithUser:#"myusername"
password:#"mypassword"
persistence:NSURLCredentialPersistenceForSession];
NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
initWithHost:#"mysite.com"
port:80
protocol:#"http"
realm:nil
authenticationMethod:NSURLAuthenticationMethodDefault];
[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential forProtectionSpace:protectionSpace];
// The movie player
NSURL *movieURL = [NSURL URLWithString:[mediaURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
MPMoviePlayerViewController *tempPlayer = [[MPMoviePlayerViewController alloc]initWithContentURL:movieURL];
// Add observer
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
// Properties
tempPlayer.moviePlayer.allowsAirPlay = YES;
tempPlayer.moviePlayer.shouldAutoplay = YES;
tempPlayer.moviePlayer.useApplicationAudioSession = NO;
[self presentMoviePlayerViewControllerAnimated:tempPlayer];
[tempPlayer.moviePlayer play];
}//end
Since this video is only viewable by a logged in user, if the video URL is accessed by a public user, they are presented with an HTML form to login. Does NSURLCredential not work in this case?
Why do all calls to NSURLConnection work, using my logged in credentials (such as downloading the video), but MPMoviePlayerViewController doesn't seem to use those same credentials, and refuses to play the video (probably because it gets the login page)?
Is there a solution to this?
Recently, I had a similar problem not being able to pass cookies to MPMoviePlayerController. I found from stack overflow that the solution is to use NSURLProtocol. Still, it was painful figuring out how to do it, so I thought I'd save people some time by sharing the coded solution: https://stackoverflow.com/a/23261001/3547099