2015-10-17 55 views
0

我是新手機gap.I試圖實現Instagram登錄手機差距iphone。 facinfg一些問題調用的plugin.cordova2.9.0版的方法,我using.Here是下面的代碼:Instagram的登錄電話ios

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 

的Config.xml

<feature name="LoginViewController"> 
    <param name="ios-package" value="LoginViewController" /> 
</feature> 

+0

請讓我知道另一種方式爲這個或任何我做wrong.Thanks –

回答

0

@subir,

您正在使用outdate d代碼示例。

你已經一:

Top Mistakes by Developers new to Cordova/Phonegap

你打11和12

11.你需要從NPM獲得你的插件了。

關於採購插件的規則可能會相當混亂。最好的辦法是閱讀下面的博客文章。使用CLI的開發人員可以從github獲得資源,並再次查看博文。

2015-10-09 - 如果沒有通告,推文或博客,存儲庫會在計劃前一週更改。我什麼都不能做,但是抱怨......這是令人討厭的。

12. <feature>已被棄用

<feature>標籤被棄用。這意味着他們不再使用。 您可以read about it here

+0

對不起,沒有得到您的回覆。我一直忘記告訴人們。 ** iOS9 [非官方支持](https://github.com/jessemonroy650/top-phonegap-mistakes/blob/master/current-tripping-points.md)** – JesseMonroy650