Custom Login Instagram API iOS - ios

I'm developing an application for iOS with the authentication, using Instagram API.
My code for authentication works good:
NSString *fullURL = [NSString stringWithFormat:#"https://instagram.com/oauth/authorize/?client_id=%#&redirect_uri=%#&response_type=code",KCLIENTID,kREDIRECTURI];
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
instagramConnectWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];
instagramConnectWebView.delegate = self;
[instagramConnectWebView loadRequest:request];
When the request is sent, my webView shows the standard Instagram login interface, where user has to insert username and password to login. If login succeed, app is redirected to my redirection-url, where i get the code and the token of this user.
My question is really simple, is there a way to customize standard interface of Instagram login? I'd like to insert in my app two simple UITextField (One for username and one for the password) and when user clicks on my UIButton login, application should send (POST or GET?) username and password to Instagram, and return response (Login success or not).
I hope i explained myself.
Thanks.

You can make this by using javascript. by loading a web view in background :
You need to access the text field of that web view and pass value of your custom UItextfields and after that you can call webView's "Submit" button's Onclick event.
NSString *javaScript = #"document.getElementById('id_username').value = 'tfusername'";
NSString *javaScript2 = #"document.getElementById('id_password').value = 'tfpassword'";
to evaluate each script you can call: [webView stringByEvaluatingJavaScriptFromString:javaScript];
You can also call script to press login button. This same thing work for me. May be this will help you.

I have created a wrapper for Instagram API for iOS. Take a look at this link . This will also give an idea of what you need to do.

Related

objective c - Open with suggestions when trying to open URL schemes

