Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在phonegap ios中登录Instagram_Javascript_Ios_Cordova_Instagram_Instagram Api - Fatal编程技术网

Javascript 在phonegap ios中登录Instagram

Javascript 在phonegap ios中登录Instagram,javascript,ios,cordova,instagram,instagram-api,Javascript,Ios,Cordova,Instagram,Instagram Api,我是phone gap的新手。我正在尝试为phone gap iphone实现Instagram登录。facinfg调用我正在使用的plugin.cordova2.9.0版本的方法时遇到一些问题。以下是代码: CDVInstagramPlugin.js function startLogin(response) {`enter code here` cordova.exec(onInstaComplete, onInstaNotComplete, 'LoginViewController'

我是phone gap的新手。我正在尝试为phone gap iphone实现Instagram登录。facinfg调用我正在使用的plugin.cordova2.9.0版本的方法时遇到一些问题。以下是代码:

CDVInstagramPlugin.js

function startLogin(response)
{`enter code here`
   cordova.exec(onInstaComplete, onInstaNotComplete, 'LoginViewController', 'initWithWebView', []);

}


//  LoginViewController.h


#import <UIKit/UIKit.h>

@interface LoginViewController : UIViewController<UIWebViewDelegate>
{
    IBOutlet UIWebView *loginWebView;
    IBOutlet UIActivityIndicatorView* loginIndicator;
    IBOutlet UILabel *loadingLabel;
}
@property(strong,nonatomic)NSString *typeOfAuthentication;
-(void)startLogin;
@end




  LoginViewController.m
//  InstagramUnsignedAuthentication


#import "LoginViewController.h"
#import "AppDelegate.h"

#define INSTAGRAM_AUTHURL                               @"https://api.instagram.com/oauth/authorize/"
#define INSTAGRAM_APIURl                                @"https://api.instagram.com/v1/users/"
#define INSTAGRAM_CLIENT_ID                             @"808a4481a80a4f9da83d3596d9e90a53"
#define INSTAGRAM_CLIENTSERCRET                         @"a997b61ab50847278170344873ddcb3b"
#define INSTAGRAM_REDIRECT_URI                          @"http://localhost/newdata/"
#define INSTAGRAM_ACCESS_TOKEN                          @"access_token"
#define INSTAGRAM_SCOPE                                 @"likes+comments+relationships"

@interface LoginViewController ()

@end

@implementation LoginViewController
@synthesize typeOfAuthentication;



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear: animated];

    [[UIApplication sharedApplication] setStatusBarHidden:YES];

}

-(void)initWithWebView//:(UIWebView *)web
{
    NSLog(@"Startlogin call");
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
//    loginWebView=web;

    loginWebView = [[UIWebView alloc]initWithFrame:self.view.frame];
    NSString* authURL = nil;

    if ([typeOfAuthentication isEqualToString:@"UNSIGNED"])
    {
        authURL = [NSString stringWithFormat: @"%@?client_id=%@&redirect_uri=%@&response_type=token&scope=%@&DEBUG=True",
                   INSTAGRAM_AUTHURL,
                   INSTAGRAM_CLIENT_ID,
                   INSTAGRAM_REDIRECT_URI,
                   INSTAGRAM_SCOPE];
    }
    else
    {
        authURL = [NSString stringWithFormat: @"%@?client_id=%@&redirect_uri=%@&response_type=code&scope=%@&DEBUG=True",
                   INSTAGRAM_AUTHURL,
                   INSTAGRAM_CLIENT_ID,
                   INSTAGRAM_REDIRECT_URI,
                   INSTAGRAM_SCOPE];
    }

    [loginWebView setDelegate:self];
    [loginWebView loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: authURL]]];
}


#pragma mark -
#pragma mark delegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
 navigationType:(UIWebViewNavigationType)navigationType
{
    return [self checkRequestForCallbackURL: request];
}

- (void) webViewDidStartLoad:(UIWebView *)webView
{
    [loginIndicator startAnimating];
    loadingLabel.hidden = NO;
    [loginWebView.layer removeAllAnimations];
    loginWebView.userInteractionEnabled = NO;
    [UIView animateWithDuration: 0.1 animations:^{
      //  loginWebView.alpha = 0.2;
    }];
}

