2016-11-06 87 views
0

我在swift中運行示例應用程序。我需要分享我的應用程序的圖像。 在Social framework的幫助下,我整合了FacebookTwitter。現在,我不知道如何與InstagramWhatsAppiOS:在社交網絡中共享圖像Swift

分享按鈕操作

@IBAction func shareAcn(sender: UITapGestureRecognizer) { 

    var documentController: UIDocumentInteractionController! 

    let fileURL = NSBundle.mainBundle().pathForResource("smc", ofType: "png") 
    documentController = UIDocumentInteractionController.init(URL: NSURL(fileURLWithPath: fileURL!)) 
    documentController.presentOptionsMenuFromRect(CGRectZero, inView: self.view, animated: true) 
} 

整合了WhatsApp

@IBAction func shareAcn(sender: UITapGestureRecognizer) { 

    var documentController: UIDocumentInteractionController! 
    let image = UIImage(named: "smc") 
    let filename = "myimage.wai" 
    let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, false)[0] as NSString 
    let destinationPath = documentsPath.stringByAppendingString("/" + filename) 
    UIImagePNGRepresentation(image!)!.writeToFile(destinationPath, atomically: false) 
    let fileURL = NSURL(fileURLWithPath: destinationPath) as NSURL 
    documentController = UIDocumentInteractionController.init(URL: fileURL) 
    documentController.UTI = "net.whatsapp.image" 
    documentController.presentOptionsMenuFromRect(CGRectZero, inView: self.view, animated: true) 
} 

如果我選擇[導入到WhatsApp的或複製到的WhatsApp]應用墜毀。如果我選擇WhatsApp,「此項目無法發送」。

Instagram的

var instagramURL: NSURL = NSURL(string: "instagram://user?username=USERNAME") 
if UIApplication.sharedApplication().canOpenURL(instagramURL) { 
    UIApplication.sharedApplication().openURL(instagramURL) 
} 

無代碼工作。

我安裝了Instagram並作爲我的用戶登錄到我的測試設備中,如果我運行,Instagram未打開。

我不知道如何解決這個問題?請引導我。

+0

見:http://stackoverflow.com/a/30988328/1457385 – shallowThought

+0

LSApplicationsQueriesSchemes? Info.plist在哪裏?我無法找到它。 –

+0

如果你在你的if語句中放置了斷點,它是否曾經打過它? – colinrf

回答

1

請確保您已在文件中設置LSApplicationQueriesSchemes條目,並聲明它嘗試查詢哪些方案。

<key>LSApplicationQueriesSchemes</key> 
<array> 
    <string>whatsapp</string> 
    <string>instagram</string> 
</array> 

不包含「://」。

如果您確實包含「://」canOpenUrl:will return NO。當您實際調用canOpenUrl時:仍必須包含冒號,即canOpenUrl:@"instagram:"

您可以在this WWDC 2015 video中找到更多信息。


而且,這條線是可疑:

var instagramURL: NSURL = NSURL(string: "instagram://user?username=USERNAME") 

我懷疑你沒有從Instagrams documentation複製此。如果是這樣,請用您的實際用戶名替換USERNAME


要找出錯誤,你可能想先嚐試一個簡單的網址:

var instagramURL: NSURL = NSURL(string: "instagram://app")