標籤彙整: line

Line API 傳送文字及圖片 for iOS

Line API 我不想看韓文及日文,終於讓我找到英文版的。
我測試之後發現它無法同時傳送文字及圖片,只能一次傳一種格式。

 

傳送文字時,若是網址會變成超連結。

    NSString *plainString = @"Hello, World! 中文測試~";

    NSString *contentKey = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
                                                                                        (CFStringRef)plainString,
                                                                                        NULL,
                                                                                        (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                                                        kCFStringEncodingUTF8);
    NSString *contentType = @"text";

    NSString *urlString = [NSString stringWithFormat:@"line://msg/%@/%@",
         contentType, contentKey];

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];

 

傳送圖片的圖片來源可以是網路、專案或是手機內的,只要是 UIImage 都行。

    UIPasteboard *pasteboard = [UIPasteboard pasteboardWithUniqueName];
    NSString *pasteboardName = pasteboard.name;
    NSURL *imageURL = [NSURL URLWithString:@"https://www.google.com.tw/images/srpr/logo4w.png"];
    [pasteboard setData:UIImagePNGRepresentation([UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]]) forPasteboardType:@"public.png"];

    NSString *contentType = @"image";
    NSString *contentKey = (__bridge_transfer NSString*)CFURLCreateStringByAddingPercentEscapes(NULL,
                                                                                             (CFStringRef)pasteboardName,
                                                                                             NULL,
                                                                                             CFSTR(":/?=,!$&'()*+;[]@#"),
                                                                                             CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));

    NSString *urlString = [NSString stringWithFormat:@"line://msg/%@/%@",
                           contentType, contentKey];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];

 

加上檢查有沒有安裝 Line 再執行以上動作會更好。

    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"line://"]]) {
        // do something
    }