当前位置:   article > 正文

IOS-海外版授权分享推送统计 Google FaceBook Twitter Instagram_友盟分享海外版

友盟分享海外版

目录

备注

Google

Google分享 

Google推送

FaceBook

FaceBook分享

Twitter

Twitter分享

Instagram

Instagram分享

SDK下载 

UIWebView转WKwebView问题



备注

如果已经有使用了友盟的话,改外海外版,授权可以使用facebook和twitter,google和ins需要单独使用,如果是直接就准备做海外版,强烈推荐使用Fierbase

Google

Google官方文档以及Demo 

https://developers.google.cn/identity/sign-in/ios/sdk/

1.根据文档添加GoogleSignInDependencies.framework ,GoogleSignIn.framework ,GoogleSignIn.bundle如果不用官方按钮的话就不需要这个

2.根据文档添加依赖

3.生成client ID,点击绿色按钮,最后生成

static NSString * const kClientID =@"AAAA.apps.googleusercontent.com";

 [GIDSignIn sharedInstance].clientID = kClientID;//用到的就是这个ID,生成的时候填写的bundleID需要建一个应用
 

4.填写URL Type保证跳转正常,格式为com.googleusercontent.apps.AAAA,AAAA就是生成kClientID的前半部分

5.代码部分

  1. //
  2. // CNMShareGoogle.h
  3. // YoYoGame
  4. //
  5. // Created by gaoshuang on 2019/2/15.
  6. // Copyright © 2019 SINASHOW. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import <GoogleSignIn/GoogleSignIn.h>
  10. @interface CNMShareGoogle : NSObject<CNMHelperProtocol>
  11. /**
  12. 设置google Platform
  13. */
  14. -(void)cnm_setupGooglePlatforms;
  15. /**
  16. 设置googleDelegate
  17. */
  18. -(void)cnm_setupGoogleDelegate;
  19. /**
  20. 登录
  21. */
  22. -(void)cnm_loginWithWithBlock:(void (^)(BOOL isSuccessed,GIDGoogleUser* userInfoResp))block;
  23. /**
  24. 回调调转
  25. @param url
  26. @param sourceApplication
  27. @param annotation
  28. @return
  29. */
  30. - (BOOL)cnm_handleURL:(NSURL *)url
  31. sourceApplication:(NSString *)sourceApplication
  32. annotation:(id)annotation;
  33. @end
  34. //
  35. // CNMShareGoogle.m
  36. // YoYoGame
  37. //
  38. // Created by gaoshuang on 2019/2/15.
  39. // Copyright © 2019 SINASHOW. All rights reserved.
  40. //
  41. #import "CNMShareGoogle.h"
  42. //static NSString * const kClientID =
  43. //@"589453917038-qaoga89fitj2ukrsq27ko56fimmojac6.apps.googleusercontent.com";
  44. static NSString * const kClientID =
  45. @"868607979203-2jjajqegiv7t5to8o69409fh6lvg9hcl.apps.googleusercontent.com";
  46. @interface CNMShareGoogle()<GIDSignInDelegate, GIDSignInUIDelegate>
  47. @property (copy, nonatomic)void (^blockLogin)(BOOL isSuccessed,GIDGoogleUser* userInfoResp);
  48. @end
  49. @implementation CNMShareGoogle
  50. HELPER_SHARED(CNMShareGoogle)
  51. -(void)cnm_setupGooglePlatforms{
  52. [GIDSignIn sharedInstance].clientID = kClientID;
  53. }
  54. - (BOOL)cnm_handleURL:(NSURL *)url
  55. sourceApplication:(NSString *)sourceApplication
  56. annotation:(id)annotation{
  57. return [[GIDSignIn sharedInstance] handleURL:url
  58. sourceApplication:sourceApplication
  59. annotation:annotation];
  60. return YES;
  61. }
  62. -(void)cnm_setupGoogleDelegate{
  63. GIDSignIn *signIn = [GIDSignIn sharedInstance];
  64. signIn.shouldFetchBasicProfile = YES;
  65. signIn.delegate = self;
  66. signIn.uiDelegate = self;
  67. }
  68. -(void)cnm_loginWithWithBlock:(void (^)(BOOL isSuccessed,GIDGoogleUser* userInfoResp))block{
  69. self.blockLogin = nil;
  70. self.blockLogin = block;
  71. [[GIDSignIn sharedInstance] signIn];
  72. }
  73. #pragma mark - GIDSignDelegate
  74. - (void)signIn:(GIDSignIn *)signIn
  75. didSignInForUser:(GIDGoogleUser *)user
  76. withError:(NSError *)error {
  77. // Perform any operations on signed in user here.
  78. // NSString *userId = user.userID; // For client-side use only!
  79. // NSString *idToken = user.authentication.idToken; // Safe to send to the server
  80. // NSString *fullName = user.profile.name;
  81. // NSString *givenName = user.profile.givenName;
  82. // NSString *familyName = user.profile.familyName;
  83. // NSString *email = user.profile.email;
  84. //性别是取不到的,不需要根据acctoken取用户信息了
  85. if (!error) {
  86. if (self.blockLogin) {
  87. self.blockLogin(YES,user);
  88. }
  89. }else{
  90. if (self.blockLogin) {
  91. self.blockLogin(NO,user);
  92. }
  93. }
  94. // ...
  95. }
  96. - (void)signIn:(GIDSignIn *)signIn
  97. didDisconnectWithUser:(GIDGoogleUser *)user
  98. withError:(NSError *)error{
  99. if (self.blockLogin) {
  100. self.blockLogin(NO,user);
  101. }
  102. }
  103. #pragma mark - GIDSignInUIDelegate
  104. //该代理一般不用实现,除非自己对界面跳转或交互有一些需求
  105. - (void)signInWillDispatch:(GIDSignIn *)signIn error:(NSError *)error {
  106. // [myActivityIndicator stopAnimating];
  107. }
  108. // Present a view that prompts the user to sign in with Google
  109. - (void)signIn:(GIDSignIn *)signIn
  110. presentViewController:(UIViewController *)viewController {
  111. [[self getCurrentVC] presentViewController:viewController animated:YES completion:nil];
  112. }
  113. // Dismiss the "Sign in with Google" view
  114. - (void)signIn:(GIDSignIn *)signIn
  115. dismissViewController:(UIViewController *)viewController {
  116. [[self getCurrentVC] dismissViewControllerAnimated:YES completion:nil];
  117. }
  118. @end

