Using PhoneGap Cleaver in iOS to override method - ios

I am using PhoneGap 2.3 - Cleaver for iOS.
How can I override shouldStartLoadWithRequest, webViewDidStartLoad, webViewDidFinishLoad function?
If I add "viewController.webView.delegate = self" to viewDidLoad, the functions above can be called but the PhoneGap API cannot be invoked.
Thanks.
MyViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
CDVViewController* viewController = [CDVViewController new];
viewController.view.frame = self.view.bounds;
//viewController.webView.delegate = self;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:landingURL]];
[viewController.webView loadRequest:request];
[self.view addSubview:viewController.view];
[self addChildViewController:viewController];
}
MyViewController.h:
#interface MyViewController : UIViewController <UIWebViewDelegate>
#end

You shouldn't override shouldStartLoadWithRequest because there is where they manage the javascript urls that call the native functions.
Or at least copy their code from the CDVViewController into your view controller and add the things you need.
example from phonegap 2.9.1
- (BOOL)webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL* url = [request URL];
/*
* Execute any commands queued with cordova.exec() on the JS side.
* The part of the URL after gap:// is irrelevant.
*/
if ([[url scheme] isEqualToString:#"gap"]) {
[_commandQueue fetchCommandsFromJs];
return NO;
}
/*
* If a URL is being loaded that's a file/http/https URL, just load it internally
*/
else if ([url isFileURL]) {
return YES;
}
/*
* If we loaded the HTML from a string, we let the app handle it
*/
else if (self.loadFromString == YES) {
self.loadFromString = NO;
return YES;
}
/*
* all tel: scheme urls we let the UIWebview handle it using the default behavior
*/
else if ([[url scheme] isEqualToString:#"tel"]) {
return YES;
}
/*
* all about: scheme urls are not handled
*/
else if ([[url scheme] isEqualToString:#"about"]) {
return NO;
}
/*
* all data: scheme urls are handled
*/
else if ([[url scheme] isEqualToString:#"data"]) {
return YES;
}
/*
* Handle all other types of urls (tel:, sms:), and requests to load a url in the main webview.
*/
else {
if ([self.whitelist schemeIsAllowed:[url scheme]]) {
return [self.whitelist URLIsAllowed:url];
} else {
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
} else { // handle any custom schemes to plugins
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification object:url]];
}
}
return NO;
}
return YES;
}
the if ([[url scheme] isEqualToString:#"gap"]) manages phonegap calls
BTW, you shouldn't use phonegap 2.3, you need at least 2.5 to pass apple store guidelines added in may, versions previous to 2.5 used the UDID and it's forbidden by apple

Related

Azure AD B2C getting oauthConnection Error: Bad Request

While trying to integrate Azure AD B2C, I am stuck with an error "oauthConnection Error: Bad Request". Following their given sample app it all works fine. But after integrating the same copy paste code from the working sample app, and trying to log in with Facebook or Google Plus, it throws an error! I am pretty sure that every credential that I used in the sample app is the same for my app. Any idea about this will be highly appreciated. Here is my code, AppDelegate.m
#import "AppData.h"
#import "NXOAuth2.h"
#import "AppDelegate.h"
#interface AppDelegate ()
#end
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self setupOAuth2AccountStore];
// Override point for customization after application launch.
return YES;
}
- (void)setupOAuth2AccountStore {
AppData *data = [AppData getInstance]; // The singleton we use to get the settings
NSDictionary *customHeaders =
[NSDictionary dictionaryWithObject:#"application/x-www-form-urlencoded"
forKey:#"Content-Type"];
// Azure B2C needs
// kNXOAuth2AccountStoreConfigurationAdditionalAuthenticationParameters for
// sending policy to the server,
// therefore we use -setConfiguration:forAccountType:
NSDictionary *B2cConfigDict = #{
kNXOAuth2AccountStoreConfigurationClientID : data.clientID,
kNXOAuth2AccountStoreConfigurationSecret : data.clientSecret,
kNXOAuth2AccountStoreConfigurationScope :
[NSSet setWithObjects:#"openid", data.clientID, nil],
kNXOAuth2AccountStoreConfigurationAuthorizeURL :
[NSURL URLWithString:data.authURL],
kNXOAuth2AccountStoreConfigurationTokenURL :
[NSURL URLWithString:data.tokenURL],
kNXOAuth2AccountStoreConfigurationRedirectURL :
[NSURL URLWithString:data.bhh],
kNXOAuth2AccountStoreConfigurationCustomHeaderFields : customHeaders,
// kNXOAuth2AccountStoreConfigurationAdditionalAuthenticationParameters:customAuthenticationParameters
};
[[NXOAuth2AccountStore sharedStore] setConfiguration:B2cConfigDict
forAccountType:data.accountIdentifier];
}
LoginViewController.m
#import "AppData.h"
#import "LoginViewController.h"
#import "NXOAuth2.h"
#interface LoginViewController ()
#end
#implementation LoginViewController {
NSURL *myLoadedUrl;
bool isRequestBusy;
}
// Put variables here
- (void)viewDidLoad {
[super viewDidLoad];
// OAuth2 Code
self.loginView.delegate = self;
[self requestOAuth2Access];
[self setupOAuth2AccountStore];
NSURLCache *URLCache =
[[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024
diskCapacity:20 * 1024 * 1024
diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];
}
- (void)resolveUsingUIWebView:(NSURL *)URL {
// We get the auth token from a redirect so we need to handle that in the
// webview.
if (![NSThread isMainThread]) {
[self performSelectorOnMainThread:#selector(resolveUsingUIWebView:)
withObject:URL
waitUntilDone:YES];
return;
}
NSURLRequest *hostnameURLRequest =
[NSURLRequest requestWithURL:URL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0f];
isRequestBusy = YES;
[self.loginView loadRequest:hostnameURLRequest];
NSLog(#"resolveUsingUIWebView ready (status: UNKNOWN, URL: %#)",
self.loginView.request.URL);
}
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
AppData *data = [AppData getInstance];
NSLog(#"webView:shouldStartLoadWithRequest: %# (%li)", request.URL,
(long)navigationType);
// The webview is where all the communication happens. Slightly complicated.
myLoadedUrl = [webView.request mainDocumentURL];
NSLog(#"***Loaded url: %#", myLoadedUrl);
// if the UIWebView is showing our authorization URL or consent URL, show the
// UIWebView control
if ([request.URL.absoluteString rangeOfString:data.authURL
options:NSCaseInsensitiveSearch]
.location != NSNotFound) {
self.loginView.hidden = NO;
} else if ([request.URL.absoluteString rangeOfString:data.loginURL
options:NSCaseInsensitiveSearch]
.location != NSNotFound) {
// otherwise hide the UIWebView, we've left the authorization flow
self.loginView.hidden = NO;
} else if ([request.URL.absoluteString rangeOfString:data.bhh
options:NSCaseInsensitiveSearch]
.location != NSNotFound) {
// otherwise hide the UIWebView, we've left the authorization flow
self.loginView.hidden = YES;
[[NXOAuth2AccountStore sharedStore] handleRedirectURL:request.URL];
} else {
self.loginView.hidden = NO;
}
return YES;
}
#pragma mark - UIWebViewDelegate methods
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// The webview is where all the communication happens. Slightly complicated.
}
- (void)handleOAuth2AccessResult:(NSURL *)accessResult {
// parse the response for success or failure
if (accessResult)
// if success, complete the OAuth2 flow by handling the redirect URL and
// obtaining a token
{
[[NXOAuth2AccountStore sharedStore] handleRedirectURL:accessResult];
} else {
// start over
[self requestOAuth2Access];
}
}
- (void)setupOAuth2AccountStore {
[[NSNotificationCenter defaultCenter]
addObserverForName:NXOAuth2AccountStoreAccountsDidChangeNotification
object:[NXOAuth2AccountStore sharedStore]
queue:nil
usingBlock:^(NSNotification *aNotification) {
if (aNotification.userInfo) {
// account added, we have access
// we can now request protected data
NSLog(#"Success!! We have an access token.");
} else {
// account removed, we lost access
}
}];
[[NSNotificationCenter defaultCenter]
addObserverForName:NXOAuth2AccountStoreDidFailToRequestAccessNotification
object:[NXOAuth2AccountStore sharedStore]
queue:nil
usingBlock:^(NSNotification *aNotification) {
NSError *error = [aNotification.userInfo
objectForKey:NXOAuth2AccountStoreErrorKey];
// Always got stuck here while trying to login with any credentials
NSLog(#"Error!! %#", error.localizedDescription);
}];
}
- (void)requestOAuth2Access {
AppData *data = [AppData getInstance];
[[NXOAuth2AccountStore sharedStore]
requestAccessToAccountWithType:data.accountIdentifier
withPreparedAuthorizationURLHandler:^(NSURL *preparedURL) {
NSURLRequest *r = [NSURLRequest requestWithURL:preparedURL];
[self.loginView loadRequest:r];
}];
}
ViewController.m
#import "ViewController.h"
#import "AppData.h"
#import "LoginViewController.h"
#import "NXOAuth2.h"
// Login Action
- (IBAction)login:(id)sender {
LoginViewController *userSelectController =
[self.storyboard instantiateViewControllerWithIdentifier:#"login"];
[self.navigationController pushViewController:userSelectController
animated:YES];
}
In case if anybody stumbles in this, Here is the solution
Go to pod, NXOAuth2Client.m and replace the method
- (void)requestTokenWithAuthGrant:(NSString *)authGrant redirectURL:(NSURL *)redirectURL; with the below code
- (void)requestTokenWithAuthGrant:(NSString *)authGrant redirectURL:(NSURL *)redirectURL;
{
NSAssert1(!authConnection, #"authConnection already running with: %#", authConnection);
NSMutableURLRequest *tokenRequest = [NSMutableURLRequest requestWithURL:tokenURL];
[tokenRequest setHTTPMethod:self.tokenRequestHTTPMethod];
[authConnection cancel]; // just to be sure
self.authenticating = YES;
NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"authorization_code", #"grant_type",
clientId, #"client_id",
// clientSecret, #"client_secret",
[redirectURL absoluteString], #"redirect_uri",
authGrant, #"code",
nil];
if (self.desiredScope) {
[parameters setObject:[[self.desiredScope allObjects] componentsJoinedByString:#" "] forKey:#"scope"];
}
if (self.customHeaderFields) {
[self.customHeaderFields enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
[tokenRequest addValue:obj forHTTPHeaderField:key];
}];
}
if (self.additionalAuthenticationParameters) {
[parameters addEntriesFromDictionary:self.additionalAuthenticationParameters];
}
authConnection = [[NXOAuth2Connection alloc] initWithRequest:tokenRequest
requestParameters:parameters
oauthClient:self
delegate:self];
authConnection.context = NXOAuth2ClientConnectionContextTokenRequest;
}
Commenting clientSecret solved the issue

SFSafariViewController does not reopen a webview inline URL link

I have a Webview with inline URL links which are opened with SFSafariViewController, as shown below:
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
if ([SFSafariViewController class] != nil) {
NSString *inR = [[inRequest URL] absoluteString];
NSURL *inReq = [NSURL URLWithString:inR];
SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:inReq entersReaderIfAvailable:YES];
safariVC.delegate = self;
[self presentViewController:safariVC animated:YES completion:nil];
} else {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
}
return YES;
}
#pragma mark - SFSafariViewController delegate methods
-(void)safariViewController:(SFSafariViewController *)controller didCompleteInitialLoad:(BOOL)didLoadSuccessfully {
// Load finished
}
-(void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
// Done button pressed
NSLog(#"DONE PRESSED!!!");
}
When I press the DONE button correctly returns to my Webview. The problem is that if I will press again on the same inline link, it does not open with SFSafariViewController but in Webview which is not what i desire. I tried to force Webview reload in safariViewControllerDidFinish but without success.
Could you please help? Thanks!
The code corrected as below (following proposal of beyowulf) and now is working OK:
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
if ([SFSafariViewController class] != nil) {
NSString *inR = [[inRequest URL] absoluteString];
NSURL *inReq = [NSURL URLWithString:inR];
SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:inReq entersReaderIfAvailable:YES];
safariVC.delegate = self;
[self presentViewController:safariVC animated:YES completion:nil];
return NO;
} else {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
} else {
return YES;
}
}
#pragma mark - SFSafariViewController delegate methods
-(void)safariViewController:(SFSafariViewController *)controller didCompleteInitialLoad:(BOOL)didLoadSuccessfully {
// Load finished
}
-(void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
// Done button pressed
NSLog(#"DONE PRESSED!!!");
}

Open links in safari instead of inside webview

I am a beginner in making iOS applications. I have made a simple webview showing my web page. The problem is that every link that is pressed in my web page opens in the webview. I want some links to open in safari. I would like links starting with "..something" to be opened inside the webview and every other link to be opened in safari. I also have a button for email and dial which i want to open in the dial app and email app on the phone. Is this a possibility? please explain simple.
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (nonatomic, strong) IBOutlet UIWebView *webView;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize webView;
- (void)viewDidLoad
{
NSURL *url = [NSURL URLWithString:#"http://MyWebPage"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestURL];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I made the same application for android using java with this code below
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
try{
System.out.println("url called:::" + url);
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
} else if (url.startsWith("http:")
|| url.startsWith("https:")) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
} else if (url.startsWith("mailto:")) {
MailTo mt=MailTo.parse(url);
send_email(mt.getTo());
}
else {
return false;
}
}catch(Exception e){
e.printStackTrace();
}
return true;
}
}
You'll need to make your controller a UIWebViewDelegate and implement the webView: shouldStartLoadWithRequest:navigationType: method.
#interface ViewController () <UIWebViewDelegate>
viewDidLoad should look like this:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURL *url = [NSURL URLWithString:#"http://MyWebPage"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
webView.delegate = self;
[webView loadRequest:requestURL];
}
- (BOOL)webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked && [self shouldOpenInSafari:[inRequest URL]]) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
- (BOOL)shouldOpenInSafari:(NSURL*)url
{
if ([url.scheme isEqualToString:#"mailto"]) {
return YES;
}
else if ([url.scheme isEqualToString:#"tel"]) {
return YES;
}
else if (([url.scheme isEqualToString:#"http"] || [url.scheme isEqualToString:#"https"]) && [url.host isEqualToString:#"example.com"]) {
return YES;
}
return NO;
}
Then you'll need to implement the shouldOpenInSafari: method. The openURL: method can also handle tel: and mailto: links.
Some code from here.
For the urls which you want to open in web view, use the same code which you have.
For opening in safari, use this
NSString* launchUrl = #"URL to open in safari";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: launchUrl]];

UIWebView prevent reload when interfaceorientation change

I have uiwebview and i would like to prevent reload the current url when i change orientation from portrait to landscape and vice versa.
Do you have any idea?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
website.delegate = self;
[self home];
}
- (void)home {
// Create a URL object
NSURL *isuURL = [NSURL URLWithString: #"http://www.example.com"];
// URL Request Object
NSURLRequest *requestObject = [NSURLRequest requestWithURL:isuURL];
// Load the request in the UIWebView
[website loadRequest:requestObject];
}
I think it is help you:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
{
[website stopLoading];
} else if ([website isLoading])
{
[website reload]; // or [website loadRequest:requestObject];
}}
TRY This:
if(! [myWebView isLoading])
{
NSURL *isuURL = [NSURL URLWithString: #"http://www.example.com"];
// URL Request Object
NSURLRequest *requestObject = [NSURLRequest requestWithURL:isuURL];
// Load the request in the UIWebView
[website loadRequest:requestObject];
}
Thanks a lot guys for your help.
Here is the final solution to my problem
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
if ([website isLoading]) {
[website reload];
}
else {
[website stopLoading];
}
}
else if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
if ([website isLoading]) {
[website reload];
}
else {
[website stopLoading];
}
}
}

Webview excluding php files on request in Phonegap

Ok so I am working on a cordova app and I am using this bit of code below to prevent external urls from opening in the web view. But I have exceptions in there to allow the iframe from google to load. My problem is that my form script is not handled properly now, so how would I add a exception to prevent php files from being sent to the external app.
I would also like the mail app to handle mailto: urls.
Thanks for the help
- (BOOL) webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
NSString *host = [request.URL host];
if(host != NULL || host != nil){
if ([host rangeOfString:#"google.com"].location != NSNotFound) {
return YES;
}else{
if ([[url scheme] isEqualToString:#"https"] || [[url scheme] isEqualToString:#"https"] || [[url scheme] isEqualToString:#"mailto:"]) {
[[UIApplication sharedApplication] openURL:url];
return NO;
}
else {
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
}
}
return [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
}

Resources