Reputation: 725
I have an existing database up and running on the remote MS SQL server
, and I want to be able to communicate and interact with that database from Xcode
. I am writing an application for OS X
in Swift
, and the data that the application should use, is stored in that remote database.
The problem is, I can't seem to find any Swift
library that could connect to the MS SQL server
based database. So far, I have only found this open-source library: SQLClient in Objective-C, but it's quite difficult to have it set, especially as I am not familiar with the Objective-C
.
Also, I keep seeing this Core-Data
library being mentioned anytime there is some communication with the database, but as far as I understand Core-Data
doesn't know how to connect to the MS SQL
database.
Does anyone have any experience in connecting the Xcode Swift app
to the remote MS SQL
database? How should one do this? Any kind of advice is more than welcome, because right now I am kind of stuck with this problem.
Upvotes: 6
Views: 19528
Reputation: 1223
I suggest to write a webservice with php for example to consume mysql data and provide a json/xml response : this is an example of code that can help you to getdata from Database and parse it to json .
function getOneAdminByUserName($cin){
require('connect.php');
//fetch table rows from mysql db
$sql = "SELECT * FROM `admin` WHERE cin= '".$cin."'";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
if(mysqli_num_rows($result)>0){
$myArray = array();
while($row =mysqli_fetch_assoc($result))
{
array_push( $myArray , $row);
}
return json_encode($myArray);
//return json_encode(mysqli_fetch_assoc($result));
}
else{
return "no";
}
Then you can consume service on your ios app with AFnetworking
NSURL *URL = [NSURL URLWithString:@"http://example.com/resources/123.json"];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:URL.absoluteString parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
Upvotes: 2
Reputation: 16
It's really bad for an application (mobile, web, any client) to directly have access to any database for security issues. Clients really should not be accessing the database, which is often holding very private/secure data. It opens up vulnerabilities (i.e., sql injection attack).
A set of web services written in php or java or some back-end technology is a more secure, scalable system. These web services can connect to the database and retrieve data. Your iOS application can call the web services and receive the data..for example in the form of XML or JSON.
So better you to use webservice.
Also coredata can use below 4 persistant store
Upvotes: 0
Reputation: 750
The ideal way is to write webservices in any serverside language such as php, asp etc. The webservices will communicae with your mysql database and you swift code will communicate to the webservices.
Upvotes: 2