Google分享 

关闭

Google推送

流程,控制台添加应用,生成GoogleService-Info.list加入工程

控制台配置生产和发布证书

  1. //需要外网环境才能返回
  2. #pragma mark FIRMessagingDelegate
  3. // [END ios_10_message_handling]
  4. // [START refresh_token]
  5. //获取注册令牌
  6. - (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken

获取到令牌在控制台测试发消息

客户端获取令牌提交到服务器

 

https://www.jianshu.com/p/8281047bcdec

https://www.freesion.com/article/8801507287/

 

FaceBook

由于FaceBook使用到了友盟 传送门 (不建议混合使用,会很乱,海外版的直接用Firebase集成吧)

  1. #import <FBSDKCoreKit/FBSDKCoreKit.h>
  2. #import <FBSDKLoginKit/FBSDKLoginKit.h>
  1. FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
  2. [login logOut];//避免出现 FBSDK Login Error Code: 308
  3. [login logInWithReadPermissions: @[@"public_profile",@"email"]
  4. fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result,
  5. NSError *error) {
  6. if (error) {
  7. NSLog(@"Process error");
  8. } else if (result.isCancelled) {
  9. NSLog(@"Cancelled");
  10. } else {
  11. NSLog(@"Logged in");
  12. NSDictionary*params=[NSDictionary new];
  13. FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
  14. initWithGraphPath:result.token.userID
  15. parameters:params
  16. HTTPMethod:@"GET"];
  17. [request
  18. startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
  19. id result,
  20. NSError *error) {
  21. NSString*name=[result objectForKey:@"name"];
  22. NSString*userid=[result objectForKey:@"id"];
  23. NSLog(@"name=%@ userID=%@ ",name,userid);
  24. }];
  25. }
  26. }];

 

FaceBook分享

使用官方sdkdemo的分享 传送门

