问题
..Hi I'm having a hard time Importing Alamofire I've finished the tutorial but I got an error message in "import Alamofire" line 2.. what should I do??..In buid phases my Target Dependencies was "Alamofire iOS (Alamofire)" that was my only option together with the "Alamofire OSX (Alamofire)" no option for "Alamofire (Alamofire)" like in the tutorial..
import UIKit
import Alamofire
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
Alamofire.request(.GET, "http://ec2-54-169-246-41.ap-southeast-1.compute.amazonaws.com:3000", parameters: nil)
.response { request, response, data, error in
println(request)
println(response)
println(error)
}
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
回答1:
I had a lot of trouble with Alamofire when I tried to copy the project from git and add it to my project. my solution was "s60" comment to use CocoaPods, so here are the steps to using a Podfile:
First open your terminal and install CocoaPoads with the following command:
sudo gem install cocoapods
After installation go to the folder of your app with the commands cd+"name of path" for example:
cd Documents
cd AlamofireProject
When you are inside your project folder use the next command:
pod init
After running this command a Podfile should be created in your directory you should open the Podfile and then specify the version of Alamofire that you want to use, the source of the Podfile and that you want to use frameworks in your app, here is how you Podfile should look
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
target 'YOUR APP NAME' do
pod 'Alamofire', '~> 2.0'
end
After you edit your Podfile save it and then run this command in the terminal:
pod install
If everything runs smoothly now you should have in your app folder a new folder called Pods and a ".xcworkspace" file. Now you must work in the workspace file where you can reference the Alamofire library smoothly like this:
import Alamofire
Alamofire.request(.GET, "https://omgvamp-hearthstone-v1.p.mashape.com/cards",parameters:["X-Mashape-Key:":"ibxqtnAb9BmshWhdS6Z4zttkVtQop1j0yGSjsn6tn5x2Y4BBF0"])
.responseJSON { _, _, result in
switch result {
case .Success(let data):
let json = JSON(data)
debugPrint(data)
self.Photos = self.ParseHS(json)
self.performSegueWithIdentifier("ToCollection", sender: self)
case .Failure(_, let error):
print("Request failed with error: \(error)")
}
}
This is a sample function of an Alamofire request that I use in one of my apps. If you have any trouble leave me a comment. XD Greetings.
回答2:
easiest way to integrate Alamofire is use CocoaPods
add below to your Podfile and run pod update Alamofire will auto integrated into your project
platform :ios, '8.0'
use_frameworks!
pod 'Alamofire', '~> 1.3.0
'
回答3:
Alamofire is very good networking library written in Swift. There are two ways to integrate Alamofire in project:
Cocoa Pod: To Add new
Alamofire
library to your current project go with thepod installation
. below is the code you have to integrate in your pod file for fresh installation of Alamofire.source 'https://github.com/CocoaPods/Specs.git' platform :ios, '8.0' use_frameworks! pod 'Alamofire', '~> 2.0'
With the latest release of Alamofire 2.0 request method get change.
I am posting some sample steps which will help you most
Here is my sample code, //Step:
import Alamofire
// Step : 1
var manager = Manager.sharedInstance
// Specifying the Headers we need
manager.session.configuration.HTTPAdditionalHeaders = [
"Content-Type": "application/graphql",
"Accept": "application/json" //Optional
]
// Step : 3 then call the Alamofire request method.
Alamofire.request(.GET, url2).responseJSON { request, response,result in
print(result.value)
}
Try this or you can check the latest update from alamofire on xcode 7:
https://github.com/Alamofire/Alamofire
In previous version of Alamofire there is one more parameter for handle in request method, now that was removed. Check Git for more information. I think this will help you more.
- Mannual: Download the Alamofire from git: https://github.com/Alamofire/Alamofire , then extract the downloaded folder.
From your project -> click on add files to "project"
-> navigate to extracted folder -> find Alamofire.xcodeproj
-> select the Alamofire.xcodeproj
file -> please make sure Copy items if needed
is checked.
Now its time to add Alamofire into embedded binaries. To do that click on project name and then navigate to General tab -> select Embedded Binaries
-> click on '+' -> now add Alamofire.xcodeproj
.
Lastly use import Alamofire
in .swift file, whenever you want to use Alamofire.
But my personal opinioin is, if you are using Xcode 7
and Swift 2
then you must go with the Cocoapod installation for Alamofire
来源:https://stackoverflow.com/questions/31824691/error-importing-alamofire