MST

星途 面试题库

面试题:Objective-C的Social框架下,针对不同社交平台分享内容的定制与适配

使用Objective-C的Social框架实现分享功能,以Facebook、Twitter和微信(假设已集成相关SDK并与Social框架协作)为例,说明如何针对不同社交平台定制分享内容的格式、图片尺寸要求以及文本长度限制等,并给出关键代码片段。
47.3万 热度难度
编程语言Objective-C

知识考点

AI 面试

面试题答案

一键面试

Facebook

  • 分享内容格式:支持文本、图片、链接等。分享链接时,Facebook 会自动提取链接中的标题、描述和图片。
  • 图片尺寸要求:理想的图片尺寸是 1200 x 630 像素,最小宽度为 600 像素。
  • 文本长度限制:一般建议分享文本不超过 400 字符,过长可能会被截断。

关键代码片段:

#import <Social/Social.h>
#import <Accounts/Accounts.h>

- (void)shareOnFacebookWithText:(NSString *)text image:(UIImage *)image link:(NSURL *)link {
    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *facebookAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
    [accountStore requestAccessToAccountsWithType:facebookAccountType options:nil completion:^(BOOL granted, NSError *error) {
        if (granted) {
            NSArray *facebookAccounts = [accountStore accountsWithAccountType:facebookAccountType];
            if (facebookAccounts.count > 0) {
                ACAccount *facebookAccount = facebookAccounts[0];
                SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
                if (text) {
                    [controller setInitialText:text];
                }
                if (image) {
                    [controller addImage:image];
                }
                if (link) {
                    [controller addURL:link];
                }
                [self presentViewController:controller animated:YES completion:nil];
            }
        }
    }];
}

Twitter

  • 分享内容格式:主要是文本,也可添加图片。链接会自动缩短。
  • 图片尺寸要求:建议图片尺寸为 440 x 220 像素,最大支持 5MB。
  • 文本长度限制:推文限制在 280 字符以内。

关键代码片段:

- (void)shareOnTwitterWithText:(NSString *)text image:(UIImage *)image link:(NSURL *)link {
    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *twitterAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    [accountStore requestAccessToAccountsWithType:twitterAccountType options:nil completion:^(BOOL granted, NSError *error) {
        if (granted) {
            NSArray *twitterAccounts = [accountStore accountsWithAccountType:twitterAccountType];
            if (twitterAccounts.count > 0) {
                ACAccount *twitterAccount = twitterAccounts[0];
                SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
                if (text) {
                    [controller setInitialText:text];
                }
                if (image) {
                    [controller addImage:image];
                }
                if (link) {
                    [controller addURL:link];
                }
                [self presentViewController:controller animated:YES completion:nil];
            }
        }
    }];
}

微信(假设已集成相关SDK并与Social框架协作)

  • 分享内容格式:支持文本、图片、链接、音乐、视频等。不同类型分享需遵循相应格式。
  • 图片尺寸要求:缩略图建议尺寸为 150 x 150 像素,分享图片最大支持 10MB。
  • 文本长度限制:分享文本长度一般限制在 1024 字符以内。

关键代码片段: 假设已导入微信SDK头文件 WXApi.h 并配置好相关代理等。

- (void)shareOnWeChatWithText:(NSString *)text image:(UIImage *)image link:(NSURL *)link {
    if ([WXApi isWXAppInstalled]) {
        if (image) {
            // 处理图片为缩略图等操作
            // 假设已有方法生成符合尺寸的缩略图thumbImage
            UIImage *thumbImage = [self generateThumbImage:image];
            WXMediaMessage *message = [WXMediaMessage message];
            message.title = @"分享标题";
            message.description = text;
            [message setThumbImage:thumbImage];
            WXWebpageObject *webpageObject = [WXWebpageObject object];
            webpageObject.webpageUrl = link.absoluteString;
            message.mediaObject = webpageObject;
            SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
            req.bText = NO;
            req.message = message;
            req.scene = WXSceneSession; // 分享到会话,如需分享到朋友圈等可修改此处
            [WXApi sendReq:req];
        } else if (text) {
            SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
            req.bText = YES;
            req.text = text;
            req.scene = WXSceneSession;
            [WXApi sendReq:req];
        }
    }
}

- (UIImage *)generateThumbImage:(UIImage *)originalImage {
    // 简单示例:根据尺寸缩放图片
    CGFloat targetWidth = 150;
    CGFloat targetHeight = 150;
    CGSize imageSize = originalImage.size;
    CGFloat widthRatio = targetWidth / imageSize.width;
    CGFloat heightRatio = targetHeight / imageSize.height;
    CGFloat scaleFactor = widthRatio < heightRatio ? widthRatio : heightRatio;
    CGFloat scaledWidth = scaleFactor * imageSize.width;
    CGFloat scaledHeight = scaleFactor * imageSize.height;
    CGRect rect = CGRectMake(0, 0, scaledWidth, scaledHeight);
    UIGraphicsBeginImageContext(rect.size);
    [originalImage drawInRect:rect];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}