友盟分享统一调用

  1. //sharWebUrl 一定得是https://标准格式,不然会跳转不进去,很坑,而且进入编辑界面点击返回键,放弃编辑,如果点击左上角返回应用会卡住
  2. FBSDKShareLinkContent *linkContent = [[FBSDKShareLinkContent alloc] init];
  3. linkContent.contentURL = [NSURL URLWithString:sharWebUrl];
  4. linkContent.contentTitle = titile;
  5. linkContent.contentDescription = content;
  6. linkContent.imageURL = [NSURL URLWithString:imageUrl];
  7. FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init];
  8. dialog.fromViewController = [self gs_getCurrentVC];
  9. dialog.shareContent = linkContent;
  10. BOOL isInstalled = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"fbapi://"]];
  11. if (isInstalled)
  12. {
  13. dialog.mode = FBSDKShareDialogModeNative;
  14. }
  15. else
  16. {
  17. dialog.mode = FBSDKShareDialogModeAutomatic;
  18. }
  19. [dialog show];

 

https://www.jianshu.com/p/dd35278ee0a3

使用官方sdkdemo 传送门

Twitter

Twitter也使用到了友盟,但是友盟返回值中缺失authTokenSecret,需要手动掉用loadUserWithID方法

 

 urltype设置  twitterkit-APPKEY

  1. 独立
  2. Twitter 3.0 sdk的话
  3. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  4. // Override point for customization after application launch.
  5. [[Twitter sharedInstance] startWithConsumerKey:@"guLppo4I" consumerSecret:@"xWpL85O3d3j6L0xTXC17j"];
  6. return YES;
  7. }
  8. - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
  9. // return [[Twitter sharedInstance] application:app openURL:url options:options];
  10. if ([[Twitter sharedInstance] application:app openURL:url options:options]) {
  11. return YES;
  12. }
  13. // If you handle other (non Twitter Kit) URLs elsewhere in your app, return YES. Otherwise
  14. return NO;
  15. }
  16. //自定义xib按钮触发事件
  17. - (IBAction)loginAction:(id)sender {
  18. [[Twitter sharedInstance] logInWithCompletion:^(TWTRSession * _Nullable session, NSError * _Nullable error) {
  19. if(session){
  20. NSLog(@"%@已登录",session.userName);
  21. [self loadTwitterUserWithID:session.userID];
  22. } else {
  23. NSLog(@"error:%@",error.localizedDescription);
  24. }
  25. }];
  26. }
  27. //获取Twitter用户信息
  28. - (void)loadTwitterUserWithID:(NSString *)userId{
  29. TWTRAPIClient *client = [TWTRAPIClient clientWithCurrentUser];
  30. [client loadUserWithID:userId completion:^(TWTRUser * _Nullable user, NSError * _Nullable error) {
  31. if (user) {
  32. NSLog(@"头像url:%@",user.profileImageURL);
  33. }else{
  34. NSLog(@"error:%@",error.localizedDescription);
  35. }
  36. }];
  37. }
  38. //管理多个用户时需要注销用户
  39. - (void)logoutTwitterUser{
  40. TWTRSessionStore *store = [[Twitter sharedInstance] sessionStore];
  41. NSString *userID = store.session.userID;
  42. [store logOutUserID:userID];
  43. }

https://www.jianshu.com/p/50f67bd865ac 