- (void) webViewDidFinishLoad:(UIWebView *)webView
{
    [loginIndicator stopAnimating];
    loadingLabel.hidden = YES;
    [loginWebView.layer removeAllAnimations];
    loginWebView.userInteractionEnabled = YES;
    [UIView animateWithDuration: 0.1 animations:^{
        //loginWebView.alpha = 1.0;
    }];
}

- (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [self webViewDidFinishLoad: webView];
}

#pragma mark -
#pragma mark auth logic


- (BOOL) checkRequestForCallbackURL: (NSURLRequest*) request
{
    NSString* urlString = [[request URL] absoluteString];

    if ([typeOfAuthentication isEqualToString:@"UNSIGNED"])
    {
        // check, if auth was succesfull (check for redirect URL)
          if([urlString hasPrefix: INSTAGRAM_REDIRECT_URI])
         {
             // extract and handle access token
             NSRange range = [urlString rangeOfString: @"#access_token="];
             [self handleAuth: [urlString substringFromIndex: range.location+range.length]];
             return NO;
         }
    }
    else
    {
        if([urlString hasPrefix: INSTAGRAM_REDIRECT_URI])
        {
            // extract and handle access token
            NSRange range = [urlString rangeOfString: @"code="];
            [self makePostRequest:[urlString substringFromIndex: range.location+range.length]];
            return NO;
        }
    }

    return YES;
}

-(void)makePostRequest:(NSString *)code
{

    NSString *post = [NSString stringWithFormat:@"client_id=%@&client_secret=%@&grant_type=authorization_code&redirect_uri=%@&code=%@",INSTAGRAM_CLIENT_ID,INSTAGRAM_CLIENTSERCRET,INSTAGRAM_REDIRECT_URI,code];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

    NSMutableURLRequest *requestData = [NSMutableURLRequest requestWithURL:
                                        [NSURL URLWithString:@"https://api.instagram.com/oauth/access_token"]];
    [requestData setHTTPMethod:@"POST"];
    [requestData setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [requestData setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [requestData setHTTPBody:postData];

    NSURLResponse *response = NULL;
    NSError *requestError = NULL;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:requestData returningResponse:&response error:&requestError];
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:nil];
    [self handleAuth:[dict valueForKey:@"access_token"]];

}

- (void) handleAuth: (NSString*) authToken
{
    NSLog(@"successfully logged in with Tocken == %@",authToken);

}