I started an application that can handle SMS or Mail url schemes but I don't want to open the default apps installed. I have tried doing this
[[UIApplication sharedApplication] openURL: url];
but this opens the default SMS and Mail apps.
I tried using the UIActivityViewController
NSString *url=#"mailto:sample#gmail.com";
NSURL *schemeURL = [NSURL URLWithString:url];
NSString * title =[NSString stringWithFormat:#"Send Email",url];
NSArray* dataToShare = #[url];
UIActivityViewController* activityViewController =[[UIActivityViewController alloc] initWithActivityItems:dataToShare applicationActivities:nil]
but this doesn't prefill the recipient field with the specified email.
It would be nice if the behaviour is the same with UIActivityViewController but let's us prefill information such as recipient.
Any suggestions ?
You can use non-default apps on macos but I don't think you can do so on iOS. Have a look at e.g. https://developer.apple.com/documentation/uikit/uiapplication/1648685-openurl?language=objc .
You could try with a URL similar to below to send with a given subject and body.
#"message://mailto://sample#gmail.com?subject=Subject&body=Body"
I think if you drop the message bit it will still work. I use it like that inside a HTML page inside an app somewhere.
If you need any special characters you will have to encode them in the subject and body.

WKWebView AJAX calls losing cookies

Our customer requested us to do a switch from WebView to WKWebView recently. Their app uses native login, which is done by a 2 POST calls to their backend, returning various authorization cookies that were later on used in every single HTTP/AJAX call throughout the whole app.
With the WebView, it all worked like a charm without a need to implement a single line of custom code. User logged in, cookies were stored to cookie storage by default, and WebView always took them from there and used them, since the HTTPCookieStorage was shared between NSURLSession and WebView.
It it a whole new story with WKWebView. Once we switched WebView to WKWebView, we saw that the authorization was not working. It was due to losing some cookies in the WKWebView. We store the cookies from the NSURLSession response now and append them to the WKWebView manually, by adding "Cookie" header to the HTTP requests.
We were able to get the authorization for HTTP calls work this way, but now we are seeing a new problem. Somehow, all the AJAX calls done in the WKWebView lose the authorization cookies.
Do you please know if there is any way to somehow have the authorization cookies appear in the AJAX calls too? Injecting javascript with
javascriptCookieString = #"document.cookie = 'testCookie=testValue';";
[self.webView evaluateJavaScript:javascriptCookieString completionHandler:nil];
did not work and it seems like there is no control whatsoever over Javascript calls, so I cannot alter the requests before they are being executed. Thank you.
I found that the following snippet did the trick for us. We had the same problem.
// add session cookie to ajax calls
WKUserContentController* userContentController =
WKUserContentController.new;
WKUserScript * cookieScript =
[[WKUserScript alloc]
initWithSource: [[User sharedInstance] getJavscriptCookieString]
injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
[userContentController addUserScript:cookieScript];
WKWebViewConfiguration *webViewConfiguration = [[WKWebViewConfiguration alloc] init];
webViewConfiguration.userContentController = userContentController;
webViewConfiguration.preferences.javaScriptCanOpenWindowsAutomatically = true;
_wk = [[WKWebView alloc] initWithFrame:self.view.frame configuration:webViewConfiguration];
The cookie string needs to be formated correct in order to be accepted.
-(NSString*) getJavscriptCookieString {
return [NSString stringWithFormat: #"document.cookie = '%#=%#'", [self getSessionName], [self getSessionValue]];
}
Hope this can be of some help.
See also:
Can I set the cookies to be used by a WKWebView?

How to display the Authentication Challenge in UIWebView for local network url?

I am trying to access a secure for local network url through UIWebView. When I access it through safari, i get an authentication challenge but the same does not appear in my UIWebView in the application. How can I make it appear?
E.g. http://292.168.1.54/TestWeb/Test.pdf
This url working in safari browser but the same url does not appear in my UIWebView.
Any pointers, sample code or links will be very helpful. Thanks a lot.
There are two ways to get to the authentication (in your case probably basic auth) challenge.
-[UIWebViewDelegate webView:shouldStartLoadWithRequest:navigationType:] will give you the request. Now you just start a second request to the same URL and use [NSURLConnectionDelegate connection:willSendRequestForAuthenticationChallenge:] to get the challenge. Then you present a dialog and ask the user for credentials. Save the credentials in NSURLCredentialStorage and then reload the page.
Create a subclass of NSURLProtocol that handles http and https. Similar to this answer and get the authentication challenge there.
I hope this code will help you.
(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.webView.delegate = self;
NSURL *url = [NSURL URLWithString:#"http://skinC.com/abc/"];// here you can write your url that you want to open
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestURL];
AppDelegate *appDel = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[self setNeedsStatusBarAppearanceUpdate];}

PhoneGap/ChildBrowser - POST App Form to ChildBrowser

I am building an iPhone App using PhoneGap. I am using ChildBrowser plugin.
If I have a form in the App with username/pass, is there anyway I can post those information to an URL like www.mydomain.com/login.php and open it in ChildBrowser?
Please let me know.
Thanks.
You can certainly pass GET parameters to childBrowser.showWebPage().
You could intercept the submit of your form in your app and gather up the field names and values passed (assuming this is a login form, so say username and password) and pass them as parameters to the URL opened in ChildBrowser.
childBrowser.showWebPage('https://www.mydomain.com/login.php?username='+username+'&password='+password)
This is a rather unsophisticated version for demonstration purposes. It wouldn't take much to make it better.
The catch is of course that it is being sent via GET. If your login form is expecting POST and you can't change that, you could have a problem.
Edit:
If POST is your only option, perhaps you would be better off using AJAX to post to the form instead of using child browser.
I was looking for a solution, and ended developing it. This doesn't come out of the box in childbrowser for iPhone.
So, here the code that I added to the plugin.
FYI, data should be a string with the format: arg1=value1&arg2=value2&foo=bar
ChildbrowserViewController
I've added the following method there:
- (void)postURL:(NSString*)url data:(NSString*)data{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: [NSURL URLWithString:url]];
[request setHTTPMethod: #"POST"];
[request setHTTPBody: [data dataUsingEncoding: NSUTF8StringEncoding]];
[webView loadRequest: request];
webView.hidden = NO;}
ChildbrowserCommand
I've added the following code in the showWebPage method:
NSString* method = [ options objectForKey:#"method"];
if ([method isEqualToString:#"POST"]){
[childBrowser postURL:url data:[options objectForKey:#"data"]];
}else{
[childBrowser loadURL:url];
}
HTH!
Milton.

iOS & Google APIs REST

I'm trying to connect to Google RESTful API's but all I get went doing so is a login screen.
My goal is to be able to create a Calendar in Google Calendars using RESTful.
I already created my API keys on Google APIs Console.
I did this:
NSString* baseURL = #"https://accounts.google.com/o/oauth2/auth?";
NSString* url = [NSString stringWithFormat:#"%#%#",baseURL,[self dictionaryToURLStringVariables:[self authParameters]]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
Auth parameters
-(NSDictionary*)authParameters
{
NSDictionary* dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:#"code",#"response_type", redirectURI,#"redirect_uri", API_ID,#"client_id", #"https://www.googleapis.com/auth/calendar",#"scope", nil];
return dictionary;
}
The response is working fine but all I get is a Google HTML login.
I checked out and google has it's own Objective-C library which is far too complicated from my point of view and is not based on iDevices but Desktop.
My 1st question is:
Is there a SIMPLE way to do it all via REST ( login, token, CRUD... ) just sending and posting requests?
e.g: http://www.googleapis.com/login?user=MY_EMAIL&password=MY_PASSWORD (<- of course not than simple / insecure but you get the idea...)
For question 1, try implementing this NSURLConnection delegate method. It allows you to supply credentials when the service asks for them.
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge

Resources