Twitter分享

  1. NSString* strDescription =@"用DataYuk,邂逅属于你的爱,快来相亲,交友,谈恋爱吧";
  2. NSString* strWebURL = @"https://winkydate.com";
  3. // Example
  4. NSMutableArray *parameter = [NSMutableArray array];
  5. if (![strDescription isEqualToString:@""]) {
  6. [parameter addObject:[NSString stringWithFormat:@"text=%@", strDescription]];
  7. }
  8. if (![strWebURL isEqualToString:@""]) {
  9. [parameter addObject:[NSString stringWithFormat:@"url=%@", strWebURL]];
  10. }
  11. NSString *shareWebLink = [[NSString stringWithFormat:@"https://twitter.com/intent/tweet?%@",
  12. [parameter componentsJoinedByString:@"&"]]
  13. stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  14. if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:shareWebLink]]) {
  15. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:shareWebLink]];
  16. }
  17. return;
  18. // //检查是否当前会话具有登录的用户
  19. // if ([[Twitter sharedInstance].sessionStore hasLoggedInUsers]) {
  20. // TWTRComposer *composer = [[TWTRComposer alloc] init];
  21. // [composer setText:messageObject.text];
  22. // //带图片方法
  23. // [composer setImage: shareObject.thumbImage ];
  24. // [composer setURL:[NSURL URLWithString:@"https://winkydate.com"]];
  25. // [composer showFromViewController:self completion:^(TWTRComposerResult result){
  26. // if(result == TWTRComposerResultCancelled) {
  27. // //分享失败
  28. // }else{
  29. // //分享成功
  30. // }
  31. // }];
  32. // }else{
  33. // [[Twitter sharedInstance] logInWithCompletion:^(TWTRSession *session, NSError *error) {
  34. // if (session) {
  35. // TWTRComposer *composer = [[TWTRComposer alloc] init];
  36. // [composer setText:messageObject.text];
  37. // //带图片方法
  38. // [composer setImage: shareObject.thumbImage ];
  39. // [composer setURL:[NSURL URLWithString:@"https://winkydate.com"]];
  40. // [composer showFromViewController:self completion:^(TWTRComposerResult result){
  41. // if(result == TWTRComposerResultCancelled) {
  42. // //分享失败
  43. // }else{
  44. // //分享成功
  45. // }
  46. // }];
  47. // NSLog(@"signed in as %@", [session userName]);
  48. // } else {
  49. // NSLog(@"error: %@", [error localizedDescription]);
  50. // }
  51. // }];
  52. // }
  53. // [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_Twitter messageObject:messageObject currentViewController:[self gs_getCurrentVC] completion:^(id data, NSError *error) {
  54. // if (error) {
  55. // // [[self gs_getCurrentVC] showToast:error.userInfo[@"message"]];
  56. // }else{
  57. // [[self gs_getCurrentVC] showToast:gs_language(@"分享成功")];
  58. // }
  59. // }];

https://www.jianshu.com/p/4ccdb3a6dc67

https://www.jianshu.com/p/2d8992a278f4

 https://www.jianshu.com/p/50f67bd865ac

Instagram

Instagram官方文档   