@end
功能启动(响应)
{`在这里输入代码`
exec(onInstaComplete,onInstaNotComplete,'LoginViewController','initWithWebView',[]);
}
//LoginViewController.h
#进口
@接口LoginViewController:UIViewController
{
ibuiwebview*登录webview;
IBOUTLE UIActivityIndicator视图*登录指示器;
IBUILabel*装载标签;
}
@属性(强,非原子)NSString*typeOfAuthentication;
-(无效)斯达洛金;
@结束
LoginViewController.m
//InstagramSignedAuthentication
#导入“LoginViewController.h”
#导入“AppDelegate.h”
#定义INSTAGRAM_AUTHURL@”https://api.instagram.com/oauth/authorize/"
#定义INSTAGRAM_APIURl@”https://api.instagram.com/v1/users/"
#定义INSTAGRAM客户端ID@“808a4481a80a4f9da83d3596d9e90a53”
#定义INSTAGRAM_CLIENTSERCRET@“a997b61ab50847278170344873ddcb3b”
#定义INSTAGRAM_重定向_URI@”http://localhost/newdata/"
#定义INSTAGRAM_ACCESS_TOKEN@“ACCESS_TOKEN”
#定义INSTAGRAM_范围@“喜欢+评论+关系”
@接口LoginViewController()
@结束
@实现LoginViewController
@综合认证类型;
-(无效)viewDidLoad{
[超级视图下载];
//加载视图后执行任何其他设置。
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
/*
#pragma标记-导航
//在基于故事板的应用程序中,您通常需要在导航之前做一些准备
-(void)prepareForSegue:(UIStoryboardSegue*)segue发送方:(id)发送方{
//使用[segue destinationViewController]获取新的视图控制器。
//将选定对象传递给新的视图控制器。
}
*/
-(无效)视图显示:(BOOL)动画
{
[超级视图显示:动画];
[[UIApplication sharedApplication]setStatusBarHidden:是];
}
-(void)initWithWebView/:(UIWebView*)web
{
NSLog(@“Startlogin调用”);
[[UIApplication sharedApplication]setStatusBarHidden:是];
//loginWebView=web;
loginWebView=[[UIWebView alloc]initWithFrame:self.view.frame];
NSString*authURL=nil;
if([typeOfAuthentication isEqualToString:@“UNSIGNED”])
{
authURL=[NSString stringWithFormat:@“%@?客户端id=%@&重定向uri=%@&响应类型=令牌和作用域=%@&调试=真”,
INSTAGRAM_AUTHURL,
INSTAGRAM\u客户端\u ID,
INSTAGRAM_重定向_URI,
INSTAGRAM_SCOPE];
}
其他的
{
authURL=[NSString stringWithFormat:@“%@?客户端id=%@&重定向uri=%@&响应类型=代码和范围=%@&调试=真”,
INSTAGRAM_AUTHURL,
INSTAGRAM\u客户端\u ID,
INSTAGRAM_重定向_URI,
INSTAGRAM_SCOPE];
}
[loginWebView setDelegate:self];
[loginWebView加载请求:[NSURLRequest requestWithURL:[NSURL URLWithString:authURL]];
}
#布拉格标记-
#pragma标记委托
-(BOOL)webView:(UIWebView*)webView应该加载请求:(NSURLRequest*)请求
导航类型:(UIWebViewNavigationType)导航类型
{
返回[self-checkRequestForCallbackURL:request];
}
-(void)webView加载:(UIWebView*)webView
{
[登录指示器开始显示];
loadingLabel.hidden=否;
[loginWebView.layer RemoveAllianimations];
loginWebView.userInteractionEnabled=否;
[UIView animateWithDuration:0.1动画:^{
//loginWebView.alpha=0.2;
}];
}
-(无效)webViewDidFinishLoad:(UIWebView*)webView
{
[登录指示器停止设置动画];
loadingLabel.hidden=是;
[loginWebView.layer RemoveAllianimations];
loginWebView.userInteractionEnabled=是;
[UIView animateWithDuration:0.1动画:^{
//loginWebView.alpha=1.0;
}];
}
-(void)webView:(UIWebView*)webView失败加载错误:(NSError*)错误
{
[自webViewDidFinishLoad:webView];
}
#布拉格标记-
#pragma标记验证逻辑
-(BOOL)checkRequestForCallbackURL:(NSURLRequest*)请求
{
NSString*urlString=[[请求URL]绝对字符串];
if([typeOfAuthentication isEqualToString:@“UNSIGNED”])
{
//检查验证是否成功(检查重定向URL)
if([urlString hasPrefix:INSTAGRAM\u REDIRECT\u URI])
{
//提取和处理访问令牌
NSRange range=[urlString rangeOfString:@“#访问_令牌=“”;
[self-handleAuth:[urlString substringfromfromindex:range.location+range.length];
返回否;
}
}
其他的
{
if([urlString hasPrefix:INSTAGRAM\u REDIRECT\u URI])
{
//提取和处理访问令牌
NSRange range=[urlString rangeOfString:@“code=“”;
[self-makePostRequest:[urlString substringFromIndex:range.location+range.length];
返回否;
}
}
返回YES;
}
-(void)makePostRequest:(NSString*)代码
{
NSString*post=[NSString stringWithFormat:@“客户端id=%@&客户端机密=%@&授权类型=授权\代码和重定向\ uri=%@&代码=%@”,INSTAGRAM\客户端id,INSTAGRAM\客户端客户端Cret,INSTAGRAM\重定向\ uri,代码];
NSData*postData=[post数据使用编码:NSASCIIStringEncoding allowLossyConversion:是];
NSString*postLength=[NSString stringWithFormat:@“%lu”,(无符号长)[postData长度]];
NSMutableURLRequest*requestData=[NSMutableUrlRequestRequestWithURL:
[NSURL URLWithString:@”https://api.instagram.com/oauth/access_token"]];
[requestData setHTTPMethod:@“POST”];
[requestData setValue:postLen
<feature name="LoginViewController">
    <param name="ios-package" value="LoginViewController" />
</feature>