Demo 

  1. #import <UIKit/UIKit.h>
  2. @interface UserModel: NSObject
  3. @property (strong, nonatomic)NSString* full_name;
  4. @property (strong, nonatomic)NSString* idD;
  5. @property (strong, nonatomic)NSString* is_business;
  6. @property (strong, nonatomic)NSString* profile_picture;
  7. @property (strong, nonatomic)NSString* username;
  8. @property (strong, nonatomic)NSString* website;
  9. @property (strong, nonatomic)NSString* access_token;
  10. @end
  11. @interface IKLoginViewController : UIViewController
  12. @property (strong, nonatomic)void (^Block)(BOOL isSuccessed, UserModel* model);
  13. @end
  14. #import "IKLoginViewController.h"
  15. #import <WebKit/WebKit.h>
  16. @implementation UserModel
  17. @end
  18. @interface IKLoginViewController () <WKNavigationDelegate>
  19. @end
  20. @implementation IKLoginViewController
  21. - (void)viewDidLoad
  22. {
  23. [super viewDidLoad];
  24. WKWebViewConfiguration *webConfiguration = [[WKWebViewConfiguration alloc] init];
  25. webConfiguration.websiteDataStore = [WKWebsiteDataStore defaultDataStore];
  26. WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:webConfiguration];
  27. webView.scrollView.bounces = NO;
  28. self.navigationItem.title = @"Instagram";
  29. [self.navigationItem.rightBarButtonItem setEnabled:NO];
  30. NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.instagram.com/oauth/authorize/?client_id=%@&redirect_uri=%@&response_type=code",@"883a3fe2c91f45a38a4354c82a9da7a3",@"http://www.funplanet.cn/"]];
  31. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  32. [self.view addSubview:webView];
  33. webView.navigationDelegate = self;
  34. [webView loadRequest:request];
  35. }
  36. - (void)authenticationSuccess
  37. {
  38. [self.navigationItem setLeftBarButtonItem:nil];
  39. [self.navigationItem.rightBarButtonItem setEnabled:YES];
  40. }
  41. - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
  42. {
  43. if ([navigationResponse.response.URL.absoluteString hasPrefix:@"http://www.funplanet.cn/?code="]){
  44. }
  45. decisionHandler(WKNavigationActionPolicyAllow);
  46. }
  47. - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(nonnull WKNavigationAction *)navigationAction decisionHandler:(nonnull void (^)(WKNavigationActionPolicy))decisionHandler
  48. {
  49. NSString* str =navigationAction.request.URL.absoluteString;
  50. if ([navigationAction.request.URL.absoluteString hasPrefix:@"http://www.funplanet.cn/?code="]){
  51. str = [str substringFromIndex:[str rangeOfString:@"code="].location+5];
  52. NSDictionary* dict = @{@"client_id":@"883a3fe2c91f45a38a43AAAAA",//应用id,登录的测试账号需要被邀请加入沙盒环境,或者用审核应用id的账号
  53. @"client_secret":@"345f20582ed343e8a494f20BBBBB",//应用私钥
  54. @"code":[str toString],//重定向后取到的codeid
  55. @"grant_type":@"authorization_code",//固定的值
  56. @"redirect_uri":@"http://www.funplanet.cn/"//固定回调的url
  57. };
  58. [self POSTDictionary:@"https://api.instagram.com/oauth/access_token" params:dict tag:1 HTTPS:YES success:^(AFHTTPRequestOperation *operation, id responseObject) {
  59. if (responseObject) {
  60. UserModel* model = [[UserModel alloc]init];
  61. model.access_token =responseObject[@"access_token"];
  62. model.full_name =responseObject[@"user"][@"full_name"];
  63. model.idD = [responseObject[@"user"][@"id"] toString];
  64. model.is_business =responseObject[@"user"][@"is_business"];
  65. model.profile_picture =responseObject[@"user"][@"profile_picture"];
  66. model.username =responseObject[@"user"][@"username"];
  67. if (_Block) {
  68. _Block(YES, model);
  69. [self popCanvas];
  70. }
  71. }else{
  72. if (_Block) {
  73. _Block(NO, nil);
  74. [self popCanvas];
  75. }
  76. }
  77. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  78. if (_Block) {
  79. _Block(NO, nil);
  80. [self popCanvas];
  81. }
  82. }];
  83. decisionHandler(WKNavigationActionPolicyCancel);
  84. }
  85. decisionHandler(WKNavigationActionPolicyAllow);
  86. }
  87. - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(null_unspecified WKNavigation *)navigation {
  88. }
  89. - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error
  90. {
  91. // if ([navigationResponse.response.URL.absoluteString hasPrefix:@"http://www.funplanet.cn/?code="]){
  92. //
  93. // }
  94. }
  95. @end

Instagram 唤起不了客户端,只能网页进行,退出登录的方法

  1. WKWebsiteDataStore *dateStore = [WKWebsiteDataStore defaultDataStore];
  2. [dateStore fetchDataRecordsOfTypes:[WKWebsiteDataStore allWebsiteDataTypes]
  3. completionHandler:^(NSArray<WKWebsiteDataRecord *> * __nonnull records) {
  4. for (WKWebsiteDataRecord *record in records)
  5. {
  6. if ( [record.displayName containsString:@"instagram.com"]) //取消备注,可以针对某域名清除,否则是全清
  7. {
  8. [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:record.dataTypes
  9. forDataRecords:@[record]
  10. completionHandler:^{
  11. NSLog(@"Cookies for %@ deleted successfully",record.displayName);
  12. }];
  13. }
  14. }
  15. }];

 

Instagram分享

  1. NSString *shareWebLink = [NSString stringWithFormat:@"instagram://app"];
  2. if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:shareWebLink]]) {
  3. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:shareWebLink]];
  4. }
  5. 另外有进入发布页面选择相册以及固定相册图片的方法

SDK下载 

Google https://developers.google.cn/identity/sign-in/ios/sdk/

FaceBook https://github.com/facebook/facebook-ios-sdk

编译需要添加一个空的swift文件(Swift module的问题)

如果遇上Redefinition of module 'Swift',建议重新pod一下

Twitter  https://github.com/twitter-archive/twitter-kit-ios

UIWebView转WKwebView问题

ITMS-90809: Deprecated API Usage - New apps that use UIWebView are no longer accepted. Instead, use WKWebView for improved security and reliability. 传送门

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/248563
推荐阅读
相关标签
  

闽ICP备14008679号